Skip to content

Commit 7f95401

Browse files
erenalpaslanmehmet6parmakappcent
authored andcommitted
Feature/exception (#56)
Co-authored-by: Eren <[email protected]>
1 parent 19f62a1 commit 7f95401

File tree

123 files changed

+448
-477
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+448
-477
lines changed

src/main/java/mobi/appcent/helium/HeliumSdkClient.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.Map;
1212

1313
public class HeliumSdkClient {
14-
private static final String BASE_API_URL = "https://api.helium.io/v1/";
1514
private OkHttpClient client;
1615
protected static final Gson gson = new Gson();
1716

@@ -22,9 +21,8 @@ public void initOkHttpClient() {
2221
this.client = HttpClient.client();
2322
}
2423

25-
//TODO: Override base url
2624
public void setOkHttpClient(OkHttpClient client) {
27-
if (this.client == null) {
25+
if (client == null) {
2826
initOkHttpClient();
2927
}else {
3028
this.client = client;

src/main/java/mobi/appcent/helium/Main.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package mobi.appcent.helium;
22

33
import mobi.appcent.helium.api.HeliumApi;
4-
4+
import mobi.appcent.helium.exception.ApiException;
5+
import mobi.appcent.helium.response.validator.ValidatorsResponse;
56

67
public class Main {
78

89
public static void main(String[] args) {
910
try {
1011
HeliumApi heliumApi = new HeliumApi();
1112
System.out.println(
12-
heliumApi.assertLocations()
13-
.getListOfAssertLocations()
13+
heliumApi.hotspots()
14+
.getHotspots()
1415
.execute()
15-
.toString()
1616
);
17-
} catch (Exception e) {
18-
System.out.println(e.getMessage());
17+
} catch (ApiException e) {
18+
System.out.println(e);
1919
}
2020

2121
}

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import mobi.appcent.helium.response.validator.ValidatorsResponse;
88
import okhttp3.Call;
99

10-
import java.io.IOException;
10+
import mobi.appcent.helium.exception.ApiException;
1111
import java.lang.reflect.Type;
1212
import java.util.ArrayList;
1313
import java.util.Collections;
@@ -21,111 +21,106 @@ public static AccountApi getInstance() {
2121
return new AccountApi();
2222
}
2323

24-
@Override
25-
String path() {
26-
return BASE_URL + "/accounts";
27-
}
28-
2924
//region getAccounts
3025
@Override
31-
public AccountsRequest getAccounts() throws IOException {
26+
public AccountsRequest getAccounts() {
3227
return new AccountsRequest(sdkClient);
3328
}
3429
//endregion
3530

3631
//region getRichestAccounts
3732
@Override
38-
public RichestAccountsRequest getRichestAccounts() throws IOException {
33+
public RichestAccountsRequest getRichestAccounts() {
3934
return new RichestAccountsRequest(sdkClient);
4035
}
4136
//endregion
4237

4338
//region getAccountForAddress
4439
@Override
45-
public AccountForAddressRequest getAccountForAddress(String address) throws IOException {
40+
public AccountForAddressRequest getAccountForAddress(String address) {
4641
return new AccountForAddressRequest(sdkClient, address);
4742
}
4843
//endregion
4944

5045
//region getHotspotsForAccount
5146
@Override
52-
public HotspotsForAccountRequest getHotspotsForAccount(String address) throws IOException {
47+
public HotspotsForAccountRequest getHotspotsForAccount(String address) {
5348
return new HotspotsForAccountRequest(sdkClient, address);
5449
}
5550
//endregion
5651

5752
//region getValidatorsForAccount
5853
@Override
59-
public ValidatorsForAccountRequest getValidatorsForAccount(String address) throws IOException {
54+
public ValidatorsForAccountRequest getValidatorsForAccount(String address) {
6055
return new ValidatorsForAccountRequest(sdkClient, address);
6156
}
6257
//endregion
6358

6459
//region getOUIsForAccount
6560
@Override
66-
public OUIsForAccountRequest getOUIsForAccount(String address) throws IOException {
61+
public OUIsForAccountRequest getOUIsForAccount(String address) {
6762
return new OUIsForAccountRequest(sdkClient, address);
6863
}
6964
//endregion
7065

7166
//region getRolesForAccount
7267
@Override
73-
public RolesForAccountRequest getRolesForAccount(String address) throws IOException {
68+
public RolesForAccountRequest getRolesForAccount(String address) {
7469
return new RolesForAccountRequest(sdkClient, address);
7570
}
7671
//endregion
7772

7873
//region getRolesCountsForAccount
7974
@Override
80-
public RolesCountsForAccountRequest getRolesCountsForAccount(String address) throws IOException {
75+
public RolesCountsForAccountRequest getRolesCountsForAccount(String address){
8176
return new RolesCountsForAccountRequest(sdkClient, address);
8277
}
8378
//endregion
8479

8580
//region getElectionsForAccount
8681
@Override
87-
public ElectionsForAccountRequest getElectionsForAccount(String address) throws IOException {
82+
public ElectionsForAccountRequest getElectionsForAccount(String address){
8883
return new ElectionsForAccountRequest(sdkClient, address);
8984
}
9085
//endregion
9186

9287
//region getChallengesForAccount
9388
@Override
94-
public ChallengesForAccountRequest getChallengesForAccount(String address) throws IOException {
89+
public ChallengesForAccountRequest getChallengesForAccount(String address){
9590
return new ChallengesForAccountRequest(sdkClient, address);
9691
}
9792
//endregion
9893

9994
//region getPendingTransactionsForAccount
10095
@Override
101-
public PendingTransactionsForAccountRequest getPendingTransactionsForAccount(String address) throws IOException {
96+
public PendingTransactionsForAccountRequest getPendingTransactionsForAccount(String address){
10297
return new PendingTransactionsForAccountRequest(sdkClient, address);
10398
}
10499
//endregion
105100

106101
//region getRewardsForAccount
107102
@Override
108-
public RewardsForAccountRequest getRewardsForAccount(String address, String minTime) throws IOException {
103+
public RewardsForAccountRequest getRewardsForAccount(String address, String minTime){
109104
return new RewardsForAccountRequest(sdkClient, address, minTime);
110105
}
111106
//endregion
112107

113108
//region getRewardsByRewardsBlockForAccount
114109
@Override
115-
public RewardsByRewardsBlockForAccountRequest getRewardsByRewardsBlockForAccount(String address, String block) throws IOException {
110+
public RewardsByRewardsBlockForAccountRequest getRewardsByRewardsBlockForAccount(String address, String block){
116111
return new RewardsByRewardsBlockForAccountRequest(sdkClient, address, block);
117112
}
118113
//endregion
119114

120115
//region getRewardTotalsForAccount
121116
@Override
122-
public RewardTotalsForAccountRequest getRewardTotalsForAccount(String address, String minTime) throws IOException {
117+
public RewardTotalsForAccountRequest getRewardTotalsForAccount(String address, String minTime){
123118
return new RewardTotalsForAccountRequest(sdkClient, address, minTime);
124119
}
125120
//endregion
126121

127122
//TODO: 500 response
128-
private String getStatsForAccount(String address) throws IOException {
123+
private String getStatsForAccount(String address){
129124
return null;
130125
}
131126
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
* Created by erenalpaslan on 3.12.2022
77
*/
88
public class AssertLocationsApi extends BaseApi implements IAssertLocationsAPI{
9-
@Override
10-
String path() {
11-
return null;
12-
}
139

1410
public static AssertLocationsApi getInstance() {
1511
return new AssertLocationsApi();
Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package mobi.appcent.helium.api;
22

33
import com.google.gson.Gson;
4-
import com.google.gson.JsonParseException;
4+
import com.google.gson.JsonIOException;
5+
import com.google.gson.JsonSyntaxException;
56
import com.google.gson.stream.JsonReader;
67
import mobi.appcent.helium.HeliumSdkClient;
8+
import mobi.appcent.helium.exception.ApiException;
79
import okhttp3.Call;
810
import okhttp3.Response;
911

12+
import mobi.appcent.helium.exception.ApiException;
13+
1014
import java.io.IOException;
1115
import java.io.StringReader;
1216
import java.lang.reflect.Type;
@@ -15,50 +19,9 @@
1519
* Base class for apis
1620
*/
1721
abstract class BaseApi {
18-
private static final Gson gson = new Gson();
19-
protected static final String BASE_URL = "https://api.helium.io/v1";
20-
21-
abstract String path();
22-
2322
protected HeliumSdkClient sdkClient;
2423

2524
protected void setSdkClient(HeliumSdkClient sdkClient) {
2625
this.sdkClient = sdkClient;
2726
}
28-
29-
protected <T> T execute(Call call, Type returnType) throws IOException {
30-
try {
31-
Response response = call.execute();
32-
return handleResponse(response, returnType);
33-
} catch (IOException e) {
34-
throw new IOException("Error");
35-
}
36-
}
37-
38-
//TODO: Handle Error
39-
private <T> T handleResponse(Response response, Type returnType) throws IOException {
40-
if(response.isSuccessful()) {
41-
if (response.body() != null) {
42-
return deserialize(response.body().string(), returnType);
43-
}
44-
}
45-
return null;
46-
}
47-
48-
public static <T> T deserialize(String body, Type returnType) {
49-
try {
50-
JsonReader jsonReader = new JsonReader(new StringReader(body));
51-
// see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
52-
jsonReader.setLenient(true);
53-
return gson.fromJson(jsonReader, returnType);
54-
} catch (JsonParseException e) {
55-
// Fallback processing when failed to parse JSON form response body:
56-
// return the response body string directly for the String return type;
57-
if (returnType.equals(String.class)) {
58-
return (T) body;
59-
} else {
60-
throw (e);
61-
}
62-
}
63-
}
6427
}

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import mobi.appcent.helium.request.block.*;
44
import org.jetbrains.annotations.NotNull;
55

6-
import java.io.IOException;
6+
import mobi.appcent.helium.exception.ApiException;
77

88
/**
99
* Created by erenalpaslan on 28.10.2022
@@ -15,43 +15,38 @@ public static BlockApi getInstance() {
1515
}
1616

1717
@Override
18-
String path() {
19-
return BASE_URL + "/blocks";
20-
}
21-
22-
@Override
23-
public HeightRequest getHeight() throws IOException {
18+
public HeightRequest getHeight(){
2419
return new HeightRequest(sdkClient);
2520
}
2621

2722
@Override
28-
public BlockStatsRequest getBlockStats() throws IOException {
23+
public BlockStatsRequest getBlockStats(){
2924
return new BlockStatsRequest(sdkClient);
3025
}
3126

3227

3328
@Override
34-
public BlockDescriptionsRequest getBlockDescriptions() throws IOException {
29+
public BlockDescriptionsRequest getBlockDescriptions(){
3530
return new BlockDescriptionsRequest(sdkClient);
3631
}
3732

3833
@Override
39-
public BlockByGivenHeightRequest getBlockByGivenHeight(@NotNull Integer height) throws IOException {
34+
public BlockByGivenHeightRequest getBlockByGivenHeight(@NotNull Integer height){
4035
return new BlockByGivenHeightRequest(sdkClient, height);
4136
}
4237

4338
@Override
44-
public BlockTransactionsByGivenHeightRequest getBlockTransactionsByGivenHeight(@NotNull Integer height) throws IOException {
39+
public BlockTransactionsByGivenHeightRequest getBlockTransactionsByGivenHeight(@NotNull Integer height){
4540
return new BlockTransactionsByGivenHeightRequest(sdkClient, height);
4641
}
4742

4843
@Override
49-
public BlockByGivenHashRequest getBlockByGivenHash(@NotNull String hash) throws IOException {
44+
public BlockByGivenHashRequest getBlockByGivenHash(@NotNull String hash){
5045
return new BlockByGivenHashRequest(sdkClient, hash);
5146
}
5247

5348
@Override
54-
public BlockTransactionsByGivenHashRequest getBlockTransactionsByGivenHash(@NotNull String hash) throws IOException {
49+
public BlockTransactionsByGivenHashRequest getBlockTransactionsByGivenHash(@NotNull String hash){
5550
return new BlockTransactionsByGivenHashRequest(sdkClient, hash);
5651
}
5752

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
* Created by erenalpaslan on 26.11.2022
1010
*/
1111
public class ChainVariablesApi extends BaseApi implements IChainVariablesApi{
12-
@Override
13-
String path() {
14-
return UrlConstant.CHAIN_VARIABLES_PATH;
15-
}
1612

1713
public static ChainVariablesApi getInstance() {
1814
return new ChainVariablesApi();

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
* Created by erenalpaslan on 27.11.2022
77
*/
88
public class ChallengesApi extends BaseApi implements IChallengesApi{
9-
@Override
10-
String path() {
11-
return null;
12-
}
139

1410
public static ChallengesApi getInstance() {
1511
return new ChallengesApi();

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mobi.appcent.helium.api;
22

33
import com.google.gson.reflect.TypeToken;
4+
import mobi.appcent.helium.exception.ApiException;
45
import mobi.appcent.helium.httpClient.HttpMethod;
56
import mobi.appcent.helium.request.city.CitiesRequest;
67
import mobi.appcent.helium.request.city.CityByGivenCityIdRequest;
@@ -12,7 +13,7 @@
1213
import okhttp3.Call;
1314
import org.jetbrains.annotations.NotNull;
1415

15-
import java.io.IOException;
16+
import mobi.appcent.helium.exception.ApiException;
1617
import java.lang.reflect.Type;
1718
import java.util.ArrayList;
1819
import java.util.Collections;
@@ -27,22 +28,17 @@ public static CityApi getInstance() {
2728
}
2829

2930
@Override
30-
String path() {
31-
return BASE_URL + "/cities";
32-
}
33-
34-
@Override
35-
public CitiesRequest getCities() throws IOException {
31+
public CitiesRequest getCities(){
3632
return new CitiesRequest(sdkClient);
3733
}
3834

3935
@Override
40-
public CityByGivenCityIdRequest getCityByGivenCityId(@NotNull String cityId) throws IOException {
36+
public CityByGivenCityIdRequest getCityByGivenCityId(@NotNull String cityId){
4137
return new CityByGivenCityIdRequest(sdkClient, cityId);
4238
}
4339

4440
@Override
45-
public HotspotsByGivenCityIdRequest getHotspotsByGivenCityId(@NotNull String cityId) throws IOException {
41+
public HotspotsByGivenCityIdRequest getHotspotsByGivenCityId(@NotNull String cityId){
4642
return new HotspotsByGivenCityIdRequest(sdkClient, cityId);
4743
}
4844
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
*/
1010
public class DCBurnsApi extends BaseApi implements IDCBurnsApi{
1111

12-
@Override
13-
String path() {
14-
return null;
15-
}
16-
1712
public static DCBurnsApi getInstance() {
1813
return new DCBurnsApi();
1914
}

0 commit comments

Comments
 (0)