Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.
Open
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
5 changes: 5 additions & 0 deletions swagger-gen/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
</dependencies>
<properties>
<java.version>1.7</java.version>
Expand Down
198 changes: 198 additions & 0 deletions swagger-gen/java/src/main/java/io/swagger/client/BybitApiClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*
* Bybit API
* ## REST API for the Bybit Exchange. Base URI: [https://api-testnet.bybit.com]
*
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/


package io.swagger.client;

import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.internal.http.HttpMethod;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class BybitApiClient extends ApiClient {

protected String apiKey;
protected String secret;

public BybitApiClient(String apiKey, String secret) {
this.apiKey = apiKey;
this.secret = secret;
}

public BybitApiClient() {
}

@Override
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

public void setSecret(String secret) {
this.secret = secret;
}

private String getApiKey() {
return apiKey;
}

private String getSecret() {
return secret;
}

/**
* return timestamp
*/
private String getTimestamp() {
Calendar cal = Calendar.getInstance();
TimeZone tz = TimeZone.getTimeZone("GMT");
cal.setTimeZone(tz);
return String.valueOf(cal.getTimeInMillis());
}

private String getSignature(TreeMap<String, String> params, String secret) throws NoSuchAlgorithmException, InvalidKeyException {
Set<String> keySet = params.keySet();
Iterator<String> iter = keySet.iterator();
StringBuilder sb = new StringBuilder();
while (iter.hasNext()) {
String key = iter.next();
sb.append(key + "=" + params.get(key));
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);

return bytesToHex(sha256_HMAC.doFinal(sb.toString().getBytes()));
}



private String bytesToHex(byte[] hash) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}

/**
* Build an HTTP request with the given options.
*
* @param path The sub-path of the HTTP URL
* @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE"
* @param queryParams The query parameters
* @param collectionQueryParams The collection query parameters
* @param body The request body object
* @param headerParams The header parameters
* @param formParams The form parameters
* @param authNames The authentications to apply
* @param progressRequestListener Progress request listener
* @return The HTTP request
* @throws ApiException If fail to serialize the request body object
*/
@Override
public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {

updateParamsForAuth(authNames, queryParams, headerParams);

JSONObject jsonParams = new JSONObject();

if (!path.contains("public")){
if (method.equals("GET") || method.equals("POST")){

String timestamp = this.getTimestamp();
TreeMap map = new TreeMap<String, String>(
new Comparator<String>() {
public int compare(String obj1, String obj2) {
//sort in alphabet order
return obj1.compareTo(obj2);
}
});

map.put("api_key", this.getApiKey());
map.put("timestamp", timestamp);

String signature = null;
try {
signature = this.getSignature(map, this.getSecret());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
queryParams.add(new Pair("api_key",this.getApiKey()));
queryParams.add(new Pair("timestamp",timestamp));
queryParams.add(new Pair("sign",signature));

if (null!=formParams && !formParams.isEmpty()){
for (Map.Entry entry:formParams.entrySet()){
jsonParams.put(entry.getKey().toString(),entry.getValue());
}
}
jsonParams.put("api_key",this.getApiKey());
jsonParams.put("timestamp",timestamp);
jsonParams.put("sign",signature);

body = jsonParams;
}
}
final String url = buildUrl(path, queryParams, collectionQueryParams);
final Request.Builder reqBuilder = new Request.Builder().url(url);
processHeaderParams(headerParams, reqBuilder);

String contentType = (String) headerParams.get("Content-Type");
// ensuring a default content type
if (contentType == null) {
contentType = "application/json";
}

RequestBody reqBody;
if (!HttpMethod.permitsRequestBody(method)) {
reqBody = null;
} else if ("application/x-www-form-urlencoded".equals(contentType)) {
reqBody = buildRequestBodyFormEncoding(formParams);
} else if ("multipart/form-data".equals(contentType)) {
reqBody = buildRequestBodyMultipart(formParams);
} else if (body == null) {
if ("DELETE".equals(method)) {
// allow calling DELETE without sending a request body
reqBody = null;
} else {
// use an empty request body (for POST, PUT and PATCH)
reqBody = RequestBody.create(MediaType.parse(contentType), "");
}
} else {
reqBody = serialize(body, contentType);
}

Request request = null;

if(progressRequestListener != null && reqBody != null) {
ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, progressRequestListener);
request = reqBuilder.method(method, progressRequestBody).build();
} else {
request = reqBuilder.method(method, reqBody).build();
}

return request;
}

}
48 changes: 48 additions & 0 deletions swagger-gen/java/src/test/java/example/OrderApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package example;

import io.swagger.client.ApiException;
import io.swagger.client.BybitApiClient;
import io.swagger.client.api.OrderApi;
import org.junit.Ignore;
import org.junit.Test;

import java.math.BigDecimal;

/**
* private API tests example for OrderApi
*/
@Ignore
public class OrderApiTest {

private String api_key = "YOUR API KEY";
private String secret = "YOUR SECRET";
private BybitApiClient apiClient = new BybitApiClient(api_key,secret);
private OrderApi api = new OrderApi(apiClient);

/**
* Place active order
*
*
*
* @throws ApiException
* if the Api call fails
*/
@Test
public void orderNewV2Test() throws ApiException {
String side = null;
String symbol = null;
String orderType = null;
BigDecimal qty = null;
String timeInForce = null;
Double price = null;
Double takeProfit = null;
Double stopLoss = null;
Boolean reduceOnly = null;
Boolean closeOnTrigger = null;
String orderLinkId = null;
String trailingStop = null;
Object response = api.orderNewV2(side, symbol, orderType, qty, timeInForce, price, takeProfit, stopLoss, reduceOnly, closeOnTrigger, orderLinkId, trailingStop);

// TODO: test validations
}
}
45 changes: 45 additions & 0 deletions swagger-gen/java/src/test/java/example/SymbolApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Bybit API
* ## REST API for the Bybit Exchange. Base URI: [https://api-testnet.bybit.com]
*
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/


package example;

import io.swagger.client.ApiException;
import io.swagger.client.BybitApiClient;
import io.swagger.client.api.SymbolApi;
import org.junit.Ignore;
import org.junit.Test;

/**
* public API tests example for SymbolApi
*/
@Ignore
public class SymbolApiTest {

private final SymbolApi api = new SymbolApi();

/**
* Query Symbols.
*
*
*
* @throws ApiException
* if the Api call fails
*/
@Test
public void symbolGetTest() throws ApiException {
Object response = api.symbolGet();

// TODO: test validations
}

}