Start work on integrating Retrofit - GSON, Authorization

This commit is contained in:
Eugen Rochko 2017-03-08 22:34:13 +01:00
parent cff0f35269
commit 348d2c8b4f
6 changed files with 74 additions and 112 deletions

View File

@ -36,4 +36,5 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.mikhaellopez:circularfillableloaders:1.2.0'
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
}

View File

@ -15,7 +15,9 @@
package com.keylesspalace.tusky;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
@ -26,11 +28,22 @@ import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Menu;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/* There isn't presently a way to globally change the theme of a whole application at runtime, just
* individual activities. So, each activity has to set its theme before any views are created. And
* the most expedient way to accomplish this was to put it in a base class and just have every
* activity extend from it. */
public class BaseActivity extends AppCompatActivity {
protected MastodonAPI mastodonAPI;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -59,6 +72,42 @@ public class BaseActivity extends AppCompatActivity {
overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right);
}
protected String getAccessToken() {
SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
return preferences.getString("accessToken", null);
}
protected String getBaseUrl() {
SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
return "https://" + preferences.getString("domain", null);
}
protected void createMastodonAPI() {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Request.Builder builder = originalRequest.newBuilder()
.header("Authorization", String.format("Bearer %s", getAccessToken()));
Request newRequest = builder.build();
return chain.proceed(newRequest);
}
})
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(getBaseUrl())
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
mastodonAPI = retrofit.create(MastodonAPI.class);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
TypedValue value = new TypedValue();

View File

@ -7,6 +7,7 @@ import com.keylesspalace.tusky.entity.StatusContext;
import java.util.List;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.DELETE;
import retrofit2.http.Field;
@ -18,7 +19,7 @@ import retrofit2.http.Part;
import retrofit2.http.Path;
import retrofit2.http.Query;
public interface MastodonService {
public interface MastodonAPI {
@GET("api/v1/timelines/home")
Call<List<Status>> homeTimeline(@Query("max_id") int maxId, @Query("since_id") int sinceId, @Query("limit") int limit);
@GET("api/v1/timelines/public")
@ -29,7 +30,7 @@ public interface MastodonService {
@GET("api/v1/notifications")
Call<List<Notification>> notifications(@Query("max_id") int maxId, @Query("since_id") int sinceId, @Query("limit") int limit);
@POST("api/v1/notifications/clear")
Call clearNotifications();
Call<ResponseBody> clearNotifications();
@GET("api/v1/notifications/{id}")
Call<Notification> notification(@Path("id") int notificationId);
@ -49,7 +50,7 @@ public interface MastodonService {
@GET("api/v1/statuses/{id}/favourited_by")
Call<List<Account>> statusFavouritedBy(@Path("id") int statusId, @Query("max_id") int maxId, @Query("since_id") int sinceId, @Query("limit") int limit);
@DELETE("api/v1/statuses/{id}")
Call deleteStatus(@Path("id") int statusId);
Call<ResponseBody> deleteStatus(@Path("id") int statusId);
@POST("api/v1/statuses/{id}/reblog")
Call<Status> reblogStatus(@Path("id") int statusId);
@POST("api/v1/statuses/{id}/unreblog")

View File

@ -1,49 +1,17 @@
package com.keylesspalace.tusky.entity;
import com.google.gson.annotations.SerializedName;
public class Media {
int id;
String type;
String url;
String preview_url;
String text_url;
public int id;
public int getId() {
return id;
}
public String type;
public void setId(int id) {
this.id = id;
}
public String url;
public String getType() {
return type;
}
@SerializedName("preview_url")
public String previewUrl;
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPreview_url() {
return preview_url;
}
public void setPreview_url(String preview_url) {
this.preview_url = preview_url;
}
public String getText_url() {
return text_url;
}
public void setText_url(String text_url) {
this.text_url = text_url;
}
@SerializedName("text_url")
public String textUrl;
}

View File

@ -1,58 +1,18 @@
package com.keylesspalace.tusky.entity;
import com.google.gson.annotations.SerializedName;
public class Relationship {
public boolean isFollowing() {
return following;
}
public int id;
public void setFollowing(boolean following) {
this.following = following;
}
public boolean following;
public boolean isFollowed_by() {
return followed_by;
}
@SerializedName("followed_by")
public boolean followedBy;
public void setFollowed_by(boolean followed_by) {
this.followed_by = followed_by;
}
public boolean blocking;
public boolean isBlocking() {
return blocking;
}
public boolean muting;
public void setBlocking(boolean blocking) {
this.blocking = blocking;
}
public boolean isMuting() {
return muting;
}
public void setMuting(boolean muting) {
this.muting = muting;
}
public boolean isRequested() {
return requested;
}
public void setRequested(boolean requested) {
this.requested = requested;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
int id;
boolean following;
boolean followed_by;
boolean blocking;
boolean muting;
boolean requested;
public boolean requested;
}

View File

@ -5,23 +5,6 @@ import com.keylesspalace.tusky.Status;
import java.util.List;
public class StatusContext {
List<Status> ancestors;
public List<Status> getAncestors() {
return ancestors;
}
public void setAncestors(List<Status> ancestors) {
this.ancestors = ancestors;
}
public List<Status> getDescendants() {
return descendants;
}
public void setDescendants(List<Status> descendants) {
this.descendants = descendants;
}
List<Status> descendants;
public List<Status> ancestors;
public List<Status> descendants;
}