Created
April 6, 2019 18:17
-
-
Save libindev/2d1745c86b4fb99b3c33aee415265021 to your computer and use it in GitHub Desktop.
Retrofit service with oAuth 2.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AuthenticationInterceptor implements Interceptor { | |
private String authToken; | |
public AuthenticationInterceptor(String token) { | |
this.authToken = token; | |
} | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
Request original = chain.request(); | |
Request.Builder builder = original.newBuilder() | |
.header("Authorization", authToken); | |
Request request = builder.build(); | |
return chain.proceed(request); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ErrorUtils { | |
public static APIError parseError(Response<?> response) { | |
Converter<ResponseBody, APIError> converter = | |
ServiceGenerator.retrofit() | |
.responseBodyConverter(APIError.class, new Annotation[0]); | |
APIError error; | |
try { | |
error = converter.convert(response.errorBody()); | |
} catch (IOException e) { | |
return new APIError(); | |
} | |
return error; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface LoginService { | |
@FormUrlEncoded | |
@POST("register") | |
Call<String> register(@Field("name") String name, @Field("email") String email, @Field("password") String password, @Field("c_password") String c_password); | |
@POST("login") | |
Call<String> login(@Body String login ); | |
@POST("login") | |
Call<String> login(@Body String login ,@Query("id") String id); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LoginService service = ServiceGenerator.createService(LoginService.class); | |
Call<String> login = service.login(json.toString(),android_id); | |
login.enqueue(new Callback<String>() { | |
@Override | |
public void onResponse(Call<String> call, Response<String> response) { | |
if (response.isSuccessful()) { | |
Log.d(TAG, response.body()); | |
} else { | |
APIError error = ErrorUtils.parseError(response); | |
int code = response.code(); | |
if (code == 401) { | |
progressBar.setVisibility(View.GONE); | |
Toast.makeText(LoginActivity.this, "UserName or Password Incorrect", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
@Override | |
public void onFailure(Call<String> call, Throwable t) { | |
Toast.makeText(LoginActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ServiceGenerator { | |
public static final String API_BASE_URL =Urls.BASE_URL; | |
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); | |
private static OkHttpClient okHttpClient = new OkHttpClient().newBuilder() | |
.connectTimeout(50, TimeUnit.SECONDS) | |
.readTimeout(50, TimeUnit.SECONDS) | |
.writeTimeout(50, TimeUnit.SECONDS) | |
.build(); | |
private static Retrofit.Builder builder = | |
new Retrofit.Builder() | |
.baseUrl(API_BASE_URL) | |
.client(okHttpClient) | |
.addConverterFactory(GsonConverterFactory.create()); | |
private static Retrofit retrofit = builder.build(); | |
public static <S> S createService(Class<S> serviceClass) { | |
return createService(serviceClass, null); | |
} | |
public static <S> S createService( | |
Class<S> serviceClass, String clientId, String clientSecret) { | |
if (!TextUtils.isEmpty(clientId) | |
&& !TextUtils.isEmpty(clientSecret)) { | |
String authToken = Credentials.basic(clientId, clientSecret); | |
return createService(serviceClass, authToken); | |
} | |
return createService(serviceClass, null, null); | |
} | |
public static <S> S createService(Class<S> serviceClass, final String authToken) { | |
if (!TextUtils.isEmpty(authToken)) { | |
AuthenticationInterceptor interceptor = | |
new AuthenticationInterceptor(authToken); | |
if (!httpClient.interceptors().contains(interceptor)) { | |
httpClient.addInterceptor(interceptor); | |
builder.client(httpClient.build()); | |
retrofit = builder.build(); | |
} | |
} | |
return retrofit.create(serviceClass); | |
} | |
public static Retrofit retrofit() { | |
return retrofit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment