Skip to content

Commit

Permalink
feat: authentication api (#11)
Browse files Browse the repository at this point in the history
* feat: add new authentication group/resource

* fix: lint

* feat: add new authentication api test

* docs: update README with authentication api
  • Loading branch information
onyxmueller authored Dec 22, 2024
1 parent a02bb4d commit 6250a3c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ To access the API, you'll need to pass your [Pinata API Key JWT](https://docs.pi
val pinataClient = PinataClient.get(PINATA_JWT_TOKEN, PINATA_GATEWAY)
```

### Authentication API

```kotlin
// Test your API keys and your ability to connect to the Pinata API
val testAuthResponse = pinataClient.authentication.test()
testAuthResponse.onSuccess { response ->
// Handle success
}.onError { code, message ->
// Handle error
}.onException { exception ->
// Handle exception
}
```

### Files API

```kotlin
Expand Down
3 changes: 2 additions & 1 deletion pinata/src/main/java/net/onyxmueller/pinata/Constant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.onyxmueller.pinata
object Constant {
private const val API_VERSION = "v3"
private const val BASE_DOMAIN = "pinata.cloud"
const val API_URL = "https://api.$BASE_DOMAIN/${API_VERSION}/"
const val API_BASE_URL = "https://api.$BASE_DOMAIN/"
const val API_URL = "$API_BASE_URL/${API_VERSION}/"
const val UPLOADS_URL = "https://uploads.$BASE_DOMAIN/$API_VERSION/"
}
27 changes: 27 additions & 0 deletions pinata/src/main/java/net/onyxmueller/pinata/PinataClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package net.onyxmueller.pinata

import android.net.Uri
import com.google.gson.GsonBuilder
import net.onyxmueller.pinata.Constant.API_BASE_URL
import net.onyxmueller.pinata.Constant.API_URL
import net.onyxmueller.pinata.Constant.UPLOADS_URL
import net.onyxmueller.pinata.adapters.ItemTypeAdapterFactory
import net.onyxmueller.pinata.adapters.PinataApiResponseCallAdapterFactory
import net.onyxmueller.pinata.authentication.AuthenticationApi
import net.onyxmueller.pinata.files.FileApiRequestHelper
import net.onyxmueller.pinata.files.FilesApi
import net.onyxmueller.pinata.files.Order
Expand All @@ -19,6 +21,9 @@ import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class PinataClient internal constructor(jwtToken: String, gatewayUrl: String) {
@Volatile
private var authenticationInstance: Authentication? = null

@Volatile
private var filesInstance: Files? = null

Expand All @@ -35,6 +40,16 @@ class PinataClient internal constructor(jwtToken: String, gatewayUrl: String) {
.build()
}

private val getAuthenticationApi: AuthenticationApi by lazy {
createRetrofit(
API_BASE_URL,
OkHttpClient()
.newBuilder()
.addInterceptor(AuthInterceptor(jwtToken))
.build(),
).create(AuthenticationApi::class.java)
}

private val getFilesApi: FilesApi by lazy {
createRetrofit(
API_URL,
Expand Down Expand Up @@ -65,6 +80,14 @@ class PinataClient internal constructor(jwtToken: String, gatewayUrl: String) {
}
}

val authentication: Authentication by lazy {
authenticationInstance ?: synchronized(this) {
authenticationInstance ?: Authentication(getAuthenticationApi).also {
authenticationInstance = it
}
}
}

val files: Files by lazy {
filesInstance ?: synchronized(this) {
filesInstance ?: Files(getFilesApi, getUploadsApi, gatewayUrl).also {
Expand All @@ -73,6 +96,10 @@ class PinataClient internal constructor(jwtToken: String, gatewayUrl: String) {
}
}

class Authentication internal constructor(private val authenticationApi: AuthenticationApi) {
suspend fun test() = authenticationApi.test()
}

class Files internal constructor(
private val filesApi: FilesApi,
private val uploadsApi: UploadsApi,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.onyxmueller.pinata.authentication

import net.onyxmueller.pinata.PinataApiResponse
import net.onyxmueller.pinata.authentication.model.AuthTestResponse
import retrofit2.http.GET

interface AuthenticationApi {
@GET("data/testAuthentication")
suspend fun test(): PinataApiResponse<AuthTestResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package net.onyxmueller.pinata.authentication.model

data class AuthTestResponse(var message: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.onyxmueller.pinata

import kotlinx.coroutines.test.runTest
import net.onyxmueller.pinata.authentication.AuthenticationApi
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
internal class AuthenticationApiTest : ApiAbstract<AuthenticationApi>() {
private lateinit var api: AuthenticationApi

@Before
fun initApi() {
api = createService(AuthenticationApi::class.java)
}

@Test
fun testAuthenticationApiTest() =
runTest {
enqueueResponse("test.json")

val response = api.test()
val responseBody = requireNotNull((response as PinataApiResponse.Success).data)

assertThat(responseBody.message, `is`("Congratulations! You are communicating with the Pinata API!"))
}
}
3 changes: 3 additions & 0 deletions pinata/src/test/resources/api-response/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "Congratulations! You are communicating with the Pinata API!"
}

0 comments on commit 6250a3c

Please sign in to comment.