Skip to content

Commit

Permalink
docs: Add documentation for new code
Browse files Browse the repository at this point in the history
  • Loading branch information
itachi1706 committed Jan 30, 2024
1 parent 3bbf9c9 commit 1346ba3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,110 @@ import com.itachi1706.helperlib.objects.ApiResponse
import kotlinx.serialization.json.Json

@Suppress("unused")
/**
* API Call Handling class
* @param context Context
* @param baseUrl Base URL to use (Default: https://api.itachi1706.com)
* @param tag Tag to use for Volley Request Queue (Default: DEFAULT)
*/
class ApiCallsHelper(
context: Context,
private val baseUrl: String = DEFAULT_URL,
private val tag: String = "DEFAULT"
) {

// Set up volley request queue
/**
* Volley Request Queue
*/
private val queue: RequestQueue = Volley.newRequestQueue(context)

/**
* Cancels all requests with the tag
*/
fun cancelAllRequests() {
queue.cancelAll(tag)
}

/**
* Gets the current sequence number of the request queue
* @return Sequence Number
*/
fun getSequenceNumber(): Int {
return queue.sequenceNumber
}

/**
* Makes a GET Call to the specified path
* @param path Path to call
* @param listener Listener to handle the response on
* @see ApiCallListener
* @see ApiResponse
* @see StringRequest
* @see internalCallHandling
*/
fun makeGetCall(path: String, listener: ApiCallListener) {
// Make a HTTP GET Call to the URL and path with Volley
internalCallHandling(Request.Method.GET, path, null, listener)
}

/**
* Makes a POST Call to the specified path
* @param path Path to call
* @param data Data to send
* @param listener Listener to handle the response on
* @see ApiCallListener
* @see ApiResponse
* @see StringRequest
* @see internalCallHandling
*/
fun makePostCall(path: String, data: String?, listener: ApiCallListener) {
// Make a HTTP POST Call to the URL and path
internalCallHandling(Request.Method.POST, path, data, listener)
}

/**
* Makes a PUT Call to the specified path
* @param path Path to call
* @param data Data to send
* @param listener Listener to handle the response on
* @see ApiCallListener
* @see ApiResponse
* @see StringRequest
* @see internalCallHandling
*/
fun makePutCall(path: String, data: String?, listener: ApiCallListener) {
// Make a HTTP PUT Call to the URL and path
internalCallHandling(Request.Method.PUT, path, data, listener)
}

/**
* Makes a DELETE Call to the specified path
* @param path Path to call
* @param listener Listener to handle the response on
* @see ApiCallListener
* @see ApiResponse
* @see StringRequest
* @see internalCallHandling
*/
fun makeDeleteCall(path: String, listener: ApiCallListener) {
// Make a HTTP DELETE Call to the URL and path
internalCallHandling(Request.Method.DELETE, path, null, listener)
}

/**
* Makes a HTTP Call to the specified path
* @param method HTTP Method to use
* @param path Path to call
* @param data Data to send if any
* @param listener Listener to handle the response on
* @see ApiCallListener
* @see ApiResponse
* @see StringRequest
* @see internalCallHandling
*/
private fun internalCallHandling(
method: Int,
path: String,
data: String?,
listener: ApiCallListener
) {
// Make a HTTP DELETE Call to the URL and path
// Make a HTTP Call to the URL and path
val url = "$baseUrl/$path"

val successListener = Response.Listener { response: String ->
Expand Down Expand Up @@ -83,18 +143,35 @@ class ApiCallsHelper(
queue.add(request)
}

/**
* Serializes the response into an ApiResponse object
* @param response Response to serialize
* @return ApiResponse object
*/
private fun serializeResponse(response: String): ApiResponse {
return Json.decodeFromString<ApiResponse>(response)
}

/**
* Interface to handle API Calls
*/
interface ApiCallListener {
/**
* Called when API Call is successful
* @param response ApiResponse object
*/
fun onApiCallSuccess(response: ApiResponse)

/**
* Called when API Call fails
* @param error Error message
*/
fun onApiCallError(error: String)
}

companion object {
const val LOG_TAG = "ApiCallsHelper"
const val UNKNOWN_ERROR = "Unknown Error"
const val DEFAULT_URL = "https://api.itachi1706.com"
private const val LOG_TAG = "ApiCallsHelper"
private const val UNKNOWN_ERROR = "Unknown Error"
private const val DEFAULT_URL = "https://api.itachi1706.com"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ package com.itachi1706.helperlib.objects
import java.io.Serializable
import java.util.Date

/**
* API Response Object
* @constructor Creates an API Response Object
* @since 1.0.0
* @property date Date of the response
* @property status Status Code of the response
* @property message Message of the response
* @property success Success of the response
* @property data Data of the response
* @property error Error of the response
* @see Serializable
* @see Date
*/
data class ApiResponse(
val date: Date,
val status: Int,
Expand Down

0 comments on commit 1346ba3

Please sign in to comment.