Skip to content

Commit 1ab0e83

Browse files
erenalpaslanmehmet6parmakappcent
authored andcommitted
#31 Create request classes for city api (#53)
Co-authored-by: Eren <[email protected]>
1 parent 2d5d2bb commit 1ab0e83

File tree

6 files changed

+134
-67
lines changed

6 files changed

+134
-67
lines changed

src/main/java/mobi/appcent/helium/api/CityApi.java

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.google.gson.reflect.TypeToken;
44
import mobi.appcent.helium.httpClient.HttpMethod;
5+
import mobi.appcent.helium.request.city.CitiesRequest;
6+
import mobi.appcent.helium.request.city.CityByGivenCityIdRequest;
7+
import mobi.appcent.helium.request.city.HotspotsByGivenCityIdRequest;
58
import mobi.appcent.helium.response.city.CitiesResponse;
69
import mobi.appcent.helium.response.city.CityResponse;
710
import mobi.appcent.helium.response.hotspot.HotspotsResponse;
@@ -29,75 +32,17 @@ String path() {
2932
}
3033

3134
@Override
32-
public APIgetCitiesRequest getCities() throws IOException {
33-
return new APIgetCitiesRequest();
34-
}
35-
36-
public class APIgetCitiesRequest {
37-
private String search;
38-
private String cursor;
39-
40-
public APIgetCitiesRequest() {}
41-
42-
public APIgetCitiesRequest search(String search) {
43-
this.search = search;
44-
return this;
45-
}
46-
47-
public APIgetCitiesRequest cursor(String cursor) {
48-
this.cursor = cursor;
49-
return this;
50-
}
51-
52-
public CitiesResponse execute() throws IOException {
53-
ArrayList<Pair> queryParams = new ArrayList<>();
54-
queryParams.add(Pair.create("search", search));
55-
queryParams.add(Pair.create("cursor", cursor));
56-
Call call = sdkClient.buildCall(path(), HttpMethod.GET, queryParams, null, null);
57-
Type type = TypeToken.get(CitiesResponse.class).getType();
58-
return CityApi.this.execute(call, type);
59-
}
35+
public CitiesRequest getCities() throws IOException {
36+
return new CitiesRequest(sdkClient);
6037
}
6138

6239
@Override
63-
public APIgetCityByGivenCityIdRequest getCityByGivenCityId(@NotNull String cityId) throws IOException {
64-
return new APIgetCityByGivenCityIdRequest(cityId);
65-
}
66-
67-
public class APIgetCityByGivenCityIdRequest {
68-
69-
private final String cityId;
70-
71-
public APIgetCityByGivenCityIdRequest(String id) {
72-
this.cityId = id;
73-
}
74-
75-
public CityResponse execute() throws IOException {
76-
String path = path()+"/"+cityId;
77-
Call call = sdkClient.buildCall(path, HttpMethod.GET, Collections.emptyList(), null, null);
78-
Type type = TypeToken.get(CityResponse.class).getType();
79-
return CityApi.this.execute(call, type);
80-
}
40+
public CityByGivenCityIdRequest getCityByGivenCityId(@NotNull String cityId) throws IOException {
41+
return new CityByGivenCityIdRequest(sdkClient, cityId);
8142
}
8243

8344
@Override
84-
public APIgetHotspotsByGivenCityIdRequest getHotspotsByGivenCityId(@NotNull String cityId) throws IOException {
85-
return new APIgetHotspotsByGivenCityIdRequest(cityId);
86-
}
87-
88-
public class APIgetHotspotsByGivenCityIdRequest {
89-
90-
private final String cityId;
91-
92-
public APIgetHotspotsByGivenCityIdRequest(String id) {
93-
this.cityId = id;
94-
}
95-
96-
public HotspotsResponse execute() throws IOException {
97-
String path = path()+"/"+cityId+"/hotspots";
98-
Call call = sdkClient.buildCall(path, HttpMethod.GET, Collections.emptyList(), null, null);
99-
Type type = TypeToken.get(HotspotsResponse.class).getType();
100-
return CityApi.this.execute(call, type);
101-
}
45+
public HotspotsByGivenCityIdRequest getHotspotsByGivenCityId(@NotNull String cityId) throws IOException {
46+
return new HotspotsByGivenCityIdRequest(sdkClient, cityId);
10247
}
10348
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package mobi.appcent.helium.api;
22

3+
import mobi.appcent.helium.request.city.CitiesRequest;
4+
import mobi.appcent.helium.request.city.CityByGivenCityIdRequest;
5+
import mobi.appcent.helium.request.city.HotspotsByGivenCityIdRequest;
36
import org.jetbrains.annotations.NotNull;
47

58
import java.io.IOException;
@@ -8,7 +11,7 @@
811
* Created by erenalpaslan on 28.10.2022
912
*/
1013
public interface ICityApi {
11-
public CityApi.APIgetCitiesRequest getCities() throws IOException;
12-
public CityApi.APIgetCityByGivenCityIdRequest getCityByGivenCityId(@NotNull String cityId) throws IOException;
13-
public CityApi.APIgetHotspotsByGivenCityIdRequest getHotspotsByGivenCityId(@NotNull String cityId) throws IOException;
14+
CitiesRequest getCities() throws IOException;
15+
CityByGivenCityIdRequest getCityByGivenCityId(@NotNull String cityId) throws IOException;
16+
HotspotsByGivenCityIdRequest getHotspotsByGivenCityId(@NotNull String cityId) throws IOException;
1417
}

src/main/java/mobi/appcent/helium/common/UrlConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class UrlConstant {
1919
public static final String ASSERT_LOCATIONS_PATH = BASE_PATH + "/assert_locations";
2020
public static final String ELECTIONS_PATH = BASE_PATH + "/elections";
2121
public static final String TRANSACTIONS_PATH = BASE_PATH + "/transactions";
22+
public static final String CITY_PATH = BASE_PATH + "/cities";
2223
public static final String HOTSPOTS_PATH = BASE_PATH + "/hotspots";
2324
public static final String BLOCKS_PATH = BASE_PATH + "/blocks";
2425
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package mobi.appcent.helium.request.city;
2+
3+
import com.google.gson.reflect.TypeToken;
4+
import mobi.appcent.helium.HeliumSdkClient;
5+
import mobi.appcent.helium.api.CityApi;
6+
import mobi.appcent.helium.common.FieldConstant;
7+
import mobi.appcent.helium.common.UrlConstant;
8+
import mobi.appcent.helium.httpClient.HttpMethod;
9+
import mobi.appcent.helium.model.Pair;
10+
import mobi.appcent.helium.request.BaseRequest;
11+
import mobi.appcent.helium.response.city.CitiesResponse;
12+
import okhttp3.Call;
13+
14+
import java.io.IOException;
15+
import java.lang.reflect.Type;
16+
import java.util.ArrayList;
17+
18+
/**
19+
* Created by erenalpaslan on 4.12.2022
20+
*/
21+
public class CitiesRequest extends BaseRequest {
22+
private final HeliumSdkClient client;
23+
private String search;
24+
private String cursor;
25+
26+
public CitiesRequest(HeliumSdkClient client) {
27+
this.client = client;
28+
}
29+
30+
public CitiesRequest search(String search) {
31+
this.search = search;
32+
return this;
33+
}
34+
35+
public CitiesRequest cursor(String cursor) {
36+
this.cursor = cursor;
37+
return this;
38+
}
39+
40+
public CitiesResponse execute() throws IOException {
41+
ArrayList<Pair> queryParams = new ArrayList<>();
42+
queryParams.add(Pair.create(FieldConstant.SEARCH, search));
43+
queryParams.add(Pair.create(FieldConstant.CURSOR, cursor));
44+
Call call = client.buildCall(UrlConstant.CITY_PATH, HttpMethod.GET, queryParams, null, null);
45+
Type type = TypeToken.get(CitiesResponse.class).getType();
46+
return execute(call, type);
47+
}
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package mobi.appcent.helium.request.city;
2+
3+
import com.google.gson.reflect.TypeToken;
4+
import mobi.appcent.helium.HeliumSdkClient;
5+
import mobi.appcent.helium.api.CityApi;
6+
import mobi.appcent.helium.common.UrlConstant;
7+
import mobi.appcent.helium.httpClient.HttpMethod;
8+
import mobi.appcent.helium.request.BaseRequest;
9+
import mobi.appcent.helium.response.city.CityResponse;
10+
import okhttp3.Call;
11+
12+
import java.io.IOException;
13+
import java.lang.reflect.Type;
14+
import java.util.Collections;
15+
16+
/**
17+
* Created by erenalpaslan on 4.12.2022
18+
*/
19+
public class CityByGivenCityIdRequest extends BaseRequest {
20+
private final HeliumSdkClient client;
21+
22+
private final String cityId;
23+
24+
public CityByGivenCityIdRequest(HeliumSdkClient client, String id) {
25+
this.client = client;
26+
this.cityId = id;
27+
}
28+
29+
public CityResponse execute() throws IOException {
30+
String path = UrlConstant.CITY_PATH +"/"+cityId;
31+
Call call = client.buildCall(path, HttpMethod.GET, Collections.emptyList(), null, null);
32+
Type type = TypeToken.get(CityResponse.class).getType();
33+
return execute(call, type);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package mobi.appcent.helium.request.city;
2+
3+
import com.google.gson.reflect.TypeToken;
4+
import mobi.appcent.helium.HeliumSdkClient;
5+
import mobi.appcent.helium.api.CityApi;
6+
import mobi.appcent.helium.common.UrlConstant;
7+
import mobi.appcent.helium.httpClient.HttpMethod;
8+
import mobi.appcent.helium.request.BaseRequest;
9+
import mobi.appcent.helium.response.hotspot.HotspotsResponse;
10+
import okhttp3.Call;
11+
12+
import java.io.IOException;
13+
import java.lang.reflect.Type;
14+
import java.util.Collections;
15+
16+
/**
17+
* Created by erenalpaslan on 4.12.2022
18+
*/
19+
public class HotspotsByGivenCityIdRequest extends BaseRequest {
20+
private final HeliumSdkClient client;
21+
22+
private final String cityId;
23+
24+
public HotspotsByGivenCityIdRequest(HeliumSdkClient client, String id) {
25+
this.client = client;
26+
this.cityId = id;
27+
}
28+
29+
public HotspotsResponse execute() throws IOException {
30+
String path = UrlConstant.CITY_PATH +"/"+cityId+"/hotspots";
31+
Call call = client.buildCall(path, HttpMethod.GET, Collections.emptyList(), null, null);
32+
Type type = TypeToken.get(HotspotsResponse.class).getType();
33+
return execute(call, type);
34+
}
35+
}

0 commit comments

Comments
 (0)