Fix usage of Retrofit in LoginActivity - baseUrl wasn't

being set correctly since BaseActivity relies on the
already saved preference

LoginActivity instantiated its own, trimmed down
MastodonAPI instance
This commit is contained in:
Eugen Rochko 2017-03-13 19:13:49 +01:00
parent 60cef27c86
commit b3c7f94951
2 changed files with 14 additions and 5 deletions

View file

@ -39,6 +39,8 @@ import java.util.Map;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class LoginActivity extends BaseActivity { public class LoginActivity extends BaseActivity {
private static String OAUTH_SCOPES = "read write follow"; private static String OAUTH_SCOPES = "read write follow";
@ -94,6 +96,15 @@ public class LoginActivity extends BaseActivity {
startActivity(viewIntent); startActivity(viewIntent);
} }
private MastodonAPI getApiFor(String domain) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://" + domain)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(MastodonAPI.class);
}
/** /**
* Obtain the oauth client credentials for this app. This is only necessary the first time the * Obtain the oauth client credentials for this app. This is only necessary the first time the
* app is run on a given server instance. So, after the first authentication, they are * app is run on a given server instance. So, after the first authentication, they are
@ -134,9 +145,7 @@ public class LoginActivity extends BaseActivity {
} }
}; };
List<String> redirectUris = new ArrayList<>(); getApiFor(domain).authenticateApp(getString(R.string.app_name), getOauthRedirectUri(), OAUTH_SCOPES,
redirectUris.add(getOauthRedirectUri());
mastodonAPI.authenticateApp(getString(R.string.app_name), redirectUris, OAUTH_SCOPES,
getString(R.string.app_website)).enqueue(callback); getString(R.string.app_website)).enqueue(callback);
} }
@ -245,7 +254,7 @@ public class LoginActivity extends BaseActivity {
editText.setError(t.getMessage()); editText.setError(t.getMessage());
} }
}; };
mastodonAPI.fetchOAuthToken(clientId, clientSecret, redirectUri, code, getApiFor(domain).fetchOAuthToken(clientId, clientSecret, redirectUri, code,
"authorization_code").enqueue(callback); "authorization_code").enqueue(callback);
} else if (error != null) { } else if (error != null) {
/* Authorization failed. Put the error response where the user can read it and they /* Authorization failed. Put the error response where the user can read it and they

View file

@ -175,7 +175,7 @@ public interface MastodonAPI {
@POST("api/v1/apps") @POST("api/v1/apps")
Call<AppCredentials> authenticateApp( Call<AppCredentials> authenticateApp(
@Field("client_name") String clientName, @Field("client_name") String clientName,
@Field("redirect_uris[]") List<String> redirectUris, @Field("redirect_uris") String redirectUris,
@Field("scopes") String scopes, @Field("scopes") String scopes,
@Field("website") String website); @Field("website") String website);