APIRequest

open class APIRequest

The APIRequest class is useful to send and manage any "API" requests and their responses

Author

N7ghtm4r3 - Tecknobit

Since

1.0.0

Constructors

Link copied to clipboard
constructor(enableCertificatesValidation: Boolean)
Constructor to init APIRequest
constructor(defaultErrorResponse: String)
Constructor to init APIRequest
constructor(defaultErrorResponse: String, enableCertificatesValidation: Boolean)
Constructor to init APIRequest
constructor(requestTimeout: Long)
Constructor to init APIRequest
constructor(requestTimeout: Long, enableCertificatesValidation: Boolean)
Constructor to init APIRequest
constructor(defaultErrorResponse: String, requestTimeout: Long)
Constructor to init APIRequest
constructor(defaultErrorResponse: String, requestTimeout: Long, enableCertificatesValidation: Boolean)
Constructor to init APIRequest
constructor()
Constructor to init APIRequest No-any params required

Types

Link copied to clipboard
open class Headers
The Headers class is useful to assemble headers values for the request
Link copied to clipboard
open class Params
The Params class is useful to assemble params values for the request
Link copied to clipboard
RequestMethod list of available request methods

Properties

Link copied to clipboard
private open var contentType: MediaType
contentType is the type of the content of the payload request, default is "text/plain"
Link copied to clipboard
val DEFAULT_ERROR_RESPONSE: String = "Error is not in api request, check out your code"
DEFAULT_ERROR_RESPONSE is constant that contains default error message if user not custom it
Link copied to clipboard
DEFAULT_REQUEST_TIMEOUT is constant that contains default request timeout if user not custom it
Link copied to clipboard
private open var defaultErrorResponse: String
defaultErrorResponse is the instance that contains default error message
Link copied to clipboard
enableCertificatesValidation whether enable the SSL certificates validation, this for example when the certificate is a self-signed certificate to by-pass
Link copied to clipboard
errorResponse is the instance that contains error message from request
Link copied to clipboard
val HMAC_SHA256_ALGORITHM: String = "HmacSHA256"
HMAC_SHA256_ALGORITHM is constant that contains "HMAC SHA 256" algorithm type
Link copied to clipboard
val HMAC_SHA512_ALGORITHM: String = "HmacSHA512"
HMAC_SHA512_ALGORITHM is constant that contains "HMAC SHA 512" algorithm type
Link copied to clipboard
private open var isSuccessfulRequest: Boolean
isSuccessfulRequest whether the request has been successful
Link copied to clipboard
val MD5_ALGORITHM: String = "MD5"
MD5_ALGORITHM is constant that contains "MD5" algorithm type
Link copied to clipboard
private open var okHttpClient: OkHttpClient
**okHttpClient** -> the http client to execute the requests
Link copied to clipboard
private open var request: Request.Builder
request is the instance useful for the requests
Link copied to clipboard
open val response: String
response is the instance that contains response message from request to return
Link copied to clipboard
val SHA1_ALGORITHM: String = "SHA-1"
SHA1_ALGORITHM is constant that contains "SHA-1" algorithm type
Link copied to clipboard
val SHA256_ALGORITHM: String = "SHA-256"
SHA256_ALGORITHM is constant that contains "SHA-256" algorithm type
Link copied to clipboard
private open var statusCode: Int
statusCode status code of a request

Functions

