Last active
May 6, 2023 09:46
-
-
Save magdamiu/a112e58db50c4abd28497f275cc7fc47 to your computer and use it in GitHub Desktop.
Repository for the users
This file contains 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
import java.util.List; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
public class UsersRepository { | |
protected static final String BASE_GITHUB_URL = "https://api.github.com/"; | |
private static UsersRepository usersRepository; | |
private GithubApi githubApi; | |
private UsersRepository(GithubApi api) { | |
this.githubApi = api; | |
} | |
public static UsersRepository getInstance() { | |
if (usersRepository == null) { | |
Retrofit retrofit = new Retrofit.Builder() | |
.baseUrl(BASE_GITHUB_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); | |
usersRepository = new UsersRepository(retrofit.create(GithubApi.class)); | |
} | |
return usersRepository; | |
} | |
public void getUsers(final OnGetUsersCallback callback) { | |
githubApi.getAllUsers() | |
.enqueue(new Callback<List<User>>() { | |
@Override | |
public void onResponse(Call<List<User>> call, Response<List<User>> response) { | |
if (response.isSuccessful()) { | |
List<User> users = response.body(); | |
if (users != null) { | |
callback.onSuccess(users); | |
} else { | |
callback.onError(); | |
} | |
} else { | |
callback.onError(); | |
} | |
} | |
@Override | |
public void onFailure(Call<List<User>> call, Throwable t) { | |
callback.onError(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment