Skip to content

Commit

Permalink
Refactoring examples to use OkHttp
Browse files Browse the repository at this point in the history
  • Loading branch information
sashirestela committed Jan 14, 2025
1 parent fdb7a69 commit bb974c2
Show file tree
Hide file tree
Showing 17 changed files with 406 additions and 58 deletions.
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

0 comments on commit bb974c2

Please sign in to comment.