Link copied to clipboard
private open fun <T> addHeader(headerKey: String, headerValue: T)
Method to set the headers of an "HTTP" request
Link copied to clipboard
fun <T> assembleParamsList(separator: String, params: Array<T>): String
open fun <T> assembleParamsList(separator: String, params: ArrayList<T>): String
Method to concatenate a list of params
Link copied to clipboard
fun <T> assembleSeparatedParamsList(starterSeparator: String, enderSeparator: String, params: Array<T>): String
open fun <T> assembleSeparatedParamsList(starterSeparator: String, enderSeparator: String, params: ArrayList<T>): String
Method to concatenate a list of params
Link copied to clipboard
open fun base64Digest(data: Array<Byte>, algorithm: String): String
open fun base64Digest(data: String, algorithm: String): String
Method to get digest
Link copied to clipboard
private open fun checkToCreateDirs(pathName: String)
Method to check if the pathname (without the name and the suffix of the file to save) given by the user exists then if not exists create that pathname
Link copied to clipboard
fun <T> concatenateParamsList(initialChar: String, key: String, params: Array<T>): String
open fun <T> concatenateParamsList(initialChar: String, key: String, params: ArrayList<T>): String
Method to concatenate a series of same key param
Link copied to clipboard
Method to create a data URI scheme
Link copied to clipboard
open fun digest(data: Array<Byte>, algorithm: String): Array<Byte>
open fun digest(data: String, algorithm: String): Array<Byte>
Method to get digest
Link copied to clipboard
open fun downloadFile(url: String, pathName: String, save: Boolean): File
Method to download a file from an "URL" source
Link copied to clipboard
open fun enableCertificatesValidation(enableCertificatesValidation: Boolean)
Method to set programmatically enableCertificatesValidation value
Link copied to clipboard
open fun <T> encodeAdditionalParams(mandatoryParams: String, extraParams: APIRequest.Params): String
Method to assemble a query string params of an "HTTP" request
Link copied to clipboard
open fun <T> encodeBodyParams(bodyParams: APIRequest.Params): String
Method to assemble a body params of an "HTTP" request
Link copied to clipboard
open fun <T> encodeQueryParams(queryParams: APIRequest.Params): String
Method to assemble a query params for an "HTTP" request
Link copied to clipboard
open fun getBase64Signature(signatureKey: Array<Byte>, data: String, algorithm: String): String
open fun getBase64Signature(signatureKey: String, data: String, algorithm: String): String
Method to get params signature for an "HTTP" request
Link copied to clipboard
open fun <T> getJSONErrorResponse(): T
Method to get the error response of the request No-any params required
Link copied to clipboard
open fun <T> getJSONResponse(): T
Method to get the response of the request No-any params required
Link copied to clipboard
Method to get status response code of the request No-any params required
Link copied to clipboard
open fun getSignature(signatureKey: String, data: String, algorithm: String): String
Method to get params signature for an "HTTP" request
Link copied to clipboard
open fun isSuccessful(): Boolean
Method to get whether the response has been successful No-any params required
Link copied to clipboard
private open fun <T> parseResponseAsJson(response: String): T
Method to parse the response of the request and format it as "JSON"
Link copied to clipboard
private open fun performRequest()
Method to get the response of an "HTTP" request No-any params required
Link copied to clipboard
Method to print the error response of the request No-any params required
Link copied to clipboard
Method to print the error response of the request No-any params required
Link copied to clipboard
open fun sendAPIRequest(requestUrl: String, method: APIRequest.RequestMethod)
open fun sendAPIRequest(requestUrl: String, method: APIRequest.RequestMethod, queryParams: APIRequest.Params)
Method to send an api request
open fun sendAPIRequest(requestUrl: String, method: APIRequest.RequestMethod, headers: APIRequest.Headers)
open fun sendAPIRequest(requestUrl: String, method: APIRequest.RequestMethod, headers: APIRequest.Headers, queryParams: APIRequest.Params)
Method to send an api request with different headers
open fun <T> sendAPIRequest(requestUrl: String, method: APIRequest.RequestMethod, headerKey: String, headerValue: T)
open fun <T> sendAPIRequest(requestUrl: String, method: APIRequest.RequestMethod, headerKey: String, headerValue: T, queryParams: APIRequest.Params)
Method to send an api request with a single header
Link copied to clipboard
Method to send an api request with a payload formatted in "JSON"
Method to send an api request with a payload formatted in "JSON" and with different headers
open fun <T> sendJSONPayloadedAPIRequest(requestUrl: String, method: APIRequest.RequestMethod, headerKey: String, headerValue: T, payload: APIRequest.Params)
Method to send an api request with a payload formatted in "JSON" and with a single header
Link copied to clipboard
open fun sendPayloadedAPIRequest(requestUrl: String, method: APIRequest.RequestMethod, payload: MultipartBody)
Method to send an api request with a payload
open fun sendPayloadedAPIRequest(requestUrl: String, method: APIRequest.RequestMethod, headers: APIRequest.Headers, payload: MultipartBody)
Method to send an api request with a payload and with different headers
open fun <T> sendPayloadedAPIRequest(requestUrl: String, method: APIRequest.RequestMethod, headerKey: String, headerValue: T, payload: APIRequest.Params)
open fun <T> sendPayloadedAPIRequest(requestUrl: String, method: APIRequest.RequestMethod, headerKey: String, headerValue: T, payload: MultipartBody)
Method to send an api request with a payload and with a single header
Link copied to clipboard
open fun setConnectionTimeout(requestTimeout: Long)
Method to set programmatically timeout for the request
Link copied to clipboard
open fun setContentType(contentType: String)
Method to set the content type of the payload of the request
Link copied to clipboard
open fun setDefaultErrorResponse(defaultErrorResponse: String)
Method to set programmatically default error message to return if is not request error
Link copied to clipboard
private open fun setHeaders(headers: APIRequest.Headers)
Method to set the headers of an "HTTP" request
Link copied to clipboard
private open fun setRequest(requestUrl: String, method: APIRequest.RequestMethod)
private open fun setRequest(requestUrl: String, method: APIRequest.RequestMethod, payload: MultipartBody)
private open fun setRequest(requestUrl: String, method: APIRequest.RequestMethod, payload: APIRequest.Params, isJsonPayload: Boolean)
Method to set up the request details
Link copied to clipboard
open fun stringDigest(data: Array<Byte>, algorithm: String): String
open fun stringDigest(data: String, algorithm: String): String
Method to get digest
Link copied to clipboard
open fun toString(): String
Returns a string representation of the object No-any params required
Link copied to clipboard
Method to validate a self-signed SLL certificate and bypass the checks of its validity No-any params required