Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Update javadoc and some other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed Nov 19, 2023
1 parent b2ee59e commit de955f1
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 53 deletions.
59 changes: 29 additions & 30 deletions client/src/main/kotlin/dev/medzik/librepass/client/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,21 @@ import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException
import java.util.concurrent.TimeUnit

/**
* LibrePass API Servers
*/
/** LibrePass API Servers */
object Server {
/**
* Production server instance.
*/
/** Production server instance. */
const val PRODUCTION = "https://api.librepass.medzik.dev"

/**
* Test server instance. The database from this instance can be deleted at any time!
*/
/** Test server instance. The database from this instance can be deleted at any time! */
@Suppress("UNUSED")
const val TEST = "https://api.test.librepass.medzik.dev"
}

/**
* HTTP Client for sending requests to the API.
* @param apiURL api url address
* @param accessToken access token to use for authorization
*
* @param apiURL The API URL to use.
* @param accessToken The access token to use for authorization.
*/
class Client(
private val apiURL: String,
Expand All @@ -47,8 +42,9 @@ class Client(

/**
* Send a GET request to the API.
* @param endpoint endpoint of the API
* @return response body
*
* @param endpoint The API endpoint to send the request to.
* @return The response from the API.
*/
@Throws(ClientException::class, ApiException::class)
fun get(endpoint: String): String {
Expand All @@ -64,8 +60,9 @@ class Client(

/**
* Send a DELETE request to the API.
* @param endpoint endpoint of the API
* @return response body
*
* @param endpoint The API endpoint to send the request to.
* @return The response from the API.
*/
@Throws(ClientException::class, ApiException::class)
fun delete(endpoint: String): String {
Expand All @@ -81,9 +78,10 @@ class Client(

/**
* Send a DELETE request to the API.
* @param endpoint endpoint of the API
* @param json JSON body of the request
* @return response body
*
* @param endpoint The API endpoint to send the request to.
* @param json The JSON to send in the request body.
* @return The response from the API.
*/
@Throws(ClientException::class, ApiException::class)
fun delete(
Expand All @@ -104,9 +102,10 @@ class Client(

/**
* Send a POST request to the API.
* @param endpoint endpoint of the API
* @param json JSON body of the request
* @return response body
*
* @param endpoint The API endpoint to send the request to.
* @param json The JSON to send in the request body.
* @return The response from the API.
*/
@Throws(ClientException::class, ApiException::class)
fun post(
Expand All @@ -127,9 +126,10 @@ class Client(

/**
* Send a PATCH request to the API.
* @param endpoint endpoint of the API
* @param json JSON body of the request
* @return response body
*
* @param endpoint The API endpoint to send the request to.
* @param json The JSON to send in the request body.
* @return The response from the API.
*/
@Throws(ClientException::class, ApiException::class)
fun patch(
Expand All @@ -150,9 +150,10 @@ class Client(

/**
* Send a PUT request to the API.
* @param endpoint endpoint of the API
* @param json JSON body of the request
* @return response body
*
* @param endpoint The API endpoint to send the request to.
* @param json The JSON to send in the request body.
* @return The response from the API.
*/
@Throws(ClientException::class, ApiException::class)
fun put(
Expand All @@ -171,9 +172,7 @@ class Client(
return executeAndExtractBody(request)
}

/**
* Execute a request and extract the body from the response.
*/
/** Execute a request and extract the body from the response. */
@Throws(ClientException::class, ApiException::class)
private fun executeAndExtractBody(request: Request): String {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import dev.medzik.librepass.responses.ResponseError

/**
* Exception thrown when the API returns an error.
*
* @property status The HTTP Status code returned by the API.
* @property error The error message returned by the API.
*/
class ApiException(
val status: Number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

object JsonUtils {
/**
* Serialize an object to JSON string.
*/
/** Serialize an object to JSON string. */
fun serialize(data: Any): String = Gson().toJson(data)

/**
* Deserialize JSON string to object.
*/
/** Deserialize JSON string to object. */
inline fun <reified T> deserialize(data: String): T = Gson().fromJson(data, object : TypeToken<T>() {}.type)
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<packaging>pom</packaging>
<name>LibrePass</name>
<description>The core LibrePass infrastructure</description>
<url>https://librepass.medzik.dev</url>
<url>https://github.com/LibrePass/LibrePass-Server</url>

<developers>
<developer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.method.support.ModelAndViewContainer
import java.util.*

/**
* Annotation for getting authorized user from request.
* @see AuthorizedUserArgumentResolver
*/
/** Annotation for getting authorized user from request. */
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.ANNOTATION_CLASS)
annotation class AuthorizedUser

/** Implementation of the [AuthorizedUser] annotation */
@Component
class AuthorizedUserArgumentResolver
@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.method.support.ModelAndViewContainer

/**
* Annotation for getting request IP from request.
* @see RequestIPArgumentResolver
*/
/** Annotation for getting request IP from request. */
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.ANNOTATION_CLASS)
annotation class RequestIP

/** Implementation of the [RequestIP] annotation. */
@Component
class RequestIPArgumentResolver
@Autowired
constructor(
@Value("\${http.ip.header}") private val ipHeader: String
@Value("\${http.ip.header}")
private val ipHeader: String
) : HandlerMethodArgumentResolver {
override fun supportsParameter(parameter: MethodParameter): Boolean {
return parameter.hasParameterAnnotation(RequestIP::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package dev.medzik.librepass.responses

/**
* ResponseError represents the error responses from the API.
*
* @property statusCode The HTTP status code.
*/
enum class ResponseError(val statusCode: HttpStatus) {
INVALID_BODY(HttpStatus.BAD_REQUEST),
INVALID_CREDENTIALS(HttpStatus.UNAUTHORIZED),
Expand All @@ -18,6 +23,11 @@ enum class ResponseError(val statusCode: HttpStatus) {
UNEXPECTED_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR)
}

/**
* HttpStatus represents the HTTP error status codes that can be returned by the API.
*
* @property code The HTTP status code.
*/
enum class HttpStatus(val code: Int) {
BAD_REQUEST(400),
UNAUTHORIZED(401),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class DateAdapter : JsonSerializer<Date>, JsonDeserializer<Date> {
context: JsonDeserializationContext?
): Date {
return if (json != null) {
val timestamp = json.asLong * 1000 // Convert seconds to milliseconds
Date(timestamp)
Date(json.asLong * 1000) // Convert seconds to milliseconds
} else {
Date()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ data class TwoFactorRequest(
* @property userId The identifier of the user.
* @property apiKey The API key.
* @property verified If false, you need to authenticate the API key with OTP code to use the API. (Only if the user enabled
* 2FA authentication)
* 2FA authentication)
*/
data class UserCredentialsResponse(
val userId: UUID,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dev.medzik.librepass.types.cipher

/**
* CipherType is an enum class that represents the type of cipher.
*/
/** CipherType represents the type of cipher. */
enum class CipherType {
Login,
SecureNote,
Expand All @@ -11,7 +9,8 @@ enum class CipherType {
companion object {
/**
* Returns the [CipherType] from the given type integer.
* @param type The type of the cipher
*
* @param type The type of the cipher.
*/
fun from(type: Int) = values()[type]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ data class EncryptedCipher(
) {
/**
* Creates a new [EncryptedCipher] object from the [Cipher].
*
* @param cipher The [Cipher] to encrypt.
* @param secretKey The key to use for encryption.
* @return The encrypted cipher.
Expand Down

0 comments on commit de955f1

Please sign in to comment.