Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Server Models

sisdiovi edited this page Oct 15, 2017 · 17 revisions

These models are annotated with @ServerModel, they will be automatically downloaded and uploaded to an existing REST Api, using JSON or Url Encoded Form format.

@ServerModel(
        baseUrl= "http://example.com",
        get="/data"
)
public class MyModel extends Model {

}

Parameters:

The base url to the REST Api to be used. This parameter supports Formatted Syntax.

String baseUrl();

A single route that will be accessed with HTTP GET method to download the model. This parameter supports [Formatted Syntax](Formatted Syntax).

String get() default "";

Headers to be passed to the routes specified in the "get" parameter. This parameter supports [Formatted Syntax](Formatted Syntax). Headers should be separated by "=" or ":".

String[] getHeaders() default {""};

A single route that will be accessed with HTTP POST method to upload the model. This parameter supports [Formatted Syntax](Formatted Syntax).

String post() default {""};

Headers to be passed to the routes specified in the "post" parameter. This parameter supports [Formatted Syntax](Formatted Syntax). Headers should be separated by "=" or ":".

String[] postHeaders() default {""};

How to Post the data, two value are available:

  • PostType.Body: Will send in the body of the POST request a JSON representing the current object.
  • PostType.Fields: Will encode every field in the body of the POST request using a Form Encoded Format, similar to an HTML Form Post.
RequestType[] postType() default RequestType.Body;

List of fields that will be sent with the request. By default, all the fields are sent.

String[] postFields() default "";

Save the response of the request in the specified field

String model() default "";

Save the response of the request in the specified model

Class<?> modelClass() default Object.class;

Specify a default query for all the injected models, when not query is provided

String defaultQuery() default "";

Simulate the server response with mocked data.

boolean mock() default false;
String mockResult() default "";

Specify a list of named requests for load or put methods,

ServerRequest[] load() default @ServerRequest(action="NONE");
ServerRequest[] put() default @ServerRequest(action="NONE");

Server Requests

With the ServerRequest annotation, that is passed as an array to the load or put parameters of the @ServerModel, a more complex API can be spacified for the model.

public @interface ServerRequest {
	String name() default "";
	
	RequestMethod method() default RequestMethod.Default;
	RequestType type() default RequestType.Body;
	
	String action();
	String[] headers() default "";
	
	String[] fields() default "";
	
	String model() default "";
	Class<?> modelClass() default Object.class;
	
	boolean mock() default false;
	String mockResult() default "";
	
	public enum RequestMethod {Default, Delete, Get, Head, Post, Put, Patch}
	public enum RequestType {Body, Fields}
}

Most of the parameters are the same with the same behavior, introducing two new parameters:

Each @ServerRequest should have a value assigned to action, it is a string to name this request. It will be used to select this request from others when a Load or Put is triggered over a @Model.

String action();

Permits use any HTTP Verb in the call. The Default values are POST for Put methods, and GET for Load methods.

RequestMethod method() default RequestMethod.Default;

Debugging the request result

To debug or get more information of the specific request made to the server, it can be declared a static method that takes as a parameter the Response object from the server

@ServerModel
static void getResponse(Response response) {
   //Do something with the response
}

Method executed when the model is successfully downloaded

When a field is injected successfully from a Server Model (it means that the information was downloaded from the Server Successfully), the method @AfterLoad filtered with @ServerModel will be called. This method is totally optional.

@AfterLoad
@ServerModel
void serverModelLoaded(String query, String orderBy) {
    //Do something with the data
}

The string parameters "query" and "orderBy" are totally optional, so you can omit one or both of them, the only important thing is that the first parameter is the "query" passed to the @Model annotation when injecting the class, and the second is the "orderBy" parameter.

Example