Package com.longbridge
Class HttpClient
- java.lang.Object
-
- com.longbridge.HttpClient
-
- All Implemented Interfaces:
AutoCloseable
public class HttpClient extends Object implements AutoCloseable
Longbridge OpenAPI HTTP client.Provides authenticated HTTP access to the Longbridge REST API. Instances are created via the static factory methods
fromApikey(java.lang.String, java.lang.String, java.lang.String)orfromApikeyEnv(). The client must be closed after use (it implementsAutoCloseable).
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()static HttpClientfromApikey(String appKey, String appSecret, String accessToken)Create a newHttpClientusing API Key authentication.static HttpClientfromApikey(String appKey, String appSecret, String accessToken, String httpUrl)Create a newHttpClientusing API Key authentication with a custom HTTP endpoint URL.static HttpClientfromApikeyEnv()Create a newHttpClientfrom environment variables (API Key authentication).static HttpClientfromOAuth(OAuth oauth)Create a newHttpClientfrom an OAuth handle.static HttpClientfromOAuth(OAuth oauth, String httpUrl)Create a newHttpClientfrom an OAuth handle with a custom HTTP endpoint URL.<T> CompletableFuture<T>request(Class<T> respClass, String method, String path)Performs a HTTP request<T> CompletableFuture<T>request(Class<T> respClass, String method, String path, Object requestBody)Performs a HTTP request with body<T> CompletableFuture<T>request(Class<T> respClass, String method, String path, Object requestBody, HashMap<String,String> headers)Performs a HTTP request with headers
-
-
-
Method Detail
-
fromApikey
public static HttpClient fromApikey(String appKey, String appSecret, String accessToken)
Create a newHttpClientusing API Key authentication.LONGBRIDGE_HTTP_URLis read from the environment automatically.- Parameters:
appKey- App keyappSecret- App secretaccessToken- Access token- Returns:
- HttpClient object
-
fromApikey
public static HttpClient fromApikey(String appKey, String appSecret, String accessToken, String httpUrl)
Create a newHttpClientusing API Key authentication with a custom HTTP endpoint URL.The
httpUrlparameter overridesLONGBRIDGE_HTTP_URLfrom the environment.- Parameters:
appKey- App keyappSecret- App secretaccessToken- Access tokenhttpUrl- HTTP endpoint URL override- Returns:
- HttpClient object
-
close
public void close() throws Exception- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
fromApikeyEnv
public static HttpClient fromApikeyEnv() throws OpenApiException
Create a newHttpClientfrom environment variables (API Key authentication).Variables:
LONGBRIDGE_HTTP_URL,LONGBRIDGE_APP_KEY,LONGBRIDGE_APP_SECRET,LONGBRIDGE_ACCESS_TOKEN- Returns:
- HttpClient object
- Throws:
OpenApiException- If an error occurs
-
fromOAuth
public static HttpClient fromOAuth(OAuth oauth)
Create a newHttpClientfrom an OAuth handle.LONGBRIDGE_HTTP_URLis read from the environment automatically.- Parameters:
oauth- OAuth handle returned byOAuthBuilder.build(java.util.function.Consumer<java.lang.String>)- Returns:
- HttpClient object
-
fromOAuth
public static HttpClient fromOAuth(OAuth oauth, String httpUrl)
Create a newHttpClientfrom an OAuth handle with a custom HTTP endpoint URL.The
httpUrlparameter overridesLONGBRIDGE_HTTP_URLfrom the environment.- Parameters:
oauth- OAuth handle returned byOAuthBuilder.build(java.util.function.Consumer<java.lang.String>)httpUrl- HTTP endpoint URL override- Returns:
- HttpClient object
-
request
public <T> CompletableFuture<T> request(Class<T> respClass, String method, String path) throws RuntimeException
Performs a HTTP request- Type Parameters:
T- Response class type- Parameters:
respClass- Response class object, it can be nullmethod- HTTP method, e.g. get, postpath- Request path- Returns:
- A Future representing the result of the operation
- Throws:
RuntimeException- If an error occurs
-
request
public <T> CompletableFuture<T> request(Class<T> respClass, String method, String path, Object requestBody) throws RuntimeException
Performs a HTTP request with body- Type Parameters:
T- Response class type- Parameters:
respClass- Response class object, it can be nullmethod- HTTP method, e.g. get, postpath- Request pathrequestBody- Request body, it can be null- Returns:
- A Future representing the result of the operation
- Throws:
RuntimeException- If an error occurs
-
request
public <T> CompletableFuture<T> request(Class<T> respClass, String method, String path, Object requestBody, HashMap<String,String> headers) throws RuntimeException
Performs a HTTP request with headers- Type Parameters:
T- Response class type- Parameters:
respClass- Response class object, it can be nullmethod- HTTP method, e.g. get, postpath- Request pathrequestBody- Request body, it can be nullheaders- Request headers, it can be null- Returns:
- A Future representing the result of the operation
- Throws:
RuntimeException- If the request fails
-
-