This repository was archived by the owner on May 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.java
More file actions
46 lines (39 loc) · 1.8 KB
/
Copy pathConvert.java
File metadata and controls
46 lines (39 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.fxdatapi;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Optional;
public class Convert {
public static Optional<String> convert(String username, String date, String baseCurrency, String targetCurrency, String amount) {
try {
HttpClient client = HttpClient.newHttpClient();
String json = String.format(
"{" +
"\"username\": \"%s\"," +
"\"date\": \"%s\"," +
"\"base_currency\": \"%s\"," +
"\"target_currency\": \"%s\"," +
"\"amount\": \"%s\"" +
"}", username, date, baseCurrency, targetCurrency, amount);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://fxdatapi.com/v1/convert"))
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.header("Cookie", "user_type=member; username=" + username)
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
return Optional.of(response.body());
} else {
System.out.println("Conversion failed with status code: " + response.statusCode());
System.out.println("Response body: " + response.body());
return Optional.empty();
}
} catch (Exception e) {
e.printStackTrace();
return Optional.empty();
}
}
}