Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
sheetsu (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Jun 8, 2016
1 parent 40ff7c3 commit 7b2d5b7
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 38 deletions.
19 changes: 19 additions & 0 deletions commands/sheetsuapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env okapi -m

var sheet = arguments[0];

var request = requestBuilder
.url("https://sheetsu.com/apis/v1.0/" + sheet)
.build();

if (request.body()) {
requestBuilder = request.newBuilder();

var BodyWrapper = Java.type("com.baulsupp.oksocial.util.WrappedRequestBody");
var body = new BodyWrapper(request.body(), "application/json");

request = requestBuilder.method(request.method(), body).build();
}

request

12 changes: 6 additions & 6 deletions src/main/java/com/baulsupp/oksocial/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -522,22 +522,22 @@ private RequestBody getRequestBody() {
}

public Request.Builder createRequestBuilder() {
Request.Builder request = new Request.Builder();
Request.Builder requestBuilder = new Request.Builder();

request.method(getRequestMethod(), getRequestBody());
requestBuilder.method(getRequestMethod(), getRequestBody());

if (headers != null) {
for (String header : headers) {
String[] parts = header.split(":", 2);
request.header(parts[0], parts[1]);
requestBuilder.header(parts[0], parts[1]);
}
}
if (referer != null) {
request.header("Referer", referer);
requestBuilder.header("Referer", referer);
}
request.header("User-Agent", userAgent);
requestBuilder.header("User-Agent", userAgent);

return request;
return requestBuilder;
}

private static SSLSocketFactory createSslSocketFactory(KeyManager[] keyManagers,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.baulsupp.oksocial.authenticator;

public class BasicCredentials {
public String user;
public String password;

public BasicCredentials() {
}

public BasicCredentials(String user, String password) {
this.user = user;
this.password = password;
}

@Override
public String toString() {
return "BasicCredentials{"
+ "user='" + user + '\''
+ "password='" + password + '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.baulsupp.oksocial.services.sheetsu;

import com.baulsupp.oksocial.authenticator.AuthInterceptor;
import com.baulsupp.oksocial.authenticator.BasicCredentials;
import com.baulsupp.oksocial.credentials.CredentialsStore;
import com.baulsupp.oksocial.secrets.Secrets;
import java.io.IOException;
import java.util.Optional;
import okhttp3.Credentials;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SheetsuAuthInterceptor implements AuthInterceptor<BasicCredentials> {
private final CredentialsStore<BasicCredentials> credentialsStore =
CredentialsStore.create(new SheetsuServiceDefinition());
public static final String NAME = "sheetsu";

@Override public String name() {
return NAME;
}

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();

Optional<BasicCredentials> credentials = readCredentials();
if (credentials.isPresent()) {
BasicCredentials token = readCredentials().get();

request =
request.newBuilder()
.addHeader("Authorization", Credentials.basic(token.user, token.password))
.build();
}

return chain.proceed(request);
}

@Override
public CredentialsStore<BasicCredentials> credentialsStore() {
return credentialsStore;
}

public boolean supportsUrl(HttpUrl url) {
String host = url.host();

return SheetsuUtil.API_HOSTS.contains(host);
}

@Override
public void authorize(OkHttpClient client) {
String user =
Secrets.prompt("Sheetsu API Key", "sheetsu.apiKey", "", false);
String password =
Secrets.prompt("Sheetsu API Password", "sheetsu.apiSecret", "", true);

BasicCredentials newCredentials = new BasicCredentials(user, password);

credentialsStore.storeCredentials(newCredentials);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baulsupp.oksocial.services.sheetsu;

import com.baulsupp.oksocial.authenticator.BasicCredentials;
import com.baulsupp.oksocial.credentials.ServiceDefinition;

public class SheetsuServiceDefinition implements ServiceDefinition<BasicCredentials> {
@Override
public String apiHost() {
return "sheetsu.com";
}

@Override
public String serviceName() {
return "Sheetsu API";
}

public BasicCredentials parseCredentialsString(String s) {
String[] parts = s.split(":", 2);
return new BasicCredentials(parts[0], parts[1]);
}

public String formatCredentialsString(BasicCredentials credentials) {
return credentials.user + ":" + credentials.password;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baulsupp.oksocial.services.sheetsu;

import com.google.common.collect.Sets;
import java.util.Collections;
import java.util.Set;

public class SheetsuUtil {
private SheetsuUtil() {
}

public static final Set<String> API_HOSTS =
Collections.unmodifiableSet(Sets.newHashSet(
"sheetsu.com")
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.baulsupp.oksocial.services.uber;

import com.baulsupp.oksocial.authenticator.AuthInterceptor;
import com.baulsupp.oksocial.authenticator.oauth2.Oauth2Token;
import com.baulsupp.oksocial.credentials.CredentialsStore;
import com.baulsupp.oksocial.secrets.Secrets;
import java.io.IOException;
Expand All @@ -11,8 +12,8 @@
import okhttp3.Request;
import okhttp3.Response;

public class UberAuthInterceptor implements AuthInterceptor<UberServerCredentials> {
private final CredentialsStore<UberServerCredentials> credentialsStore =
public class UberAuthInterceptor implements AuthInterceptor<Oauth2Token> {
private final CredentialsStore<Oauth2Token> credentialsStore =
CredentialsStore.create(new UberServiceDefinition());
public static final String NAME = "uber";

Expand All @@ -24,9 +25,9 @@ public class UberAuthInterceptor implements AuthInterceptor<UberServerCredential
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();

Optional<UberServerCredentials> credentials = readCredentials();
Optional<Oauth2Token> credentials = readCredentials();
if (credentials.isPresent()) {
String token = readCredentials().get().serverToken;
String token = readCredentials().get().accessToken;

request =
request.newBuilder().addHeader("Authorization", "Token " + token).build();
Expand All @@ -36,7 +37,7 @@ public Response intercept(Interceptor.Chain chain) throws IOException {
}

@Override
public CredentialsStore<UberServerCredentials> credentialsStore() {
public CredentialsStore<Oauth2Token> credentialsStore() {
return credentialsStore;
}

Expand All @@ -51,7 +52,7 @@ public void authorize(OkHttpClient client) {
String serverToken =
Secrets.prompt("Uber Server Token", "uber.serverToken", "", true);

UberServerCredentials newCredentials = new UberServerCredentials(serverToken);
Oauth2Token newCredentials = new Oauth2Token(serverToken);

credentialsStore.storeCredentials(newCredentials);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.baulsupp.oksocial.services.uber;

import com.baulsupp.oksocial.authenticator.oauth2.Oauth2Token;
import com.baulsupp.oksocial.credentials.ServiceDefinition;

public class UberServiceDefinition implements ServiceDefinition<UberServerCredentials> {
public class UberServiceDefinition implements ServiceDefinition<Oauth2Token> {
@Override
public String apiHost() {
return "api.uber.com";
Expand All @@ -13,11 +14,11 @@ public String serviceName() {
return "Uber API";
}

public UberServerCredentials parseCredentialsString(String s) {
return new UberServerCredentials(s);
public Oauth2Token parseCredentialsString(String s) {
return new Oauth2Token(s);
}

public String formatCredentialsString(UberServerCredentials credentials) {
return credentials.serverToken;
public String formatCredentialsString(Oauth2Token credentials) {
return credentials.accessToken;
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/baulsupp/oksocial/util/WrappedRequestBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baulsupp.oksocial.util;

import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;

public class WrappedRequestBody extends RequestBody {
private final String contentType;
private final RequestBody body;

public WrappedRequestBody(RequestBody body, String contentType) {
this.body = body;
this.contentType = contentType;
}

@Override public MediaType contentType() {
return MediaType.parse(contentType);
}

@Override public void writeTo(BufferedSink bufferedSink) throws IOException {
body.writeTo(bufferedSink);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ com.baulsupp.oksocial.services.foursquare.FourSquareAuthInterceptor
com.baulsupp.oksocial.services.google.GoogleAuthInterceptor
com.baulsupp.oksocial.services.imgur.ImgurAuthInterceptor
com.baulsupp.oksocial.services.lyft.LyftAuthInterceptor
com.baulsupp.oksocial.services.sheetsu.SheetsuAuthInterceptor
com.baulsupp.oksocial.services.squareup.SquareUpAuthInterceptor
com.baulsupp.oksocial.services.stackexchange.StackExchangeAuthInterceptor
com.baulsupp.oksocial.services.twitter.TwitterAuthInterceptor
Expand Down

0 comments on commit 7b2d5b7

Please sign in to comment.