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,     val connectionErrorMessage: String,     val byPassSSLValidation: 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

byPassSSLValidation

Whether bypass the SSL certificates validation, this for example when is a self-signed the certificate USE WITH CAUTION

Constructors

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

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
protected val byPassSSLValidation: Boolean = false
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 var host: String
Link copied to clipboard
private val initHost: () -> Unit

`initHost** Method used to init correctly the host value

Link copied to clipboard
protected var interceptorAction: () -> Unit?

interceptorAction The callback of the interceptor to execute when a request has been sent, if not specified is `null** by default and no interceptions will be executed

Link copied to clipboard
protected val ktorClient: HttpClient

ktorClient the HTTP client used to send the stats and the performance data

Link copied to clipboard
private val loggerMutex: Mutex

loggerMutex the mutex used to log atomically the log messages if debugMode is true

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 var userId: String?
Link copied to clipboard
protected var userToken: String?

Functions

Link copied to clipboard
fun attachInterceptorOnRequest(interceptor: () -> Unit)

Method used to attach a new interceptor to the Requester to execute it when a request has been sent

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

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

Link copied to clipboard
open fun clearSession()

Method used to clear the current session of the requester, for example, after user logged out or changed the auth credentials

Link copied to clipboard
protected fun connectionErrorMessage(): JsonObject

Method used to set the RESPONSE_STATUS_KEY to send when an error during the connection occurred

Link copied to clipboard
protected fun createPaginationQuery(page: Int, pageSize: Int): JsonObject

Method used to create the query with the pagination parameters

Link copied to clipboard
protected suspend fun execDelete(    endpoint: String,     headers: Map<String, Any> = emptyMap(),     query: JsonObject? = null,     payload: JsonObject? = null): JsonObject

Method used to execute a RequestMethod.DELETE request to the backend

Link copied to clipboard
protected suspend fun execGet(endpoint: String, headers: Map<String, Any> = emptyMap(), query: JsonObject? = null): JsonObject

Method used to execute a RequestMethod.GET request to the backend

Link copied to clipboard
protected suspend fun execMultipartRequest(    endpoint: String,     headers: Map<String, Any> = emptyMap(),     query: JsonObject? = null,     payload: PartData): JsonObject
protected suspend fun execMultipartRequest(    endpoint: String,     headers: Map<String, Any> = emptyMap(),     query: JsonObject? = null,     payload: List<PartData>): JsonObject

Method used to execute a multipart request to the backend

Link copied to clipboard
protected suspend fun execPatch(    endpoint: String,     headers: Map<String, Any> = emptyMap(),     query: JsonObject? = null,     payload: JsonObject = JsonObject(emptyMap())): JsonObject

Method used to execute a RequestMethod.PATCH request to the backend

Link copied to clipboard
protected suspend fun execPost(    endpoint: String,     headers: Map<String, Any> = emptyMap(),     query: JsonObject? = null,     payload: JsonObject = JsonObject(emptyMap())): JsonObject

Method used to execute a RequestMethod.POST request to the backend

Link copied to clipboard
protected suspend fun execPut(    endpoint: String,     headers: Map<String, Any> = emptyMap(),     query: JsonObject? = null,     payload: JsonObject = JsonObject(emptyMap())): JsonObject

Method used to execute a RequestMethod.PUT request to the backend

Link copied to clipboard
private suspend fun execRequest(    method: RequestMethod,     endpoint: String,     headers: Map<String, Any>,     query: JsonObject? = null,     payload: JsonObject? = null): JsonObject

Method used to execute a request to the backend

Link copied to clipboard
protected fun interceptRequest()

Method used to execute the interceptorAction if it is specified by the attachInterceptorOnRequest method

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

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

Link copied to clipboard
private fun logHeaders(headers: Map<String, Any>)

Method used to log the current headers used in the requests

Link copied to clipboard
private fun logQuery(query: JsonObject?)

Method used to log the current query used in the request

Link copied to clipboard
private suspend fun logRequestInfo(    requestUrl: String,     headers: Map<String, Any>,     query: JsonObject?,     requestPayloadInfo: () -> Unit,     response: JsonObject?)

Method used to print the details of the request sent if the debugMode is enabled

Link copied to clipboard
private fun HttpRequestBuilder.prepareRequest(    headers: Map<String, Any>,     contentType: ContentType? = null,     query: JsonObject? = null,     payload: () -> Unit? = null)

Method used to prepare the details of the request to execute

Link copied to clipboard
private fun JsonObject.prettyPrint()

Method used to print a JsonObject request part in pretty format

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

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