Requester

abstract class Requester(var host: String, var userId: String? = null, var userToken: String? = null, var debugMode: Boolean = false, val connectionTimeout: Long = DEFAULT_REQUEST_TIMEOUT.toLong(), val connectionErrorMessage: String, val enableCertificatesValidation: Boolean = false)

The Requester class is useful to communicate with backend based on the SpringBoot framework

Author

N7ghtm4r3 - Tecknobit

Parameters

host

: the host address where is running the backend

userId

: the user identifier

userToken

: the user token

debugMode

: whether the requester is still in development and who is developing needs the log of the requester's workflow, if it is enabled all the details of the requests sent and the errors occurred will be printed in the console

connectionTimeout

: time to keep alive request then throw the connection refused error

connectionErrorMessage

: the error to send when a connection error occurred

enableCertificatesValidation

: whether enable the SSL certificates validation, this for example when the certificate is a self-signed certificate to by-pass

Inheritors

Constructors

Link copied to clipboard
constructor(host: String, userId: String? = null, userToken: String? = null, debugMode: Boolean = false, connectionTimeout: Long = DEFAULT_REQUEST_TIMEOUT.toLong(), connectionErrorMessage: String, enableCertificatesValidation: Boolean = false)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
protected val apiRequest: APIRequest

apiRequest -> the instance to communicate and make the requests to the backend

Link copied to clipboard
Link copied to clipboard
protected val connectionTimeout: Long
Link copied to clipboard
protected var debugMode: Boolean
Link copied to clipboard
protected val enableCertificatesValidation: Boolean = false
Link copied to clipboard
protected val headers: APIRequest.Headers

headers the headers used in the request

Link copied to clipboard
protected var host: String
Link copied to clipboard
private val initHost: () -> Unit

initHost function to init correctly the host value

Link copied to clipboard

mustValidateCertificates flag whether the requests must validate the SSL certificates, this for example when the SSL is a self-signed certificate

Link copied to clipboard
protected val timeFormatter: TimeFormatter

timeFormatter the formatter used to format the timestamp values

Link copied to clipboard
protected var userId: String?
Link copied to clipboard
protected var userToken: String?

Functions

Link copied to clipboard
open fun changeHost(host: String)

Function to change, during the runtime for example when the session changed, the host address to make the requests

Link copied to clipboard
protected fun connectionErrorMessage(): JSONObject

Function to set the RESPONSE_STATUS_KEY to send when an error during the connection occurred

Link copied to clipboard
protected fun execDelete(endpoint: String, payload: APIRequest.Params? = null): JSONObject

Function to execute a RequestMethod.DELETE request to the backend

Link copied to clipboard
protected fun execGet(endpoint: String): JSONObject

Function to execute a RequestMethod.GET request to the backend

Link copied to clipboard
protected fun execMultipartRequest(endpoint: String, body: MultipartBody): JSONObject

Function to exec a multipart body request

Link copied to clipboard
protected fun execPatch(endpoint: String, payload: APIRequest.Params = Params()): JSONObject

Function to execute a RequestMethod.PATCH request to the backend

Link copied to clipboard
protected fun execPost(endpoint: String, payload: APIRequest.Params = Params()): JSONObject

Function to execute a RequestMethod.POST request to the backend

Link copied to clipboard
protected fun execPut(endpoint: String, payload: APIRequest.Params = Params()): JSONObject

Function to execute a RequestMethod.PUT request to the backend

Link copied to clipboard
private fun execRequest(method: APIRequest.RequestMethod, endpoint: String, payload: APIRequest.Params? = null): JSONObject

Function to execute a request to the backend

Link copied to clipboard
private fun isSuccessfulResponse(response: JSONObject?): SocketManager.StandardResponseCode

Function to get whether the request has been successful or not

Link copied to clipboard
private fun logError(exception: Exception)

Function to print a log of an exception occurred during a request sent if the debugMode is enabled

Link copied to clipboard
private fun logHeaders()

Function to log the current headers used in the requests

Link copied to clipboard
private fun logRequestInfo(requestUrl: String, requestPayloadInfo: () -> Unit, response: JSONObject?)

Function to print the details of the request sent if the debugMode is enabled

Link copied to clipboard
fun sendRequest(request: () -> JSONObject, onResponse: (JsonHelper) -> Unit, onConnectionError: (JsonHelper) -> Unit? = null)
fun sendRequest(request: () -> JSONObject, onSuccess: (JsonHelper) -> Unit, onFailure: (JsonHelper) -> Unit, onConnectionError: (JsonHelper) -> Unit? = null)

Function to execute and manage the response of a request

Link copied to clipboard
fun setConnectionTimeout(connectionTimeout: Long)

Function to set programmatically timeout for the requests

Link copied to clipboard
fun setUserCredentials(userId: String?, userToken: String?)

Function to set the user credentials used to make the authenticated requests

Link copied to clipboard
private fun validateSelfSignedCertificate(okHttpClient: OkHttpClient): OkHttpClient

Method to validate a self-signed SLL certificate and bypass the checks of its validity

No-any params required