· 6 years ago · Mar 20, 2019, 07:02 PM
1curl -X POST -u "1ada25121ae7c173b1005d96d0f54387:ae3b6618969b0f81aedb7d59bee48a18" "https://api.hitbtc.com/api/2/order" -H "accept: application/json" -H "Content-Type: application/x-www-form-urlencoded" -d "symbol=ETHBTC&side=sell&type=limit&timeInForce=GTC&quantity=0.01&price=0"
2
3
4package cz.bthc.rest_consumer.api;
5
6import org.apache.http.auth.AuthScope;
7import org.apache.http.auth.UsernamePasswordCredentials;
8import org.apache.http.client.CredentialsProvider;
9import org.apache.http.client.HttpClient;
10import org.apache.http.impl.client.BasicCredentialsProvider;
11import org.apache.http.impl.client.HttpClientBuilder;
12import org.springframework.http.HttpEntity;
13import org.springframework.http.HttpHeaders;
14import org.springframework.http.HttpMethod;
15import org.springframework.http.ResponseEntity;
16import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
17import org.springframework.web.client.RestClientException;
18import org.springframework.web.client.RestTemplate;
19
20import lombok.AllArgsConstructor;
21import lombok.Getter;
22import lombok.NoArgsConstructor;
23
24@NoArgsConstructor
25public class RestClient extends RestTemplate {
26
27 public RestClient(String username, String password) {
28 CredentialsProvider credsProvider = new BasicCredentialsProvider();
29 credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
30 HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
31 setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
32 }
33
34}
35
36public class ... {
37
38 public HitBtcOrder sendOrder(String symbol, double quantity, double price, String side)
39 throws RestResponseException, JsonParseException {
40
41 HttpHeaders headers = new HttpHeaders();
42 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
43
44 MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
45 map.add("symbol", symbol);
46 map.add("side", side);
47 map.add("quantity", Double.toString(quantity));
48 map.add("price", Double.toString(price));
49 HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
50
51 RestClient restClient = new RestClient(publicKey, secretKey);
52 // TADY JE PROBLEM
53 ResponseEntity<String> re = restClient.postForEntity(URI + "/order", request, String.class);
54 String content = re.getBody();
55 return JsonUtils.stringToObject(content, HitBtcOrder);
56 }
57
58}