-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFcsApi.java
More file actions
227 lines (198 loc) · 6.32 KB
/
FcsApi.java
File metadata and controls
227 lines (198 loc) · 6.32 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* FCS API - REST API Client
*
* Java client for Forex, Cryptocurrency, and Stock market data
*
* @package FcsApi
* @author FCS API <support@fcsapi.com>
* @link https://fcsapi.com
*/
package com.fcsapi;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
* FCS API REST Client
* Main client class for accessing Forex, Crypto, and Stock market data.
*/
public class FcsApi {
private static final String BASE_URL = "https://api-v4.fcsapi.com/";
private final Gson gson = new Gson();
private Map<String, Object> lastResponse = new HashMap<>();
/** Configuration instance */
private FcsConfig config;
// Lazy-loaded modules
private FcsForex forex;
private FcsCrypto crypto;
private FcsStock stock;
/**
* Get Forex API module (lazy loading)
*/
public FcsForex getForex() {
if (forex == null) forex = new FcsForex(this);
return forex;
}
/**
* Get Crypto API module (lazy loading)
*/
public FcsCrypto getCrypto() {
if (crypto == null) crypto = new FcsCrypto(this);
return crypto;
}
/**
* Get Stock API module (lazy loading)
*/
public FcsStock getStock() {
if (stock == null) stock = new FcsStock(this);
return stock;
}
/**
* Constructor with default config
*/
public FcsApi() {
this.config = new FcsConfig();
}
/**
* Constructor with FcsConfig
* @param config FcsConfig object
*/
public FcsApi(FcsConfig config) {
this.config = config != null ? config : new FcsConfig();
}
/**
* Constructor with API key string
* @param apiKey API key string
*/
public FcsApi(String apiKey) {
this.config = FcsConfig.withAccessKey(apiKey);
}
/**
* Set request timeout
* @param seconds Timeout in seconds
* @return Self for method chaining
*/
public FcsApi setTimeout(int seconds) {
config.setTimeout(seconds);
return this;
}
/**
* Get config
* @return FcsConfig instance
*/
public FcsConfig getConfig() {
return config;
}
/**
* Generate token for frontend use
* Only works when AuthMethod is 'token'
* @return Map with _token, _expiry, _public_key
*/
public Map<String, Object> generateToken() {
return config.generateToken();
}
/**
* Make API request (POST with form data)
* @param endpoint API endpoint
* @param parameters Request parameters
* @return Response data or null on error
*/
public Map<String, Object> request(String endpoint, Map<String, Object> parameters) {
if (parameters == null) {
parameters = new HashMap<>();
}
// Add authentication parameters
Map<String, Object> authParams = config.getAuthParams();
parameters.putAll(authParams);
String url = BASE_URL + endpoint;
try {
URL urlObj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Accept", "application/json");
conn.setConnectTimeout(config.getConnectTimeout() * 1000);
conn.setReadTimeout(config.getTimeout() * 1000);
conn.setDoOutput(true);
// Build form data
StringBuilder formData = new StringBuilder();
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
if (formData.length() > 0) formData.append("&");
formData.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
formData.append("=");
formData.append(URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8"));
}
// Send request
try (OutputStream os = conn.getOutputStream()) {
os.write(formData.toString().getBytes(StandardCharsets.UTF_8));
}
// Read response
StringBuilder response = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
}
// Parse JSON response
lastResponse = gson.fromJson(response.toString(), new TypeToken<Map<String, Object>>(){}.getType());
return lastResponse;
} catch (Exception e) {
lastResponse = new HashMap<>();
lastResponse.put("status", false);
lastResponse.put("code", 0);
lastResponse.put("msg", "Request Error: " + e.getMessage());
lastResponse.put("response", null);
return null;
}
}
/**
* Make API request with no parameters
* @param endpoint API endpoint
* @return Response data or null on error
*/
public Map<String, Object> request(String endpoint) {
return request(endpoint, null);
}
/**
* Get last response
* @return Last response map
*/
public Map<String, Object> getLastResponse() {
return lastResponse;
}
/**
* Get response data only
* @return Response data or null
*/
public Object getResponseData() {
return lastResponse.get("response");
}
/**
* Check if last request was successful
* @return True if successful, false otherwise
*/
public boolean isSuccess() {
Object status = lastResponse.get("status");
if (status instanceof Boolean) {
return (Boolean) status;
}
return false;
}
/**
* Get error message from last response
* @return Error message or null if successful
*/
public String getError() {
if (isSuccess()) return null;
Object msg = lastResponse.get("msg");
return msg != null ? msg.toString() : "Unknown error";
}
}