This repository has been archived by the owner on Apr 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
188 additions
and
38 deletions.
There are no files selected for viewing
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
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 | ||
|
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
22 changes: 22 additions & 0 deletions
22
src/main/java/com/baulsupp/oksocial/authenticator/BasicCredentials.java
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
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 + '\'' | ||
+ '}'; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuAuthInterceptor.java
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
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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuServiceDefinition.java
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
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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuUtil.java
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
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") | ||
); | ||
} |
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
21 changes: 0 additions & 21 deletions
21
src/main/java/com/baulsupp/oksocial/services/uber/UberServerCredentials.java
This file was deleted.
Oops, something went wrong.
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
24 changes: 24 additions & 0 deletions
24
src/main/java/com/baulsupp/oksocial/util/WrappedRequestBody.java
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
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); | ||
} | ||
} |
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