Skip to content

Commit b877473

Browse files
spkjpfaustbrian
authored andcommitted
fix: set request headers (#20)
1 parent 2970c90 commit b877473

5 files changed

Lines changed: 27 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## Unreleased
99

10-
## 0.1.1 - 2018-11-23
10+
## 0.1.1 - 2018-12-10
1111

12-
### Changed:
13-
- Always send the `Content-Type` header
12+
### Fixed:
13+
- Set `API-Version` instead of `version` on client
14+
- Correctly set request headers
1415

1516
## 0.1.0 - 2018-10-08
1617
- Initial Release

src/main/java/org/arkecosystem/client/Connection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class Connection<T extends AbstractAPI> {
1515
private T api;
1616

1717
public Connection(Map<String, Object> config) {
18-
this.version = ((int) (config.get("version")));
19-
this.client = new Client(config.get("host").toString(), (int) config.get("version"));
18+
this.version = ((int) (config.get("API-Version")));
19+
this.client = new Client(config.get("host").toString(), Integer.toString(this.version));
2020

2121
this.api = (T) ((this.version == 1) ? new One(this.client) : new Two(this.client));
2222
}

src/main/java/org/arkecosystem/client/http/Client.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,34 @@
1010

1111
public class Client {
1212
private String host;
13-
private int version;
13+
private String version;
1414
private OkHttpClient client;
15+
private Headers headers;
1516
private MediaType JSON = MediaType.parse("application/json");
1617

17-
public Client(String host, int version) {
18+
public Client(String host, String version) {
1819
this.host = host;
1920
this.version = version;
2021
this.client = new OkHttpClient();
22+
23+
HashMap<String, String> headers = new HashMap<>();
24+
headers.put("API-Version", this.version);
25+
headers.put("content-type", this.JSON.toString());
26+
27+
this.headers = Headers.of(headers);
2128
}
2229

2330
public LinkedTreeMap<String, Object> get(String url, Map<String, Object> params) throws IOException {
24-
HttpUrl.Builder httpBuider = HttpUrl.parse(this.host + url).newBuilder();
31+
HttpUrl.Builder httpBuilder = HttpUrl.parse(this.host + url).newBuilder();
2532

2633
if (params != null) {
2734
for (Map.Entry<String, Object> entry : params.entrySet()) {
28-
httpBuider.addQueryParameter(entry.getKey(), entry.getValue().toString());
35+
httpBuilder.addQueryParameter(entry.getKey(), entry.getValue().toString());
2936
}
3037
}
3138

32-
Request request = new Request.Builder().url(httpBuider.build()).build();
39+
Request request = new Request.Builder().headers(this.headers).url(httpBuilder.build()).build();
40+
3341
Response response = client.newCall(request).execute();
3442
return new Gson().fromJson(response.body().string(), new LinkedTreeMap<String, Object>().getClass());
3543
}
@@ -40,7 +48,7 @@ public LinkedTreeMap<String, Object> get(String url) throws IOException {
4048

4149
public LinkedTreeMap<String, Object> post(String url, Map payload) throws IOException {
4250
RequestBody body = RequestBody.create(JSON, new Gson().toJson(payload));
43-
Request request = new Request.Builder().url(this.host + url).post(body).build();
51+
Request request = new Request.Builder().headers(this.headers).url(this.host + url).post(body).build();
4452
Response response = client.newCall(request).execute();
4553
return new Gson().fromJson(response.body().string(), new LinkedTreeMap<String, Object>().getClass());
4654
}

src/test/java/org/arkecosystem/client/ConnectionManagerTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
import static org.junit.jupiter.api.Assertions.assertEquals;
1111

1212
public class ConnectionManagerTest {
13-
1413
@Test
1514
public void connect() {
1615
HashMap<String, Object> map = new HashMap<>();
1716
map.put("host", "dummy");
18-
map.put("version", 1);
17+
map.put("API-Version", 1);
1918

2019
ConnectionManager manager = new ConnectionManager();
2120
manager.connect(map);
@@ -26,7 +25,7 @@ public void connect() {
2625
public void disconnect() {
2726
HashMap<String, Object> map = new HashMap<>();
2827
map.put("host", "dummy");
29-
map.put("version", 1);
28+
map.put("API-Version", 1);
3029

3130
ConnectionManager manager = new ConnectionManager();
3231
manager.connect(map);
@@ -35,10 +34,11 @@ public void disconnect() {
3534
assertEquals(0, manager.getConnections().size());
3635
}
3736

37+
@Test
3838
public void connection() {
3939
HashMap<String, Object> map = new HashMap<>();
4040
map.put("host", "dummy");
41-
map.put("version", 1);
41+
map.put("API-Version", 1);
4242

4343
ConnectionManager manager = new ConnectionManager();
4444
manager.connect(map);
@@ -68,14 +68,14 @@ public void getConnections() {
6868

6969
HashMap<String, Object> map = new HashMap<>();
7070
map.put("host", "dummy");
71-
map.put("version", 1);
71+
map.put("API-Version", 1);
7272

7373
Connection<One> connection1 = manager.connect(map);
7474
assertNotNull(connection1);
7575

7676
map = new HashMap<>();
7777
map.put("host", "dummy");
78-
map.put("version", 2);
78+
map.put("API-Version", 2);
7979

8080
Connection<Two> connection2 = manager.connect(map, "backup");
8181
assertNotNull(connection2);

src/test/java/org/arkecosystem/client/MockHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static Connection connection(int version) {
1212

1313
HashMap<String, Object> map = new HashMap<>();
1414
map.put("host", mockServer.url("/").toString());
15-
map.put("version", version);
15+
map.put("API-Version", version);
1616
Connection connection = new Connection(map);
1717

1818
MockResponse mockedResponse = new MockResponse();

0 commit comments

Comments
 (0)