<commit_before>/*
 *     Copyright 2016 <NAME> & <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.dv8tion.jda.client;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import net.dv8tion.jda.JDA;
import net.dv8tion.jda.JDABuilder;
import net.dv8tion.jda.hooks.IEventManager;
import net.dv8tion.jda.requests.Requester;
import org.json.JSONObject;

import javax.security.auth.login.LoginException;

public class JDAClientBuilder extends JDABuilder
{
    protected String email = null;
    protected String password = null;

    public JDAClientBuilder()
    {
        Requester.USER_AGENT = "JDA-Client DiscordBot (" + JDAClientInfo.GITHUB + ", " + JDAClientInfo.VERSION + ")";
    }

    public JDAClientBuilder setEmail(String email)
    {
        this.email = email;
        return this;
    }

    public JDAClientBuilder setPassword(String password)
    {
        this.password = password;
        return this;
    }

    @Override
    public JDABuilder setBotToken(String botToken)
    {
        throw new UnsupportedOperationException("Bot-tokens are now allowed for JDA-Client!");
    }

    @Override
    public JDA buildAsync() throws LoginException, IllegalArgumentException
    {
        login();
        return super.buildAsync();
    }

    @Override
    public JDA buildBlocking() throws LoginException, IllegalArgumentException, InterruptedException
    {
        login();
        return super.buildBlocking();
    }

    private void login() throws LoginException
    {
        if (email == null || email.trim().isEmpty() || password == null || password.trim().isEmpty())
            throw new LoginException("Email or Password were null or empty");
        try
        {
            HttpResponse<String> response = Unirest.post(Requester.DISCORD_API_PREFIX + "auth/login")
                    .header("Content-Type", "application/json")
                    .header("user-agent", Requester.USER_AGENT)
                    .body(new JSONObject()
                            .put("email", email)
                            .put("password", password)
                            .toString())
                    .asString();
            if(response.getStatus() < 200 || response.getStatus() > 299)
                throw new LoginException("Email/Password combination was incorrect. server responded with: " + response.getStatus() + " - " + response.getBody());
            token = new JSONObject(response.getBody()).getString("token");
        }
        catch (UnirestException e)
        {
            throw new LoginException("Could not login due to network issue: " + e.getMessage());
        }
    }

    @Override
    public JDAClientBuilder setProxy(String proxyUrl, int proxyPort)
    {
        super.setProxy(proxyUrl, proxyPort);<commit_msg>Fixed issue with login getting called twice when buildBlocking is used<commit_after>/*
 *     Copyright 2016 <NAME> & <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.dv8tion.jda.client;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import net.dv8tion.jda.JDA;
import net.dv8tion.jda.JDABuilder;
import net.dv8tion.jda.hooks.IEventManager;
import net.dv8tion.jda.requests.Requester;
import org.json.JSONObject;

import javax.security.auth.login.LoginException;

public class JDAClientBuilder extends JDABuilder
{
    protected String email = null;
    protected String password = null;

    public JDAClientBuilder()
    {
        Requester.USER_AGENT = "JDA-Client DiscordBot (" + JDAClientInfo.GITHUB + ", " + JDAClientInfo.VERSION + ")";
    }

    public JDAClientBuilder setEmail(String email)
    {
        this.email = email;
        return this;
    }

    public JDAClientBuilder setPassword(String password)
    {
        this.password = password;
        return this;
    }

    @Override
    public JDABuilder setBotToken(String botToken)
    {
        throw new UnsupportedOperationException("Bot-tokens are now allowed for JDA-Client!");
    }

    @Override
    public JDA buildAsync() throws LoginException, IllegalArgumentException
    {
        //call login here to fetch token (buildBlocking calls this so we should be fine)
        login();
        return super.buildAsync();
    }

    @Override
    public JDA buildBlocking() throws LoginException, IllegalArgumentException, InterruptedException
    {
        //will return JDAClient instead of JDA
        return super.buildBlocking();
    }

    private void login() throws LoginException
    {
        if (email == null || email.trim().isEmpty() || password == null || password.trim().isEmpty())
            throw new LoginException("Email or Password were null or empty");
        try
        {
            HttpResponse<String> response = Unirest.post(Requester.DISCORD_API_PREFIX + "auth/login")
                    .header("Content-Type", "application/json")
                    .header("user-agent", Requester.USER_AGENT)
                    .body(new JSONObject()
                            .put("email", email)
                            .put("password", password)
                            .toString())
                    .asString();
            if(response.getStatus() < 200 || response.getStatus() > 299)
                throw new LoginException("Email/Password combination was incorrect. server responded with: " + response.getStatus() + " - " + response.getBody());
            token = new JSONObject(response.getBody()).getString("token");
        }
        catch (UnirestException e)
        {
            throw new LoginException("Could not login due to network issue: " + e.getMessage());
        }
    }

    @Override
    public JDAClientBuilder setProxy(String proxyUrl, int proxyPort)
    {
        super.setProxy(proxyUrl, proxyPort);
{'fraction_non_alphanumeric': 0.09661211599196096, 'fraction_numerical': 0.005455067470571347, 'mean_word_length': 2.621101871101871, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4330508', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1868, 'n_words': 530}