Skip to content

Instantly share code, notes, and snippets.

@smaspe
Last active December 18, 2015 14:18
Show Gist options
  • Save smaspe/5795917 to your computer and use it in GitHub Desktop.
Save smaspe/5795917 to your computer and use it in GitHub Desktop.
public class DelegatingRequest<T> extends Request<T> {
Listener<T> listener;
NewtorkResponseParser<T> parser;
Map<String, String> headers = new HashMap<String, String>();
byte[] customBody = null;
Map<String, String> params = new HashMap<String, String>();
private String customBodyContentType = null;
public DelegatingRequest(int method, String url, NewtorkResponseParser<T> parser, Listener<T> listener, ErrorListener errorListener) {
super(method, url, errorListener);
this.parser = parser;
this.listener = listener;
}
public void addHeader(String key, String value) {
headers.put(key, value);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers;
}
public void addParam(String key, String value) {
params.put(key, value);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return params;
}
public void setCustomBody(byte[] customBody, String contentType) {
this.customBody = customBody;
this.customBodyContentType = contentType;
}
@Override
public byte[] getBody() throws AuthFailureError {
return customBody != null ? customBody : super.getBody();
}
@Override
public String getBodyContentType() {
return customBodyContentType != null ? customBodyContentType : super.getBodyContentType();
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
return parser.parseResponse(response);
}
@Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
public interface NewtorkResponseParser<T> {
public Response<T> parseResponse(NetworkResponse response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment