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

Use OkHttp as an alternative Http client #91

Merged
merged 5 commits into from
Jan 15, 2025
Merged
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
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<!-- Dependencies Versions -->
<lombok.version>1.18.36</lombok.version>
<jackson.version>2.18.2</jackson.version>
<okhttp.version>4.12.0</okhttp.version>
<slf4j.version>2.0.16</slf4j.version>
<junit.version>[5.11.0,6.0.0)</junit.version>
<mockito.version>[5.14.0,6.0.0)</mockito.version>
Expand Down Expand Up @@ -215,6 +216,12 @@
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down Expand Up @@ -266,6 +273,10 @@
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down Expand Up @@ -404,4 +415,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.sashirestela.cleverclient.example;

import io.github.sashirestela.cleverclient.client.HttpClientAdapter;
import io.github.sashirestela.cleverclient.client.JavaHttpClientAdapter;
import io.github.sashirestela.cleverclient.client.OkHttpClientAdapter;
import okhttp3.OkHttpClient;

import java.net.http.HttpClient;

public abstract class AbstractExample {

protected HttpClientAdapter clientAdapter;

protected AbstractExample(String clientAlias) {
switch (clientAlias.toLowerCase()) {
case "javahttp":
clientAdapter = new JavaHttpClientAdapter();
break;
case "okhttp":
clientAdapter = new OkHttpClientAdapter();
break;
default:
clientAdapter = null;
break;
}
}

protected AbstractExample(HttpClient httpClient) {
clientAdapter = new JavaHttpClientAdapter(httpClient);
}

protected AbstractExample(OkHttpClient okHttpClient) {
clientAdapter = new OkHttpClientAdapter(okHttpClient);
}

protected void showTitle(String title) {
final var times = 50;
System.out.println("=".repeat(times));
System.out.println(title);
System.out.println("-".repeat(times));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import io.github.sashirestela.cleverclient.example.jsonplaceholder.Post;
import io.github.sashirestela.cleverclient.example.jsonplaceholder.PostService;

public class BasicExample {
public class BasicExample extends AbstractExample {

public static void main(String[] args) {
final var BASE_URL = "https://jsonplaceholder.typicode.com";
public BasicExample(String clientAlias) {
super(clientAlias);
}

public BasicExample() {
this("javahttp");
}

public void run() {
var cleverClient = CleverClient.builder()
.baseUrl(BASE_URL)
.baseUrl("https://jsonplaceholder.typicode.com")
.clientAdapter(clientAdapter)
.build();
var postService = cleverClient.create(PostService.class);

Expand Down Expand Up @@ -54,11 +61,9 @@ public static void main(String[] args) {
System.out.println("Post was deleted");
}

private static void showTitle(String title) {
final var times = 50;
System.out.println("=".repeat(times));
System.out.println(title);
System.out.println("-".repeat(times));
public static void main(String[] args) {
var example = new BasicExample();
example.run();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.sashirestela.cleverclient.example;

public class BasicExampleOkHttp extends BasicExample {

public BasicExampleOkHttp() {
super("okhttp");
}

public static void main(String[] args) {
var example = new BasicExampleOkHttp();
example.run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
import java.io.IOException;
import java.io.InputStream;

public class FileDownloadExample {

static interface ImageService {

@GET("/150/{id}")
InputStream getImage(@Path("id") String id);
public class FileDownloadExample extends AbstractExample {

public FileDownloadExample(String clientAlias) {
super(clientAlias);
}

public static void main(String[] args) throws IOException {
final var BASE_URL = "https://via.placeholder.com";
public FileDownloadExample() {
this("javahttp");
}

public void run() throws IOException {
var cleverClient = CleverClient.builder()
.baseUrl(BASE_URL)
.baseUrl("https://via.placeholder.com")
.clientAdapter(clientAdapter)
.build();

var imageService = cleverClient.create(ImageService.class);
Expand All @@ -31,4 +31,16 @@ public static void main(String[] args) throws IOException {
file.close();
}

static interface ImageService {

@GET("/150/{id}")
InputStream getImage(@Path("id") String id);

}

public static void main(String[] args) throws IOException {
var example = new FileDownloadExample();
example.run();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.sashirestela.cleverclient.example;

import java.io.IOException;

public class FileDownloadExampleOkHttp extends FileDownloadExample {

public FileDownloadExampleOkHttp() {
super("okhttp");
}

public static void main(String[] args) throws IOException {
var example = new FileDownloadExampleOkHttp();
example.run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,31 @@
import io.github.sashirestela.cleverclient.annotation.GET;
import io.github.sashirestela.cleverclient.annotation.Header;

public class HeaderExample {
public class HeaderExample extends AbstractExample {

public HeaderExample(String clientAlias) {
super(clientAlias);
}

public HeaderExample() {
this("javahttp");
}

public void run() {
var cleverClient = CleverClient.builder()
.baseUrl("https://httpbin.org")
.clientAdapter(clientAdapter)
.build();

showTitle("First Group of Headers");
var classHeaderService = cleverClient.create(ClassHeaderService.class);
System.out.println(classHeaderService.getFullHeaders());
System.out.println(classHeaderService.getClassHeaders());

showTitle("Second Group of Headers");
var methodHeaderService = cleverClient.create(MethodHeaderService.class);
System.out.println(methodHeaderService.getHeaders());
}

@Header(name = "First-Header", value = "firstValue")
@Header(name = "Second-Header", value = "secondValue")
Expand All @@ -29,18 +53,8 @@ static interface MethodHeaderService {
}

public static void main(String[] args) {
final var BASE_URL = "https://httpbin.org";

var cleverClient = CleverClient.builder()
.baseUrl(BASE_URL)
.build();

var classHeaderService = cleverClient.create(ClassHeaderService.class);
System.out.println(classHeaderService.getFullHeaders());
System.out.println(classHeaderService.getClassHeaders());

var methodHeaderService = cleverClient.create(MethodHeaderService.class);
System.out.println(methodHeaderService.getHeaders());
var example = new HeaderExample();
example.run();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.sashirestela.cleverclient.example;

public class HeaderExampleOkHttp extends HeaderExample {

public HeaderExampleOkHttp() {
super("okhttp");
}

public static void main(String[] args) {
var example = new HeaderExampleOkHttp();
example.run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import io.github.sashirestela.cleverclient.example.jsonplaceholder.AlbumService;
import io.github.sashirestela.cleverclient.example.jsonplaceholder.PostService;

public class MultiServiceExample {
public class MultiServiceExample extends AbstractExample {

public static void main(String[] args) {
final var BASE_URL = "https://jsonplaceholder.typicode.com";
public MultiServiceExample(String clientAlias) {
super(clientAlias);
}

public MultiServiceExample() {
this("javahttp");
}

public void run() {
var cleverClient = CleverClient.builder()
.baseUrl(BASE_URL)
.baseUrl("https://jsonplaceholder.typicode.com")
.clientAdapter(clientAdapter)
.build();
var postService = cleverClient.create(PostService.class);
var albumService = cleverClient.create(AlbumService.class);
Expand All @@ -32,11 +39,9 @@ public static void main(String[] args) {
System.out.println(post);
}

private static void showTitle(String title) {
final var times = 50;
System.out.println("=".repeat(times));
System.out.println(title);
System.out.println("-".repeat(times));
public static void main(String[] args) {
var example = new MultiServiceExample();
example.run();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.sashirestela.cleverclient.example;

public class MultiServiceExampleOkHttp extends MultiServiceExample {

public MultiServiceExampleOkHttp() {
super("okhttp");
}

public static void main(String[] args) {
var example = new MultiServiceExampleOkHttp();
example.run();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.github.sashirestela.cleverclient.example;

import io.github.sashirestela.cleverclient.CleverClient;
import io.github.sashirestela.cleverclient.example.openai.AudioService;
import io.github.sashirestela.cleverclient.example.openai.TranscriptionRequest;
import io.github.sashirestela.cleverclient.example.openai.TranscriptionRequest.AudioResponseFormat;
import io.github.sashirestela.cleverclient.example.openai.TranscriptionRequest.TimestampGranularity;

import java.nio.file.Paths;

public class MultipartExample extends AbstractExample {

public MultipartExample(String clientAlias) {
super(clientAlias);
}

public MultipartExample() {
this("javahttp");
}

public void run() {
var cleverClient = CleverClient.builder()
.baseUrl("https://api.openai.com")
.clientAdapter(clientAdapter)
.header("Authorization", "Bearer " + System.getenv("OPENAI_API_KEY"))
.endOfStream("[DONE]")
.build();
var audioService = cleverClient.create(AudioService.class);
var audioRequest = TranscriptionRequest.builder()
.file(Paths.get("src/test/resources/hello_audio.mp3"))
.model("whisper-1")
.responseFormat(AudioResponseFormat.VERBOSE_JSON)
.temperature(0.2)
.timestampGranularity(TimestampGranularity.WORD)
.timestampGranularity(TimestampGranularity.SEGMENT)
.build();
var audioResponse = audioService.transcribe(audioRequest);
System.out.println(audioResponse.getText());
}

public static void main(String[] args) {
var example = new MultipartExample();
example.run();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.sashirestela.cleverclient.example;

public class MultipartExampleOkHttp extends MultipartExample {

public MultipartExampleOkHttp() {
super("okhttp");
}

public static void main(String[] args) {
var example = new MultipartExampleOkHttp();
example.run();
}

}
Loading
Loading