Last active
August 6, 2019 00:45
-
-
Save mageddo/a979e9538de24dd9be1d652616c715f3 to your computer and use it in GitHub Desktop.
OkHTTP Client Example
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
import com.mageddo.portainer.client.apiclient.PortainerAuthenticationFilter; | |
import com.mageddo.portainer.client.apiclient.PortainerStackApiClient; | |
import okhttp3.ConnectionPool; | |
import okhttp3.HttpUrl; | |
import okhttp3.OkHttpClient; | |
import java.util.concurrent.TimeUnit; | |
public class Main { | |
public static void main(String[] args) { | |
final HttpUrl baseUrl = HttpUrl.parse("http://acme.com"); | |
var apiClient = new PortainerStackApiClient( | |
createClient() | |
.newBuilder() | |
.authenticator(new PortainerAuthenticationFilter()) | |
.build(), | |
baseUrl | |
); | |
apiClient.createStack(...); | |
} | |
public static OkHttpClient createClient() { | |
return new OkHttpClient.Builder() | |
.connectTimeout(5, TimeUnit.SECONDS) | |
.readTimeout(30, TimeUnit.SECONDS) | |
.callTimeout(36, TimeUnit.SECONDS) | |
.connectionPool(new ConnectionPool(1, 1, TimeUnit.MINUTES)) | |
.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
import okhttp3.Authenticator; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import okhttp3.Route; | |
public class PortainerAuthenticationFilter implements Authenticator { | |
@Override | |
public Request authenticate(Route route, Response response) { | |
return response | |
.request() | |
.newBuilder() | |
.addHeader("Authorization", "Bearer someToken") | |
.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
public class PortainerStackApiClient { | |
private final OkHttpClient okHttpClient; | |
private final HttpUrl baseUrl; | |
public PortainerStackApiClient(OkHttpClient okHttpClient, HttpUrl baseUrl) { | |
this.okHttpClient = okHttpClient; | |
this.baseUrl = baseUrl; | |
} | |
public void createStack(StackCreateReqV1 createReqV1){ | |
Call call = okHttpClient | |
.newCall( | |
new Request.Builder() | |
.url( | |
baseUrl | |
.newBuilder() | |
.addEncodedPathSegments("api/stacks") | |
.setQueryParameter("type", "1") | |
.setQueryParameter("method", "string") | |
.setQueryParameter("endpointId", "1") | |
.build() | |
) | |
.post(RequestBody.create( | |
MediaType.get("application/json"), JsonUtils.writeValueAsString(createReqV1) | |
)) | |
.build() | |
); | |
try(Response res = call.execute()){ | |
Validate.isTrue(res.isSuccessful(), res.body().string()); | |
} catch (IOException e) { | |
throw new UncheckedIOException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment