-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(BE-122): retrieve-the-authenticated-user
- Loading branch information
Showing
7 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
shared/src/commonMain/kotlin/com/snacks/lemonsqueezy/api/UsersApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
shared/src/jvmTest/kotlin/com/snacks/lemonsqueezy/api/UsersApiTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |