This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from palantir/develop
1.0.0-rc
- Loading branch information
Showing
41 changed files
with
870 additions
and
277 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,26 @@ | ||
/* | ||
* Copyright 2017 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
apply from: "${rootDir}/gradle/java.gradle" | ||
apply from: "${rootDir}/gradle/junit.gradle" | ||
|
||
dependencies { | ||
compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}" | ||
compile "com.fasterxml.jackson.module:jackson-module-afterburner:${jacksonVersion}" | ||
compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:${jacksonVersion}" | ||
compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${jacksonVersion}" | ||
compile "com.fasterxml.jackson.datatype:jackson-datatype-guava:${jacksonVersion}" | ||
} |
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
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 @@ | ||
/* | ||
* Copyright 2017 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
apply from: "${rootDir}/gradle/java.gradle" | ||
apply from: "${rootDir}/gradle/junit.gradle" | ||
|
||
dependencies { | ||
compile project(":extras:jackson-support") | ||
compile "com.google.guava:guava:${guavaVersion}" | ||
compile "com.squareup.retrofit2:retrofit:${retrofitVersion}" | ||
compile "com.squareup.retrofit2:converter-jackson:${retrofitVersion}" | ||
} |
64 changes: 64 additions & 0 deletions
64
extras/slack-clients/src/main/java/com/palantir/roboslack/clients/SlackClients.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,64 @@ | ||
/* | ||
* Copyright 2017 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.roboslack.clients; | ||
|
||
import com.palantir.roboslack.jackson.ObjectMappers; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Stream; | ||
import okhttp3.ConnectionPool; | ||
import okhttp3.OkHttpClient; | ||
import retrofit2.Converter; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.jackson.JacksonConverterFactory; | ||
|
||
/** | ||
* Utility class for generating service clients for RPC calls to Slack (intended for internal use only). | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public final class SlackClients { | ||
|
||
private static final String DEFAULT_USER_AGENT = "RoboSlack/1.0.0"; | ||
|
||
private SlackClients() {} | ||
|
||
private static String addTrailingSlash(String uri) { | ||
return uri.charAt(uri.length() - 1) == '/' ? uri : uri + "/"; | ||
} | ||
|
||
private static OkHttpClient createOkHttpClient(String userAgent) { | ||
return new OkHttpClient.Builder() | ||
.addInterceptor(UserAgentInterceptor.of(userAgent)) | ||
.connectionPool(new ConnectionPool(100, 10, TimeUnit.MINUTES)) | ||
.build(); | ||
} | ||
|
||
public static <T> T create(Class<T> clazz, String userAgent, String uri, | ||
Converter.Factory... specialPurposeConverters) { | ||
Retrofit.Builder retrofit = new Retrofit.Builder() | ||
.baseUrl(addTrailingSlash(uri)) | ||
.client(createOkHttpClient(userAgent)); | ||
Stream.of(specialPurposeConverters).forEach(retrofit::addConverterFactory); | ||
retrofit.addConverterFactory(JacksonConverterFactory.create(ObjectMappers.newObjectMapper())); | ||
return retrofit.build().create(clazz); | ||
} | ||
|
||
public static <T> T create(Class<T> clazz, String uri, Converter.Factory... specialPurposeConverters) { | ||
return create(clazz, DEFAULT_USER_AGENT, uri, specialPurposeConverters); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
extras/slack-clients/src/main/java/com/palantir/roboslack/clients/UserAgentInterceptor.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,53 @@ | ||
/* | ||
* Copyright 2017 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.roboslack.clients; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
import java.io.IOException; | ||
import java.util.regex.Pattern; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Response; | ||
|
||
/** | ||
* Intercepts requests, then injects and validates the user agent string. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public final class UserAgentInterceptor implements Interceptor { | ||
|
||
private static final Pattern VALID_USER_AGENT = Pattern.compile("[A-Za-z0-9()\\-#;/.,_\\s]+"); | ||
private final String userAgent; | ||
|
||
private UserAgentInterceptor(String userAgent) { | ||
checkArgument(VALID_USER_AGENT.matcher(userAgent).matches(), | ||
"User Agent must match pattern '%s': %s", VALID_USER_AGENT, userAgent); | ||
this.userAgent = userAgent; | ||
} | ||
|
||
public static UserAgentInterceptor of(String userAgent) { | ||
return new UserAgentInterceptor(userAgent); | ||
} | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
return chain.proceed(chain.request().newBuilder() | ||
.header("User-Agent", userAgent) | ||
.build()); | ||
} | ||
|
||
} |
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
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
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
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
Oops, something went wrong.