Last active
December 18, 2015 14:18
-
-
Save smaspe/5795917 to your computer and use it in GitHub Desktop.
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
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