Package com.longport
Class HttpClient
- java.lang.Object
-
- com.longport.HttpClient
-
- All Implemented Interfaces:
AutoCloseable
public class HttpClient extends Object implements AutoCloseable
-
-
Constructor Summary
Constructors Constructor Description HttpClient(String httpUrl, String appKey, String appSecret, String accessToken)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
static HttpClient
fromEnv()
Create a new `HttpClient` from the given environment variables<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
-
close
public void close() throws Exception
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-
fromEnv
public static HttpClient fromEnv() throws OpenApiException
Create a new `HttpClient` from the given environment variablesIt first gets the environment variables from the .env file in the current directory. # Variables - `LONGPORT_HTTP_URL` - HTTP endpoint url - `LONGPORT_APP_KEY` - App key - `LONGPORT_APP_SECRET` - App secret - `LONGPORT_ACCESS_TOKEN` - Access token
- Returns:
- Config object
- Throws:
OpenApiException
- If an error occurs
-
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
-
-