-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added model classes and the default username query parameter
- Loading branch information
Matthias Bruns
committed
May 20, 2017
1 parent
6682f70
commit 9c42ed9
Showing
5 changed files
with
270 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/matthiasbruns/rxretrofit/network/CityResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.matthiasbruns.rxretrofit.network; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by mbruns on 20.05.17. | ||
* The response wrapper for GSON to be able to parse the JSON response into our models | ||
*/ | ||
|
||
public class CityResponse { | ||
|
||
public List<Geoname> geonames; | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/matthiasbruns/rxretrofit/network/CityService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.matthiasbruns.rxretrofit.network; | ||
|
||
import io.reactivex.Single; | ||
import retrofit2.http.GET; | ||
import retrofit2.http.Query; | ||
|
||
/** | ||
* Created by mbruns on 20.05.17. | ||
*/ | ||
|
||
public interface CityService { | ||
|
||
/** | ||
* This method returns all cities within a given bounding box | ||
* | ||
* Example from the api docs: citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo | ||
* | ||
* @param north bounding box north | ||
* @param south bounding box south | ||
* @param east bounding box east | ||
* @param west bounding box west | ||
* @param lang geoname output language | ||
*/ | ||
@GET("citiesJSON") | ||
Single<CityResponse> queryGeonames(@Query("north") double north, @Query("south") double south, | ||
@Query("east") double east, @Query("west") double west, @Query("lang") String lang); | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/matthiasbruns/rxretrofit/network/Geoname.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.matthiasbruns.rxretrofit.network; | ||
|
||
/** | ||
* Created by mbruns on 20.05.17. | ||
* Created to parse the JSON response with GSON | ||
* | ||
* "lng": -99.12766456604, | ||
* "geonameId": 3530597, | ||
* "countrycode": "MX", | ||
* "name": "Mexiko-Stadt", | ||
* "fclName": "city, village,...", | ||
* "toponymName": "Mexico City", | ||
* "fcodeName": "capital of a political entity", | ||
* "wikipedia": "en.wikipedia.org/wiki/Mexico_City", | ||
* "lat": 19.428472427036, | ||
* "fcl": "P", | ||
* "population": 12294193, | ||
* "fcode": "PPLC" | ||
*/ | ||
|
||
public class Geoname { | ||
|
||
public double lat; | ||
public double lng; | ||
public long geonameId; | ||
public String countrycode; | ||
public String name; | ||
public String fclName; | ||
public String toponymName; | ||
public String fcodeName; | ||
public String wikipedia; | ||
public String fcl; | ||
public long population; | ||
public String fcode; | ||
} |
66 changes: 66 additions & 0 deletions
66
app/src/main/java/com/matthiasbruns/rxretrofit/network/RetrofitHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.matthiasbruns.rxretrofit.network; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.HttpUrl; | ||
import okhttp3.Interceptor; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
|
||
/** | ||
* This class initializes retrofit with a default configuration. | ||
* You can use this class to initialize the different services. | ||
*/ | ||
|
||
public class RetrofitHelper { | ||
|
||
/** | ||
* The CityService communicates with the json api of the city provider. | ||
*/ | ||
public CityService getCityService() { | ||
final Retrofit retrofit = createRetrofit(); | ||
return retrofit.create(CityService.class); | ||
} | ||
|
||
/** | ||
* This custom client will append the "username=demo" query after every request. | ||
*/ | ||
private OkHttpClient createOkHttpClient() { | ||
final OkHttpClient.Builder httpClient = | ||
new OkHttpClient.Builder(); | ||
httpClient.addInterceptor(new Interceptor() { | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
final Request original = chain.request(); | ||
final HttpUrl originalHttpUrl = original.url(); | ||
|
||
final HttpUrl url = originalHttpUrl.newBuilder() | ||
.addQueryParameter("username", "demo") | ||
.build(); | ||
|
||
// Request customization: add request headers | ||
final Request.Builder requestBuilder = original.newBuilder() | ||
.url(url); | ||
|
||
final Request request = requestBuilder.build(); | ||
return chain.proceed(request); | ||
} | ||
}); | ||
|
||
return httpClient.build(); | ||
} | ||
|
||
/** | ||
* Creates a pre configured Retrofit instance | ||
*/ | ||
private Retrofit createRetrofit() { | ||
return new Retrofit.Builder() | ||
.baseUrl("http://api.geonames.org/") | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.client(createOkHttpClient()) | ||
.build(); | ||
} | ||
} |