Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to com.github.scribejava #83

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: java
jdk:
- oraclejdk7
- openjdk6
- openjdk7
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,14 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scribe</groupId>
<artifactId>scribe</artifactId>
<version>1.3.3</version>
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
<version>1.9.31</version>
</dependency>
<dependency>
<groupId>com.github.scribejava</groupId>
<artifactId>scribejava-apis</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/tumblr/jumblr/JumblrClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.tumblr.jumblr.types.Blog;
import com.tumblr.jumblr.types.Post;
import com.tumblr.jumblr.types.User;
import org.scribe.model.Token;
import com.github.scribejava.core.model.Token;

import java.io.IOException;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.ArrayList;
import java.util.List;

import org.scribe.model.Response;
import com.github.scribejava.core.model.Response;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.scribe.model.OAuthRequest;
import com.github.scribejava.core.model.OAuthRequest;

/**
* Convert a OAuthRequest POST into a multi-part OAuthRequest
Expand All @@ -30,7 +30,7 @@ public MultipartConverter(OAuthRequest request, Map<String, ?> bodyMap) throws I
}

public OAuthRequest getRequest() {
OAuthRequest request = new OAuthRequest(originalRequest.getVerb(), originalRequest.getUrl());
OAuthRequest request = new OAuthRequest(originalRequest.getVerb(), originalRequest.getUrl(), originalRequest.getService());
request.addHeader("Authorization", originalRequest.getHeaders().get("Authorization"));
request.addHeader("Content-Type", "multipart/form-data, boundary=" + boundary);
request.addHeader("Content-length", bodyLength.toString());
Expand Down
36 changes: 22 additions & 14 deletions src/main/java/com/tumblr/jumblr/request/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Map;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.TumblrApi;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.apis.TumblrApi;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Token;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuthService;

/**
* Where requests are made from
Expand Down Expand Up @@ -80,7 +80,7 @@ public Token postXAuth(final String email, final String password) {

// Construct an XAuth request
private OAuthRequest constructXAuthPost(String email, String password) {
OAuthRequest request = new OAuthRequest(Verb.POST, xauthEndpoint);
OAuthRequest request = new OAuthRequest(Verb.POST, xauthEndpoint, service);
request.addBodyParameter("x_auth_username", email);
request.addBodyParameter("x_auth_password", password);
request.addBodyParameter("x_auth_mode", "client_auth");
Expand All @@ -95,7 +95,7 @@ public ResponseWrapper get(String path, Map<String, ?> map) {

public OAuthRequest constructGet(String path, Map<String, ?> queryParams) {
String url = "https://" + hostname + "/v2" + path;
OAuthRequest request = new OAuthRequest(Verb.GET, url);
OAuthRequest request = new OAuthRequest(Verb.GET, url, service);
if (queryParams != null) {
for (Map.Entry<String, ?> entry : queryParams.entrySet()) {
request.addQuerystringParameter(entry.getKey(), entry.getValue().toString());
Expand All @@ -108,7 +108,7 @@ public OAuthRequest constructGet(String path, Map<String, ?> queryParams) {

private OAuthRequest constructPost(String path, Map<String, ?> bodyMap) {
String url = "https://" + hostname + "/v2" + path;
OAuthRequest request = new OAuthRequest(Verb.POST, url);
OAuthRequest request = new OAuthRequest(Verb.POST, url, service);

for (Map.Entry<String, ?> entry : bodyMap.entrySet()) {
String key = entry.getKey();
Expand Down Expand Up @@ -136,8 +136,12 @@ public void setToken(final Token token) {
this.token = token;
}

/* package-visible for testing */ ResponseWrapper clear(Response response) {
if (response.getCode() == 200 || response.getCode() == 201) {
private ResponseWrapper clear(Response response) {
return clear(response, response.getCode());
}

/* package-visible for testing */ ResponseWrapper clear(Response response, int code) {
if (code == 200 || code == 201) {
String json = response.getBody();
try {
Gson gson = new GsonBuilder().
Expand Down Expand Up @@ -181,8 +185,12 @@ private Token parseXAuthResponse(final Response response) {
throw new JumblrException(response);
}

/* package-visible for testing */ Token clearXAuth(Response response) {
if (response.getCode() == 200 || response.getCode() == 201) {
private Token clearXAuth(Response response) {
return clearXAuth(response, response.getCode());
}

/* package-visible for testing */ Token clearXAuth(Response response, int code) {
if (code == 200 || code == 201) {
return parseXAuthResponse(response);
} else {
throw new JumblrException(response);
Expand Down
21 changes: 8 additions & 13 deletions src/test/java/com/tumblr/jumblr/request/RequestBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import org.junit.rules.ExpectedException;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Token;

public class RequestBuilderTest {

Expand All @@ -31,11 +31,10 @@ public void setup() {
@Test
public void testClearEmptyJson() {
Response r = mock(Response.class);
when(r.getCode()).thenReturn(200);
when(r.getBody()).thenReturn("");

thrown.expect(JumblrException.class);
ResponseWrapper got = rb.clear(r);
ResponseWrapper got = rb.clear(r, 200);
}

@Test
Expand All @@ -53,43 +52,39 @@ public void testGetParams() {
@Test
public void testXauthForbidden() {
Response r = mock(Response.class);
when(r.getCode()).thenReturn(403);
when(r.getBody()).thenReturn("");

thrown.expect(JumblrException.class);
rb.clearXAuth(r);
rb.clearXAuth(r, 403);
}

@Test
public void testXauthSuccess() {
Response r = mock(Response.class);
when(r.getCode()).thenReturn(200);
when(r.getBody()).thenReturn("oauth_token=valueForToken&oauth_token_secret=valueForSecret");

Token token = rb.clearXAuth(r);
Token token = rb.clearXAuth(r, 200);
assertEquals(token.getToken(), "valueForToken");
assertEquals(token.getSecret(), "valueForSecret");
}

@Test
public void testXauthSuccessWithExtra() {
Response r = mock(Response.class);
when(r.getCode()).thenReturn(201);
when(r.getBody()).thenReturn("oauth_token=valueForToken&oauth_token_secret=valueForSecret&other=paramisokay");

Token token = rb.clearXAuth(r);
Token token = rb.clearXAuth(r, 201);
assertEquals(token.getToken(), "valueForToken");
assertEquals(token.getSecret(), "valueForSecret");
}

@Test
public void testXauthBadResponseGoodCode() {
Response r = mock(Response.class);
when(r.getCode()).thenReturn(200);
when(r.getBody()).thenReturn("");

thrown.expect(JumblrException.class);
rb.clearXAuth(r);
rb.clearXAuth(r, 200);
}

}