Skip to content

Commit c223b59

Browse files
David Sánchezwoakas
David Sánchez
authored andcommitted
Adding methods to change baseUrl
There was no way to change the default URL when using a token instead of the API Key. These methods were added to overcome that.
1 parent c5cbed7 commit c223b59

File tree

2 files changed

+87
-70
lines changed

2 files changed

+87
-70
lines changed

src/main/java/com/ubidots/ApiClient.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import com.google.gson.JsonParser;
1010

1111
public class ApiClient {
12-
12+
1313
private ServerBridge bridge;
14-
14+
1515
public ApiClient(String apiKey) {
1616
bridge = new ServerBridge(apiKey);
1717
}
@@ -20,7 +20,7 @@ public ApiClient(String apiKey, String baseUrl) {
2020
bridge = new ServerBridge(apiKey, baseUrl);
2121
}
2222

23-
public ApiClient() {}
23+
public ApiClient() {}
2424

2525
ServerBridge getServerBridge() {
2626
return bridge;
@@ -31,77 +31,81 @@ public ApiClient fromToken(String token) {
3131
return this;
3232
}
3333

34+
public void setBaseUrl(String baseUrl) {
35+
bridge.setBaseUrl(baseUrl);
36+
}
37+
3438
public DataSource[] getDataSources() {
3539
String json = bridge.get("datasources");
36-
40+
3741
Gson gson = new Gson();
3842
List<Map<String, Object>> rawDataSources = gson.fromJson(json, List.class);
39-
43+
4044
DataSource[] dataSources = new DataSource[rawDataSources.size()];
41-
45+
4246
for (int i = 0; i < rawDataSources.size(); i++) {
4347
dataSources[i] = new DataSource(rawDataSources.get(i), this);
4448
}
4549

4650
return dataSources;
4751
}
48-
52+
4953
public DataSource getDataSource(String id) {
5054
String json = bridge.get("datasources/" + id);
51-
55+
5256
Gson gson = new Gson();
5357
Map<String, Object> rawDataSource = (Map<String, Object>) gson.fromJson(json, Map.class);
54-
58+
5559
if (rawDataSource.containsKey("detail"))
5660
return null;
5761
else
5862
return new DataSource(rawDataSource, this);
5963
}
60-
64+
6165
public DataSource createDataSource(String name) {
6266
return createDataSource(name, null, null);
6367
}
64-
68+
6569
public DataSource createDataSource(String name, Map<String, String> context, List<String> tags) {
6670
Map<String, Object> data = new HashMap<String, Object>();
6771
data.put("name", name);
68-
69-
if (context != null)
72+
73+
if (context != null)
7074
data.put("context", context);
71-
75+
7276
if (tags != null)
7377
data.put("tags", tags);
74-
78+
7579
Gson gson = new Gson();
7680
String json = bridge.post("datasources/", gson.toJson(data));
7781
Map<String, Object> rawDataSource = (Map<String, Object>) gson.fromJson(json, Map.class);
78-
82+
7983
DataSource ds = new DataSource(rawDataSource, this);
8084

8185
return ds;
8286
}
83-
87+
8488
public Variable[] getVariables() {
8589
String json = bridge.get("variables");
86-
90+
8791
Gson gson = new Gson();
8892
List<Map<String, Object>> rawVariables = gson.fromJson(json, List.class);
89-
93+
9094
Variable[] variables = new Variable[rawVariables.size()];
91-
95+
9296
for (int i = 0; i < rawVariables.size(); i++) {
9397
variables[i] = new Variable(rawVariables.get(i), this);
9498
}
95-
99+
96100
return variables;
97101
}
98102

99103
public Variable getVariable(String id) {
100104
String json = bridge.get("variables/" + id);
101-
105+
102106
Gson gson = new Gson();
103107
Map<String, Object> rawVariable = (Map<String, Object>) gson.fromJson(json, Map.class);
104-
108+
105109
if (rawVariable.containsKey("detail"))
106110
return null;
107111
else

src/main/java/com/ubidots/ServerBridge.java

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,25 @@
2525

2626
/**
2727
* A Bridge between the Server and API objects.
28-
*
28+
*
2929
* Responsibilities: Make petitions to the browser with the right headers
3030
* and arguments.
3131
* @author Ubidots
3232
*/
3333

3434
public class ServerBridge {
35-
35+
3636
/* Final static variables */
3737
public static final String DEFAULT_BASE_URL = "https://things.ubidots.com/api/v1.6/";
38-
38+
3939
/* Instance variables */
4040
private String baseUrl;
4141
private String apiKey;
4242
private String token;
4343
private Map<String, String> tokenHeader;
4444
private Map<String, String> apiKeyHeader;
45-
46-
45+
46+
4747
ServerBridge(String apiKey) {
4848
this(apiKey, DEFAULT_BASE_URL);
4949
}
@@ -55,7 +55,7 @@ public class ServerBridge {
5555

5656
apiKeyHeader = new HashMap<>();
5757
apiKeyHeader.put("X-UBIDOTS-APIKEY", this.apiKey);
58-
58+
5959
initialize();
6060
}
6161

@@ -68,65 +68,78 @@ public class ServerBridge {
6868
tokenHeader.put("X-AUTH-TOKEN", token);
6969
}
7070

71+
ServerBridge(String token, boolean isToken, String baseUrl) {
72+
this.token = token;
73+
this.baseUrl = baseUrl;
74+
apiKey = null;
75+
76+
tokenHeader = new HashMap<>();
77+
tokenHeader.put("X-AUTH-TOKEN", token);
78+
}
79+
7180
public void initialize() {
7281
recieveToken();
7382
}
74-
83+
84+
public void setBaseUrl(String baseUrl) {
85+
this.baseUrl = baseUrl;
86+
}
87+
7588
private void recieveToken() {
7689
Gson gson = new Gson();
7790
token = (String) gson.fromJson(postWithApiKey("auth/token"), Map.class).get("token");
78-
91+
7992
tokenHeader = new HashMap<>();
8093
tokenHeader.put("X-AUTH-TOKEN", token);
8194
}
82-
95+
8396
private String postWithApiKey(String path) {
8497
String response = null; // return variable
85-
Map<String, String> headers = prepareHeaders(apiKeyHeader);
86-
98+
Map<String, String> headers = prepareHeaders(apiKeyHeader);
99+
87100
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
88101
String url = baseUrl + path;
89102

90103
HttpPost post = new HttpPost(url);
91-
104+
92105
for (String name : headers.keySet()) {
93-
post.setHeader(name, headers.get(name));
106+
post.setHeader(name, headers.get(name));
94107
}
95-
108+
96109
HttpResponse resp = client.execute(post);
97-
110+
98111
BufferedReader rd = new BufferedReader(
99112
new InputStreamReader(resp.getEntity().getContent()));
100113

101114
StringBuffer result = new StringBuffer();
102115
String line;
103-
116+
104117
while ((line = rd.readLine()) != null) {
105118
result.append(line);
106119
}
107120

108121
response = result.toString();
109122
} catch (Exception e) {
110123
e.printStackTrace();
111-
}
112-
124+
}
125+
113126
return response;
114127
}
115-
128+
116129
private Map<String, String> getCustomHeaders() {
117130
Map<String, String> customHeaders = new HashMap<>();
118-
131+
119132
customHeaders.put("content-type", "application/json");
120-
133+
121134
return customHeaders;
122135
}
123-
136+
124137
private Map<String, String> prepareHeaders(Map<String, String> arg) {
125138
Map<String, String> headers = new HashMap<>();
126-
139+
127140
headers.putAll(getCustomHeaders());
128141
headers.putAll(arg);
129-
142+
130143
return headers;
131144
}
132145

@@ -167,19 +180,19 @@ String get(String path, Map<String, String> customParams) {
167180
String url = baseUrl + path;
168181

169182
HttpGet get = new HttpGet(url);
170-
183+
171184
for (String name : headers.keySet()) {
172-
get.setHeader(name, headers.get(name));
185+
get.setHeader(name, headers.get(name));
173186
}
174-
187+
175188
HttpResponse resp = client.execute(get);
176-
189+
177190
BufferedReader rd = new BufferedReader(
178191
new InputStreamReader(resp.getEntity().getContent()));
179192

180193
StringBuffer result = new StringBuffer();
181194
String line;
182-
195+
183196
while ((line = rd.readLine()) != null) {
184197
result.append(line);
185198
}
@@ -195,7 +208,7 @@ String get(String path, Map<String, String> customParams) {
195208
} catch (Exception e) {
196209
e.printStackTrace();
197210
}
198-
211+
199212
return response;
200213
}
201214

@@ -214,32 +227,32 @@ String post(String path, String json) {
214227
String url = baseUrl + path;
215228

216229
HttpPost post = new HttpPost(url);
217-
230+
218231
for (String name : headers.keySet()) {
219-
post.setHeader(name, headers.get(name));
232+
post.setHeader(name, headers.get(name));
220233
}
221-
234+
222235
post.setEntity(new StringEntity(json));
223236
HttpResponse resp = client.execute(post);
224-
237+
225238
BufferedReader rd = new BufferedReader(
226239
new InputStreamReader(resp.getEntity().getContent()));
227240

228241
StringBuffer result = new StringBuffer();
229242
String line;
230-
243+
231244
while ((line = rd.readLine()) != null) {
232245
result.append(line);
233246
}
234247

235248
response = result.toString();
236249
} catch (Exception e) {
237250
e.printStackTrace();
238-
}
239-
251+
}
252+
240253
return response;
241254
}
242-
255+
243256
/**
244257
* Perform a DELETE request on the API with a given path
245258
* @param path Path to append to the base URL.
@@ -254,32 +267,32 @@ String delete(String path) {
254267
String url = baseUrl + path;
255268

256269
HttpDelete delete = new HttpDelete(url);
257-
270+
258271
for (String name : headers.keySet()) {
259-
delete.setHeader(name, headers.get(name));
272+
delete.setHeader(name, headers.get(name));
260273
}
261-
274+
262275
HttpResponse resp = client.execute(delete);
263-
276+
264277
if (resp.getEntity() == null) {
265278
response = "";
266279
} else {
267280
BufferedReader rd = new BufferedReader(
268281
new InputStreamReader(resp.getEntity().getContent()));
269-
282+
270283
StringBuffer result = new StringBuffer();
271284
String line;
272-
285+
273286
while ((line = rd.readLine()) != null) {
274287
result.append(line);
275288
}
276-
289+
277290
response = result.toString();
278291
}
279292
} catch (Exception e) {
280293
e.printStackTrace();
281-
}
282-
294+
}
295+
283296
return response;
284297
}
285298
}

0 commit comments

Comments
 (0)