Skip to content

Instantly share code, notes, and snippets.

@jpotts18
Last active December 21, 2015 19:39
Show Gist options
  • Save jpotts18/6356172 to your computer and use it in GitHub Desktop.
Save jpotts18/6356172 to your computer and use it in GitHub Desktop.
package com.mydietitian.android;
import com.mydietitian.android.models.*;
import retrofit.Callback;
import retrofit.client.Response;
import retrofit.http.*;
/**
* User: jpotts
* Date: 8/23/13
* Time: 1:30 PM
*/
public interface MDServer {
// Authorization
@FormUrlEncoded
@POST("/oauth/authorize")
void autorize(
@Field("client_id") String clientId,
@Field("client_secret") String clientSecret,
@Field("redirect_uri") String redirectUri,
@Field("grant_type") String grantType,
@Field("username") String username,
@Field("password") String password,
Callback<Token> callback);
@POST("/profile/password/reset")
void resetPassword(Callback<Email> callback);
// Timeline
@GET("/timeline")
void getTimeline(Callback<Timeline> callback);
// Profile
@GET("/profile")
void getProfile(Callback<Profile> callback);
@PUT("/profile")
void putProfile(Callback<Profile> callback);
@POST("/message")
void postMessageAsync(@Body Message message, Callback<Response> cb);
@GET("/items")
void getItems(Callback<Items> callback);
@PUT("/profile")
void postProfileAsync(@Body Profile profile, Callback<Profile> callback);
@GET("/weight")
void getWeightAsync(Callback<Weight> callback);
@POST("/weight")
void postWeightAsync(@Body Weight weight, Callback<Weight> callback);
@GET("/parameters")
void getDailyInputAsync(Callback<DailyInput> callback);
@GET("/activities/:id")
void getPhysicalActivitiesAsync(Callback<PhysicalActivity> callback);
}
package com.mydietitian.android.utils;
import retrofit.RestAdapter;
import retrofit.Server;
import retrofit.client.Client;
import java.util.HashMap;
import java.util.Map;
public class RestClient {
private static RestClient instance;
private RestAdapter mRestAdapter;
// creating a cache for using different clients
private Map<String, Object> mClients = new HashMap<String, Object>();
// singleton pattern
public static RestClient getInstance() {
if (null == instance) {
instance = new RestClient();
}
return instance;
}
// used on Application.OnCreate() to initialize the service
public void configureRestAdapter(String baseServerPath){
mRestAdapter = new RestAdapter.Builder()
.setServer(new Server(baseServerPath)).build();
}
@SuppressWarnings("unchecked")
public <T> T getClient(Class<T> clazz) {
// guard for uninitialized mRestAdapter
if (mRestAdapter == null) {
return null;
}
// initializing generic client
T client = null;
// check service cache for client
if ((client = (T) mClients.get(clazz.getCanonicalName())) != null) {
return client;
}
// create a new client and save it in cache
client = mRestAdapter.create(clazz);
mClients.put(clazz.getCanonicalName(), client);
return client;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment