Skip to content

Latest commit

 

History

History
101 lines (79 loc) · 3.85 KB

doc_android_model.md

File metadata and controls

101 lines (79 loc) · 3.85 KB

Java Model SDK

Model classes for use with the Mojio REST API. These objects are annotated for serialization with GSON. The Model SDK can be plugged into your existing REST-client framework.

Download

compile 'io.moj.java:mojio-sdk-model:0.0.1'

Instructions

The Model SDK is a collection of POJOs matching the entities in the Mojio API. If you're starting from scratch, consider using the mojio-sdk-rest module for both the model objects and a basic client. If you have existing an REST-client framework or need finer control, however, this module can stand on its own.

Below is an example of integrating the mojio-sdk-model with Retrofit

package io.moj.example;

import io.moj.mobile.android.sdk.model.ListResponse;
import io.moj.mobile.android.sdk.model.User;
import io.moj.mobile.android.sdk.model.Vehicle;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;

/**
 * Retrofit interface for the Mojio REST API.
 */
public interface MojioApi {

    @GET("vehicles")
    Call<ListResponse<Vehicle>> getVehicles();

    @GET("vehicles/{id}")
    Call<Vehicle> getVehicle(@Path("id") id);

    @PUT("vehicles/{id}")
    Call<Vehicle> updateVehicle(@Path("id") id, @Body Vehicle vehicle);

    @GET("vehicles/{id}/history/states")
    Call<ListResponse<VehicleMeasure>> getTripHistory(@Path("id") id);

    @GET("vehicles/{id}/history/states")
    Call<ListResponse<VehicleMeasure>> getTripHistory(@Path("id") id, @Query("skip") int skip, @Query("take") int take);

}

Here's an example of how to embed access tokens in requests that require authentication using an OkHttp Interceptor:

OkHttpClient httpClient = new OkHttpClient.Builder()
        .addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();

                // set the access token in the header if we have it
                if (!TextUtils.isEmpty(accessToken)) {
                    request = request.newBuilder()
                            .header("Authorization", "Bearer " + accessToken)
                            .build();
                }
                return chain.proceed(request);
            }
        })
        .build();
MojioApi mojioApi = new Retrofit.Builder()
        .baseUrl(MojioEnvironment.getDefault().getApiUrl())
        .addConverterFactory(GsonConverterFactory.create())
        .client(httpClient)
        .build().create(MojioApi.class);

Even if you're not using Retrofit, the mojio-sdk-model module can be plugged into your existing architecture. Currently, however, we have a dependency on Gson. If this is an issue for you, let us know with a feature request!

Persisting model objects

Each Mojio entity (not including enums or units) such as Vehicle, Mojio, and User extends MojioObject which includes a Long "_id" field to facilitate usage of Android's Cursor framework.

The private fields inside model objects also follow non-standard cases for Java to match the entities exactly as returned by the API. We've abstracted this detail away with getters and setters and the benefit is that your SQLite tables, when generated from these model objects with an ORM such as Cupboard, will exactly mirror the API.

To help build queries with whatever framework you're using, each model class defines static constants for each of it's fields:

SQLiteQueryBuilder query = new SQLiteQueryBuilder();
query.setTables(Vehicle.class.getSimpleName());
query.appendWhere(Vehicle.ID + "=");
Cursor cursor = qBuilder.query(db, projection, selection, selectionArgs, null, null, orderBy);