Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package retrofit.labs;
import com.squareup.okhttp.RequestBody;
import java.util.Map;
public abstract class FormUrlEncodedRequest<T> extends Request<T> {
public FormUrlEncodedRequest(Listener<T> listener) {
super(listener);
}
public abstract Map<String, RequestBody> fields();
}
package com.example.retrofit.labs;
import com.example.retrofit.Contributor;
import java.util.List;
import retrofit.labs.SimpleRequest;
public class GitHubRequest extends SimpleRequest<List<Contributor>> {
private final String owner;
private final String repo;
public GitHubRequest(String owner, String repo, Listener<List<Contributor>> listener) {
super(listener);
this.owner = owner;
this.repo = repo;
}
@Override public String path() {
return String.format("/repos/%s/%s/contributors", owner, repo);
}
@Override public Method method() {
return Method.GET;
}
}
package com.example.retrofit.labs;
import com.example.retrofit.Contributor;
import java.util.List;
import retrofit.RestAdapter;
import retrofit.labs.Request;
public class GitHubRequestTest {
private static final String API_URL = "https://api.github.com";
public static void main(String... args) {
// Create a very simple REST adapter which points the GitHub API endpoint.
RestAdapter restAdapter = new RestAdapter.Builder()
.endpoint(API_URL)
.build();
// Create an instance of our GitHub API interface.
Request.Listener<List<Contributor>> listener = new Request.Listener<List<Contributor>>() {
@Override public void onSuccess(List<Contributor> contributors) {
// Fetch and print a list of the contributors to this library.
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
}
@Override public void onError(Exception e) {
}
};
GitHubRequest request = new GitHubRequest("square", "retrofit", listener);
restAdapter.create(request);
}
}
package retrofit.labs;
import com.squareup.okhttp.RequestBody;
import java.util.Map;
public abstract class MultipartRequest<T> extends Request<T> {
public MultipartRequest(Listener<T> listener) {
super(listener);
}
public abstract Map<String, RequestBody> parts();
}
package retrofit.labs;
import com.squareup.okhttp.RequestBody;
import java.util.Collections;
import java.util.Map;
public abstract class Request<T> {
public enum Method {
GET, POST, PUT, PATCH, HEAD
}
public interface Listener<T> {
void onSuccess(T response);
void onError(Exception e);
}
private Listener<T> listener;
public Request(Listener<T> listener) {
this.listener = listener;
}
public abstract String path();
public abstract Request.Method method();
public RequestBody body() {
return null;
}
public Map<String, String> queryParams() {
return Collections.emptyMap();
}
public Map<String, String> headers() {
return Collections.emptyMap();
}
}
package retrofit.labs;
public abstract class SimpleRequest<T> extends Request<T> {
public SimpleRequest(Listener<T> listener) {
super(listener);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.