Skip to content

Instantly share code, notes, and snippets.

@LOG-TAG
Forked from f2prateek/HttpRequestAdapter.java
Last active August 29, 2015 14:08
Show Gist options
  • Save LOG-TAG/e8086940e68df81e8969 to your computer and use it in GitHub Desktop.
Save LOG-TAG/e8086940e68df81e8969 to your computer and use it in GitHub Desktop.
package com.f2prateek.five00px.data.api.auth;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import oauth.signpost.http.HttpRequest;
import retrofit.client.Header;
import retrofit.client.Request;
public class HttpRequestAdapter implements HttpRequest {
private static final String DEFAULT_CONTENT_TYPE = "application/json";
private Request request;
private String contentType;
public HttpRequestAdapter(Request request) {
this(request, DEFAULT_CONTENT_TYPE);
}
public HttpRequestAdapter(Request request, String contentType) {
this.request = request;
this.contentType = contentType;
}
@Override
public Map<String, String> getAllHeaders() {
HashMap<String, String> headers = new HashMap<String, String>();
for (Header header : request.getHeaders()) {
headers.put(header.getName(), header.getValue());
}
return headers;
}
@Override
public String getContentType() {
return contentType;
}
@Override
public String getHeader(String key) {
for (Header header : request.getHeaders()) {
if (key.equals(header.getName())) {
return header.getValue();
}
}
return null;
}
@Override
public InputStream getMessagePayload() throws IOException {
throw new RuntimeException(new UnsupportedOperationException());
}
@Override
public String getMethod() {
return request.getMethod();
}
@Override
public String getRequestUrl() {
return request.getUrl();
}
@Override
public void setHeader(String key, String value) {
ArrayList<Header> headers = new ArrayList<Header>();
headers.addAll(request.getHeaders());
headers.add(new Header(key, value));
Request copy = new Request(request.getMethod(), request.getUrl(), headers, request.getBody());
request = copy;
}
@Override
public void setRequestUrl(String url) {
throw new RuntimeException(new UnsupportedOperationException());
}
@Override
public Object unwrap() {
return request;
}
}
package com.f2prateek.five00px.data.api.auth;
import oauth.signpost.AbstractOAuthConsumer;
import oauth.signpost.http.HttpRequest;
import retrofit.client.Request;
public class RetrofitHttpOAuthConsumer extends AbstractOAuthConsumer {
private static final long serialVersionUID = 1L;
public RetrofitHttpOAuthConsumer(String consumerKey, String consumerSecret) {
super(consumerKey, consumerSecret);
}
@Override
protected HttpRequest wrap(Object request) {
if (!(request instanceof retrofit.client.Request)) {
throw new IllegalArgumentException("This consumer expects requests of type "
+ retrofit.client.Request.class.getCanonicalName());
}
return new HttpRequestAdapter((Request) request);
}
}
package com.f2prateek.five00px.data.api.auth;
import com.f2prateek.ln.Ln;
import java.io.IOException;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import retrofit.client.OkClient;
import retrofit.client.Request;
import retrofit.client.Response;
/**
* This is a helper class, a {@code retrofit.client.UrlConnectionClient} to use
* when building your {@code retrofit.RestAdapter}.
*/
public class SigningOkClient extends OkClient {
private final OAuthConsumer consumer;
public SigningOkClient(OAuthConsumer consumer) {
this.consumer = consumer;
}
@Override
public Response execute(Request request) throws IOException {
Request requestToSend = request;
try {
HttpRequestAdapter signedAdapter = (HttpRequestAdapter) consumer.sign(request);
requestToSend = (Request) signedAdapter.unwrap();
} catch (OAuthMessageSignerException | OAuthExpectationFailedException | OAuthCommunicationException e) {
Ln.e(e);
}
return super.execute(requestToSend);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment