Sensibill API Request
- To make Sensibill API request simpler, use instance of Request class to communicate with Sensibill Rest APIs
-
Request()
Default constructor forRequest
class. Does not need arguments. -
setEndPoint(endpoint: String)
Set API Endpoint. Endpoint cannot be null. Does not require a base url. -
addParameter(params: Map<String, String>)
Add parameters into url. Params cannot be null. -
setMethod(method: String, body: RequestBody?)
Set method and a request body for API call. Method cannot be null. When the method does not require a body, pass null. Example:setMethod("GET", null)
-
createRequestBody(jsonString: String): RequestBody
Create a request body by given json string. Return a request body. JsonString cannot be null. init(endpoint: String, responseCallback: Callback, authenticationType: RequestAuthenticationType = RequestAuthenticationType.AUTHORIZATION_BEARER, method: String = "GET", params: Map<String, String>? = null, body: RequestBody? = null)
Aruguments | Description |
---|---|
endpoint | Provide endpoint for API call. Require a non-null value. |
responseCallback | Provide callback for API execution. Require a non-null value. |
authenticationType | Provide token type for different API calls. Default type is set to RequestAuthenticationType.AUTHORIZATION_BEARER . |
method | Provide a HTTP method. Default method is set to GET . |
params | Provide a dictionary HTTP parameter. Default method is set to null. |
body | Provide a HTTP request body. Default method is set to null. |
Example:
- Construct the call by
init()
Request().init("endpoint", object : Callback { override fun onFailure(call: Call?, e: IOException?) { runOnUiThread { // implement UI logic here } } override fun onResponse(call: Call?, response: Response?) { // get response json text val responseText = response?.body()?.string() runOnUiThread { // implement UI logic here } } })
- Construct the call step by step
val request = Request() request.setEndPoint("endpoint") request.setMethod("GET", null) request.start(object : Callback { override fun onFailure(call: Call?, e: IOException?) { runOnUiThread { // implement UI logic here } } override fun onResponse(call: Call?, response: Response?) { // get response json text val response = response?.body()?.string() runOnUiThread { // implement UI logic here } } })
- Method
init()
has default value for some arguments, to pass value into arguments without following the order of method definition, need to pass as the formatargument = value
// the first default argument is authenticationType, to skip it and not follow the order in method definition
// need to pass as the format `argument = value`
Request().init("endpoit", callback, method = "GET")