|
|
**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](2.1Development 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](2.1Development Using MVP Pattern)). The datasource called `V2APIUser.updateUser(patchUser)`. What happens here? Let's take a look at the implementation of `V2APIUser`:
|
|
|
|
|
|
```kotlin
|
... | ... | @@ -18,6 +20,7 @@ public final class V2APIUser { |
|
|
}
|
|
|
```
|
|
|
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:
|
|
|
|
|
|
```kotlin
|
|
|
public final class V2API {
|
|
|
|
... | ... | @@ -51,4 +54,5 @@ public final class V2API { |
|
|
|
|
|
}
|
|
|
```
|
|
|
|
|
|
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 |