Skip to content

Commit

Permalink
feat(BE-122): retrieve-the-authenticated-user
Browse files Browse the repository at this point in the history
  • Loading branch information
hanrw committed Nov 23, 2023
1 parent 2ff7e50 commit f63cf5f
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.snacks.lemonsqueezy.api.internal.ktor.internal.createHttpClient
import com.snacks.lemonsqueezy.api.internal.ktor.default
import getPlatform

class LemonSqueezyApi(token: String) : LicenseApi by LemonSqueezyLicenseApi(
class LemonSqueezyApi(token: String) : LicenseKeysApi by LemonSqueezyLicenseKeysApi(
HttpRequester.default(
createHttpClient(
url = BASE_URL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.serialization.json.Json

interface LicenseApi {
interface LicenseKeysApi {
suspend fun activeLicense(licenseKey: String, instanceName: String): LicenseActivationResult

suspend fun deactivateLicense(licenseKey: String, instanceId: String): LicenseDeactivationResponse
}

internal class LemonSqueezyLicenseApi(
internal class LemonSqueezyLicenseKeysApi(
private val requester: HttpRequester,
) : LicenseApi {
) : LicenseKeysApi {
private val json = Json { ignoreUnknownKeys = true }

override suspend fun activeLicense(licenseKey: String, instanceName: String): LicenseActivationResult {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.snacks.lemonsqueezy.api

import com.snacks.lemonsqueezy.api.internal.ktor.HttpRequester
import com.snacks.lemonsqueezy.api.internal.ktor.performRequest
import com.snacks.lemonsqueezy.api.response.UserResponse
import io.ktor.client.request.*
import io.ktor.http.*

/**
* A user represents your personal user account that you use to log in to Lemon Squeezy.
*/
interface UsersApi {
suspend fun me(): UserResponse
}

class LemonSqueezyUsersApi(
private val requester: HttpRequester,
) : UsersApi {
override suspend fun me(): UserResponse {
return requester.performRequest<UserResponse> {
method = HttpMethod.Get
contentType(ContentType("application", "vnd.api+json"))
url(path = "/v1/users/me")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,18 @@ data class Meta(
val customerName: String,
@SerialName("customer_email")
val customerEmail: String,
)

data class UserAttributes(
val name: String,
val email: String,
val color: String,
@SerialName("avatar_url")
val avatarUrl: String,
@SerialName("has_custom_avatar")
val hasCustomAvatar: Boolean,
@SerialName("created_at")
val createdAt: String,
@SerialName("updated_at")
val updatedAt: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.snacks.lemonsqueezy.api.response
import com.snacks.lemonsqueezy.api.data.Instance
import com.snacks.lemonsqueezy.api.data.LicenseKey
import com.snacks.lemonsqueezy.api.data.Meta
import com.snacks.lemonsqueezy.api.data.UserAttributes
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.contracts.ExperimentalContracts
Expand Down Expand Up @@ -42,4 +43,26 @@ data class LicenseActivationErrorResponse(
@SerialName("license_key")
val licenseKey: LicenseKey,
val meta: Meta,
) : LicenseActivationResult()
) : LicenseActivationResult()


/**
* {
* "type": "users",
* "id": "1",
* "attributes": {
* "name": "Darlene Daugherty",
* "email": "[email protected]",
* "color": "#898FA9",
* "avatar_url": "https://www.gravatar.com/avatar/1ace5b3965c59dbcd1db79d85da75048?d=blank",
* "has_custom_avatar": false,
* "createdAt": "2021-05-24T14:08:31.000000Z",
* "updatedAt": "2021-08-26T13:24:54.000000Z"
* }
* }
*/
data class UserResponse(
val type: String,
val id: String,
val attributes: UserAttributes,
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.mockito.kotlin.argThat
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.whenever

class LicenseApiTest {
class LicenseKeysApiTest {
private var requester: HttpRequester = mock()

@Test
Expand Down Expand Up @@ -52,7 +52,7 @@ class LicenseApiTest {
)
)

val api = LemonSqueezyLicenseApi(requester)
val api = LemonSqueezyLicenseKeysApi(requester)
val licenseKey = "your_license_key"
val instanceId = "your_instance_id"

Expand Down Expand Up @@ -136,7 +136,7 @@ class LicenseApiTest {
)
)

val api = LemonSqueezyLicenseApi(requester)
val api = LemonSqueezyLicenseKeysApi(requester)
val licenseKey = "your_license_key"
val instanceName = "your_instance_name"

Expand Down Expand Up @@ -220,7 +220,7 @@ class LicenseApiTest {
)
)

val api = LemonSqueezyLicenseApi(requester)
val api = LemonSqueezyLicenseKeysApi(requester)
val licenseKey = "your_license_key"
val instanceName = "your_instance_name"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.snacks.lemonsqueezy.api

import com.snacks.lemonsqueezy.api.data.UserAttributes
import com.snacks.lemonsqueezy.api.internal.ktor.HttpRequester
import com.snacks.lemonsqueezy.api.response.UserResponse
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.util.reflect.*
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Test
import org.mockito.Mockito
import org.mockito.kotlin.argThat
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.whenever

class UsersApiTest {
private var requester: HttpRequester = Mockito.mock()

@Test
fun `should return me when success`() = runBlocking {
val httpRequestCaptor = argumentCaptor<HttpRequestBuilder.() -> Unit>()
val expectedResponse = UserResponse(
type = "users",
id = "some-user-id",
attributes = UserAttributes(
name = "some-user-name",
email = "some-user-email",
color = "#898FA9",
avatarUrl = "some-avatar-url",
hasCustomAvatar = false,
createdAt = "2021-05-24T14:08:31.000000Z",
updatedAt = "2021-08-26T13:24:54.000000Z"
)
)

whenever(
requester.performRequest<UserResponse>(
info = argThat<TypeInfo> {
this.type == UserResponse::class
},
builder = httpRequestCaptor.capture()
)
).thenReturn(expectedResponse)

val result = LemonSqueezyUsersApi(requester).me()

val httpRequestBuilder = HttpRequestBuilder()
httpRequestCaptor.firstValue.invoke(httpRequestBuilder)

assertEquals(expectedResponse, result)
assertEquals("application/vnd.api+json", httpRequestBuilder.contentType().toString())
assertEquals(HttpMethod.Get, httpRequestBuilder.method)
}
}

0 comments on commit f63cf5f

Please sign in to comment.