Skip to content

Commit

Permalink
fix(#76-support-auth-header): added interface and new extension fun
Browse files Browse the repository at this point in the history
  • Loading branch information
y9Kap committed Oct 21, 2023
1 parent 01bb7df commit 88580cf
Show file tree
Hide file tree
Showing 27 changed files with 68 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

package app.meetacy.sdk.engine.ktor.requests.auth

import app.meetacy.sdk.engine.ktor.requests.extencion.postWithoutToken
import app.meetacy.sdk.engine.ktor.response.models.GenerateIdentityResponse
import app.meetacy.sdk.engine.requests.GenerateAuthRequest
import app.meetacy.sdk.types.annotation.UnsafeConstructor
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.url.Url
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.http.content.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
Expand All @@ -28,15 +25,7 @@ internal class AuthEngine(
put("nickname", request.nickname)
}

val string = httpClient.post(url.string) {
setBody(
TextContent(
text = jsonObject.toString(),
contentType = ContentType.Application.Json
)
)
header("Api-Version", request.apiVersion.int.toString())
}.body<String>()
val string = postWithoutToken(url.string, jsonObject, httpClient, request)

val token = json.decodeFromString<GenerateIdentityResponse>(string).result

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package app.meetacy.sdk.engine.ktor.requests.extencion

import app.meetacy.sdk.engine.requests.MeRequestWithToken
import app.meetacy.sdk.engine.requests.MeetacyRequest
import app.meetacy.sdk.engine.requests.TokenProviderEmpty
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.http.content.*
import kotlinx.serialization.json.JsonObject

public suspend inline fun <T, R : MeRequestWithToken<T>> post(
urlString: String,
jsonObject: JsonObject,
httpClient: HttpClient,
request: R
): String {
return httpClient.post(urlString) {
setBody(
TextContent(
text = jsonObject.toString(),
contentType = ContentType.Application.Json
)
)
header("Token", request.token)
header("Api-Version", request.apiVersion.toString())
}.body<String>()
}

public suspend inline fun <T, R : TokenProviderEmpty<T>> postWithoutToken(
url: String,
jsonObject: JsonObject,
httpClient: HttpClient,
request: R
): String {
return httpClient.post(url) {
setBody(
TextContent(
text = jsonObject.toString(),
contentType = ContentType.Application.Json
)
)
header("Api-Version", request.apiVersion.toString())
}.body<String>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package app.meetacy.sdk.engine.ktor.requests.meetings

import app.meetacy.sdk.engine.ktor.mapToMeeting
import app.meetacy.sdk.engine.ktor.mapToUser
import app.meetacy.sdk.engine.ktor.requests.extencion.post
import app.meetacy.sdk.engine.ktor.response.models.CreateMeetingResponse
import app.meetacy.sdk.engine.ktor.response.models.EditMeetingResponse
import app.meetacy.sdk.engine.ktor.response.models.ListMeetingsResponse
import app.meetacy.sdk.engine.requests.*
import app.meetacy.sdk.engine.requests.CreateMeetingRequest
Expand Down Expand Up @@ -44,16 +46,7 @@ internal class MeetingsEngine(
put("pagingId", pagingId?.string)
}

val string = httpClient.post(url.string) {
setBody(
TextContent(
text = jsonObject.toString(),
contentType = ContentType.Application.Json
)
)
header("Authorization", token.string)
header("Api-Version", apiVersion.int.toString())
}.body<String>()
val string = post(url.string, jsonObject, httpClient, request)

val response = Json.decodeFromString<ListMeetingsResponse>(string)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.Invitation
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.Location
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.Meeting
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.Location
import kotlinx.serialization.Serializable






import kotlinx.serialization.SerialName


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.Meeting
import kotlinx.serialization.Serializable






import kotlinx.serialization.SerialName


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.User
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.User
import kotlinx.serialization.Serializable






import kotlinx.serialization.SerialName


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
package app.meetacy.sdk.engine.ktor.response.models

import kotlinx.serialization.Serializable






import kotlinx.serialization.SerialName


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.Meeting
import dev.icerock.moko.network.generated.models.User
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.ListFriendsResponseResult
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.User
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.Location
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.Meeting
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.ListMeetingParticipantsResponseResult
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.User
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.ListNotificationsResponseResult
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.Notification
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
*/
package app.meetacy.sdk.engine.ktor.response.models

import dev.icerock.moko.network.generated.models.Meeting
import dev.icerock.moko.network.generated.models.User
import kotlinx.serialization.Serializable


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
package app.meetacy.sdk.engine.ktor.response.models

import kotlinx.serialization.Serializable






import kotlinx.serialization.SerialName


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import app.meetacy.sdk.types.location.Location
import app.meetacy.sdk.types.meeting.Meeting

public class CreateMeetingRequest(
public val token: Token,
public override val token: Token,
public val title: String,
public val date: Date,
public val location: Location,
public val description: String?,
public val visibility: Meeting.Visibility,
public val fileId: FileId? = null
) : MeetacyRequest<CreateMeetingRequest.Response> {
) : MeetacyRequest<CreateMeetingRequest.Response>, MeRequestWithToken<CreateMeetingRequest.Response> {
public data class Response(val meeting: Meeting)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import app.meetacy.sdk.types.user.UserId
public data class DeleteFriendRequest(
val token: Token,
val friendId: UserId
) : SimpleMeetacyRequest
) : SimpleMeetacyRequest
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import app.meetacy.sdk.types.auth.Token

public data class GenerateAuthRequest(
val nickname: String
) : MeetacyRequest<GenerateAuthRequest.Response> {
) : MeetacyRequest<GenerateAuthRequest.Response>, TokenProviderEmpty<GenerateAuthRequest.Response> {
public data class Response(val token: Token)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import app.meetacy.sdk.types.paging.PagingId
import app.meetacy.sdk.types.paging.PagingResponse

public data class ListMeetingsHistoryRequest(
val token: Token,
override val token: Token,
val amount: Amount,
val pagingId: PagingId?
) : MeetacyRequest<ListMeetingsHistoryRequest.Response> {
) : MeetacyRequest<ListMeetingsHistoryRequest.Response>, MeRequestWithToken<ListMeetingsHistoryRequest.Response> {
public data class Response(val paging: PagingResponse<Meeting>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package app.meetacy.sdk.engine.requests

import app.meetacy.sdk.types.auth.Token

public sealed interface MeRequestWithToken<out T> : MeetacyRequest<T> {
public val token: Token
}

public interface TokenProviderEmpty<out T> : MeetacyRequest<T>


0 comments on commit 88580cf

Please sign in to comment.