|
|
**Network calls in the Android game client**
|
|
|
In order to access the actual database via the game server, the app client has to communicate with the server. This is done using the AkamuAPI class (`V2API.java`). In it, a collection of functions can be found which do the actual network calls. This is done using a REST API ([OkHttp](https://square.github.io/okhttp/)). Functions for doing PUT/GET/POST/DELETE/PATCH requests are provided. Furthermore it holds the server url, the token and a few [interceptors](https://square.github.io/okhttp/interceptors/) for easier logging and error handling.
|
|
|
|
|
|
**Accessing the API**
|
|
|
The app uses the MVP pattern ([see](Development/Development Using MVP Pattern)) for passing data from the datasource to the view and vice versa. The API calls are executed asynchronously in the datasource. However there is a further abstraction layer between AkamuAPI and the datasources. The classes in this layer follow the naimg convention V2APIXYZ where XYZ defines some kind of grouping of calls which execute similar or corresponding actions. E.g. there is the `V2APIUser `class which is responsible for all kinds of calls that deal with the app user model i.e. getting all users, one specific user and updating the user. Let's take the example from our ([MVP tutorial](Development/Development Using MVP Pattern)). The datasource called `V2APIUser.updateUser(patchUser)`. What happens here? Let's take a look at the implementation of `V2APIUser`:
|
|
|
|
|
|
```
|
|
|
public final class V2APIUser {
|
|
|
|
|
|
...
|
|
|
|
|
|
public static void updateUser(PatchUser newUserInfo) throws IOException, AkamuAPIException {
|
|
|
Gson gsonBuilder = new GsonBuilder().create();
|
|
|
V2API.patch("user", gsonBuilder.toJson(newUserInfo));
|
|
|
}
|
|
|
|
|
|
...
|
|
|
}
|
|
|
```
|
|
|
We can see that there is actually not much to see here. However, these two lines in the `updateUser` method allow us to convert the `PatchUser` model object into a JSON string. This is done by using [Gson](https://github.com/google/gson/blob/master/UserGuide.md). In the second line the AkamuAPI is then called with a PATCH request to the endpoint with name "user". Let's take a short look on the PATCH call in the AkamuAPI, too:
|
|
|
```
|
|
|
public final class V2API {
|
|
|
|
|
|
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
|
|
private static final String baseURL = "https://dev.akamu.de/api/v2/"; // Dev version of server
|
|
|
private static String token = "";
|
|
|
private static final OkHttpClient client = new OkHttpClient.Builder().build();
|
|
|
|
|
|
...
|
|
|
|
|
|
static String patch(String endpoint, String requestBodyJSON) throws IOException, AkamuAPIException {
|
|
|
RequestBody body = RequestBody.create(JSON, requestBodyJSON);
|
|
|
Request request = new Request.Builder()
|
|
|
.url(baseURL + endpoint)
|
|
|
.header("Authorization", "Bearer " + token)
|
|
|
.patch(body)
|
|
|
.build();
|
|
|
// This is the actual network call
|
|
|
Response response = client.newCall(request).execute();
|
|
|
String responseBody = new String(Objects.requireNonNull(response.body()).bytes(), Charset.forName("UTF-8"));
|
|
|
|
|
|
// Only return the response body if the HTTP response code was 200
|
|
|
// Throw an exception otherwise
|
|
|
if (!responseCodeOK(response.code()))
|
|
|
throw new AkamuAPIException(response.code(), responseBody);
|
|
|
|
|
|
return responseBody;
|
|
|
}
|
|
|
|
|
|
...
|
|
|
|
|
|
}
|
|
|
```
|
|
|
OkHttp does most of the work for us here. We only provide some handling of the HTTP response codes. |
|
|
\ No newline at end of file |