// Path: intent-factory/src/main/java/com/jakewharton/u2020/intentfactory/IntentFactory.java
// public interface IntentFactory {
//   Intent createUrlIntent(String url);
// 
//   IntentFactory REAL = url -> {
//     Intent intent = new Intent(Intent.ACTION_VIEW);
//     intent.setData(Uri.parse(url));
//     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 
//     return intent;
//   };
// }


import android.content.Intent;
import android.net.Uri;
import com.f2prateek.rx.preferences.Preference;
import com.jakewharton.u2020.intentfactory.IntentFactory;
import com.squareup.moshi.Moshi;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;
    HttpUrl authorizeUrl = HttpUrl.parse("https://github.com/login/oauth/authorize") //
        .newBuilder() //
        .addQueryParameter("client_id", CLIENT_ID) //
        .build();

    return intentFactory.createUrlIntent(authorizeUrl.toString());
  }

  public void handleResult(Uri data) {
    if (data == null) return;

    String code = data.getQueryParameter("code");
    if (code == null) return;

    try {
      // Trade our code for an access token.
      Request request = new Request.Builder() //
          .url("https://github.com/login/oauth/access_token") //
          .header("Accept", "application/json") //
          .post(new FormBody.Builder() //
              .add("client_id", CLIENT_ID) //
              .add("client_secret", CLIENT_SECRET) //
              .add("code", code) //
              .build()) //
          .build();

      Response response = client.newCall(request).execute();
      if (response.isSuccessful()) {
        AccessTokenResponse accessTokenResponse =
            moshi.adapter(AccessTokenResponse.class).fromJson(response.body().string());
// Path: intent-factory/src/main/java/com/jakewharton/u2020/intentfactory/IntentFactory.java
// public interface IntentFactory {
//   Intent createUrlIntent(String url);
// 
//   IntentFactory REAL = url -> {
//     Intent intent = new Intent(Intent.ACTION_VIEW);
//     intent.setData(Uri.parse(url));
//     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 
//     return intent;
//   };
// }


// Path: app/src/main/java/com/jakewharton/u2020/data/api/oauth/OauthManager.java
import android.content.Intent;
import android.net.Uri;
import com.f2prateek.rx.preferences.Preference;
import com.jakewharton.u2020.intentfactory.IntentFactory;
import com.squareup.moshi.Moshi;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;

    HttpUrl authorizeUrl = HttpUrl.parse("https://github.com/login/oauth/authorize") //
        .newBuilder() //
        .addQueryParameter("client_id", CLIENT_ID) //
        .build();

    return intentFactory.createUrlIntent(authorizeUrl.toString());
  }

  public void handleResult(Uri data) {
    if (data == null) return;

    String code = data.getQueryParameter("code");
    if (code == null) return;

    try {
      // Trade our code for an access token.
      Request request = new Request.Builder() //
          .url("https://github.com/login/oauth/access_token") //
          .header("Accept", "application/json") //
          .post(new FormBody.Builder() //
              .add("client_id", CLIENT_ID) //
              .add("client_secret", CLIENT_SECRET) //
              .add("code", code) //
              .build()) //
          .build();

      Response response = client.newCall(request).execute();
      if (response.isSuccessful()) {
        AccessTokenResponse accessTokenResponse =
            moshi.adapter(AccessTokenResponse.class).fromJson(response.body().string());
        if (accessTokenResponse != null && accessTokenResponse.access_token != null) {