Create REST client to consume external API endpoint


#1

Create the type that will be used to send out HTTP/HTTPs requests

@rest(url='https://api.example.com', retries=3)
type ExampleApiClient mixes REST {

    @rest(uri='some/path', method='POST', bodyType='json')
    doCallPostAndCreateSomething: function(
            @rest(header=true, headerName='Accept') accept: string,
            @rest(header=true, headerName='Authorization') authorization: string,
            @rest(parameterName='param1')        param1 : json,
            @rest(parameterName='param2')        param2 :  string
        ): PostEndpointResponsePayload
}

Define the response payload type which will be create by the platform and filled with data mapped from the raw response. Example:

type EndpointResponsePayload<T> {

    /**
     * Some field in the response
     */
    somefield: sometype

    /**
     * The time at which the API response was sent.
     */
    timestamp: datetime

    /**
     * An error code, normally the same as the status of the response (e.g. 201)
     */
    code: int

    /**
     * Array of result objects in the response body.
     */
    data: [T]

}

The response body can be anything, in this case we expect an array of some parameter. Example an array of EndpointResultEntry will be received in the body:

type PostEndpointResponsePayload mixes EndpointResponsePayload<EndpointResultEntry>

and EndpointResultEntry should map to the json objects that the endpoint returns as a result:

type EndpointResultEntry {
  field1: Type1
  field2: Type2
  . . .
}