Skip to content

Commit

Permalink
Full code reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashir Estela committed Mar 19, 2024
1 parent c72c2aa commit 30e76fb
Show file tree
Hide file tree
Showing 72 changed files with 416 additions and 328 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ private static void showTitle(String title) {
System.out.println(title);
System.out.println("-".repeat(times));
}
}

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

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import io.github.sashirestela.cleverclient.CleverClient;
import io.github.sashirestela.cleverclient.annotation.GET;
import io.github.sashirestela.cleverclient.annotation.Path;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileDownloadExample {

static interface ImageService {

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

}

public static void main(String[] args) throws IOException {
Expand All @@ -29,4 +30,5 @@ public static void main(String[] args) throws IOException {
file.write(binaryData.readAllBytes());
file.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ static interface ClassHeaderService {

@GET("/headers")
String getClassHeaders();

}

static interface MethodHeaderService {
Expand All @@ -24,6 +25,7 @@ static interface MethodHeaderService {
@Header(name = "Fourth-Header", value = "fourthValue")
@Header(name = "Fith-Header", value = "fithValue")
String getHeaders();

}

public static void main(String[] args) {
Expand All @@ -40,4 +42,5 @@ public static void main(String[] args) {
var methodHeaderService = cleverClient.create(MethodHeaderService.class);
System.out.println(methodHeaderService.getHeaders());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ private static void showTitle(String title) {
System.out.println(title);
System.out.println("-".repeat(times));
}
}

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

import java.util.Arrays;

import io.github.sashirestela.cleverclient.CleverClient;
import io.github.sashirestela.cleverclient.example.openai.ChatRequest;
import io.github.sashirestela.cleverclient.example.openai.ChatResponse;
import io.github.sashirestela.cleverclient.example.openai.ChatService;
import io.github.sashirestela.cleverclient.example.openai.Message;

import java.util.Arrays;

/**
* Before running this example you must have an OpenAI account and keep your Api
* Key in an environment variable called OPENAI_API_KEY.
* Before running this example you must have an OpenAI account and keep your Api Key in an
* environment variable called OPENAI_API_KEY.
*
* @see https://platform.openai.com/docs/api-reference/authentication
*/
Expand Down Expand Up @@ -59,4 +59,5 @@ private static void showTitle(String title) {
System.out.println(title);
System.out.println("-".repeat(times));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
@Builder
@Data
public class Album {

private Integer id;
private String title;
private Integer userId;
}

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

import java.util.List;

import io.github.sashirestela.cleverclient.annotation.Body;
import io.github.sashirestela.cleverclient.annotation.DELETE;
import io.github.sashirestela.cleverclient.annotation.GET;
Expand All @@ -11,6 +9,8 @@
import io.github.sashirestela.cleverclient.annotation.Query;
import io.github.sashirestela.cleverclient.annotation.Resource;

import java.util.List;

@Resource("/albums")
public interface AlbumService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -14,8 +13,10 @@
@Data
@JsonInclude(Include.NON_NULL)
public class Post {

private Integer id;
private String title;
private String body;
private Integer userId;
}

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

import java.util.List;

import io.github.sashirestela.cleverclient.annotation.Body;
import io.github.sashirestela.cleverclient.annotation.DELETE;
import io.github.sashirestela.cleverclient.annotation.GET;
Expand All @@ -12,6 +10,8 @@
import io.github.sashirestela.cleverclient.annotation.Query;
import io.github.sashirestela.cleverclient.annotation.Resource;

import java.util.List;

@Resource("/posts")
public interface PostService {

Expand All @@ -33,4 +33,4 @@ public interface PostService {
@DELETE("/{postId}")
Post deletePost(@Path("postId") Integer postId);

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

import java.util.List;

import lombok.Builder;
import lombok.Getter;
import lombok.With;

import java.util.List;

@Getter
@Builder
public class ChatRequest {

private String model;
private List<Message> messages;
private Double temperature;
@With
private Boolean stream;
}

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

import java.util.List;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.List;

@NoArgsConstructor
@Getter
@ToString
public class ChatResponse {

private String id;
private String object;
private Long created;
Expand All @@ -20,4 +21,5 @@ public class ChatResponse {
public String firstContent() {
return getChoices().get(0).getMessage().getContent();
}
}

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

import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

import io.github.sashirestela.cleverclient.annotation.Body;
import io.github.sashirestela.cleverclient.annotation.POST;
import io.github.sashirestela.cleverclient.annotation.Resource;

import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

@Resource("/v1/chat/completions")
public interface ChatService {

Expand All @@ -26,4 +26,4 @@ default CompletableFuture<Stream<ChatResponse>> createAsyncStream(ChatRequest ch
return __createAsyncStream(request);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
Expand All @@ -11,11 +10,13 @@
@Getter
@ToString
public class Choice {

private Integer index;

@JsonAlias({ "delta" })
private Message message;

@JsonProperty("finish_reason")
private String finishReason;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@Getter
@ToString
public class Message {

private String role;
private String content;
}

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

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
Expand All @@ -10,6 +9,7 @@
@Getter
@ToString
public class Usage {

@JsonProperty("prompt_tokens")
private Integer promptTokens;

Expand All @@ -18,4 +18,5 @@ public class Usage {

@JsonProperty("total_tokens")
private Integer totalTokens;
}

}
47 changes: 22 additions & 25 deletions src/main/java/io/github/sashirestela/cleverclient/CleverClient.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package io.github.sashirestela.cleverclient;

import java.net.http.HttpClient;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.UnaryOperator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.sashirestela.cleverclient.http.HttpProcessor;
import io.github.sashirestela.cleverclient.http.HttpRequestData;
import io.github.sashirestela.cleverclient.support.Configurator;
Expand All @@ -18,14 +8,23 @@
import lombok.Getter;
import lombok.NonNull;
import lombok.Singular;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.http.HttpClient;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.UnaryOperator;

/**
* Main class and entry point to use this library. This is a kind of wrapper
* that makes it easier to use the Java's HttpClient component to call http
* services by using annotated interfaces.
* Main class and entry point to use this library. This is a kind of wrapper that makes it easier to
* use the Java's HttpClient component to call http services by using annotated interfaces.
*/
@Getter
public class CleverClient {

private static final Logger logger = LoggerFactory.getLogger(CleverClient.class);

private final String baseUrl;
Expand All @@ -37,15 +36,13 @@ public class CleverClient {
/**
* Constructor to create an instance of CleverClient.
*
* @param baseUrl Root of the url of the API service to call.
* Mandatory.
* @param baseUrl Root of the url of the API service to call. Mandatory.
* @param headers Http headers for all the API service. Optional.
* @param httpClient Custom Java's HttpClient component. One is created
* by default if none is passed. Optional.
* @param requestInterceptor Function to modify the request once it has been
* built.
* @param endOfStream Text used to mark the final of streams when
* handling server sent events (SSE). Optional.
* @param httpClient Custom Java's HttpClient component. One is created by default if none
* is passed. Optional.
* @param requestInterceptor Function to modify the request once it has been built.
* @param endOfStream Text used to mark the final of streams when handling server sent events
* (SSE). Optional.
*/
@Builder
public CleverClient(@NonNull String baseUrl, @Singular Map<String, String> headers, HttpClient httpClient,
Expand All @@ -69,9 +66,8 @@ public CleverClient(@NonNull String baseUrl, @Singular Map<String, String> heade
}

/**
* Creates an instance of an annotated interface that represents a resource of
* the API service and its methods represent the endpoints that we can call:
* Get, Post, Put, Patch, Delete.
* Creates an instance of an annotated interface that represents a resource of the API service and
* its methods represent the endpoints that we can call: Get, Post, Put, Patch, Delete.
*
* @param <T> Type of the interface.
* @param interfaceClass The interface to be instanced.
Expand All @@ -80,4 +76,5 @@ public CleverClient(@NonNull String baseUrl, @Singular Map<String, String> heade
public <T> T create(Class<T> interfaceClass) {
return this.httpProcessor.createProxy(interfaceClass);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Body {
}
}
Loading

0 comments on commit 30e76fb

Please sign in to comment.