This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Update client to new endpoints * Update existing endpoints in server * Implement getExchanges server endpoint * Pass filter to ExchangeApi.getExchanges * PUT * dry * PUT and other fix * Fix test
- Loading branch information
Diane Huxley
authored
Mar 20, 2024
1 parent
d43ff3f
commit 926170b
Showing
14 changed files
with
391 additions
and
94 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
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
95 changes: 95 additions & 0 deletions
95
httpserver/src/main/kotlin/tbdex/sdk/httpserver/handlers/GetExchange.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,95 @@ | ||
package tbdex.sdk.httpserver.handlers | ||
|
||
import io.ktor.http.HttpHeaders | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.ApplicationCall | ||
import io.ktor.server.response.respond | ||
import tbdex.sdk.httpclient.RequestToken | ||
import tbdex.sdk.httpclient.models.ErrorDetail | ||
import tbdex.sdk.httpserver.models.ErrorResponse | ||
import tbdex.sdk.httpserver.models.ExchangesApi | ||
import tbdex.sdk.httpserver.models.GetCallback | ||
import tbdex.sdk.httpserver.models.GetExchangesFilter | ||
import tbdex.sdk.protocol.models.Message | ||
|
||
/** | ||
* Get exchanges response | ||
* | ||
* @property data list of exchanges (list of tbdex messages) | ||
*/ | ||
class GetExchangeResponse( | ||
val data: List<Message>? | ||
) | ||
|
||
/** | ||
* Get exchanges | ||
* | ||
* @param call Ktor server application call | ||
* @param exchangesApi Exchanges API interface | ||
* @param callback Callback function to be invoked | ||
*/ | ||
@Suppress("SwallowedException") | ||
suspend fun getExchange( | ||
call: ApplicationCall, | ||
exchangesApi: ExchangesApi, | ||
callback: GetCallback?, | ||
pfiDid: String | ||
) { | ||
val authzHeader = call.request.headers[HttpHeaders.Authorization] | ||
if (authzHeader == null) { | ||
call.respond( | ||
HttpStatusCode.Unauthorized, | ||
ErrorResponse( | ||
errors = listOf( | ||
ErrorDetail( | ||
detail = "Authorization header required" | ||
) | ||
) | ||
) | ||
) | ||
return | ||
} | ||
|
||
val arr = authzHeader.split("Bearer ") | ||
if (arr.size != 2) { | ||
call.respond( | ||
HttpStatusCode.Unauthorized, | ||
ErrorResponse( | ||
errors = listOf( | ||
ErrorDetail( | ||
detail = "Malformed Authorization header. Expected: Bearer TOKEN_HERE" | ||
) | ||
) | ||
) | ||
) | ||
return | ||
} | ||
|
||
val token = arr[1] | ||
val requesterDid: String | ||
try { | ||
requesterDid = RequestToken.verify(token, pfiDid) | ||
} catch (e: Exception) { | ||
call.respond( | ||
HttpStatusCode.Unauthorized, | ||
ErrorResponse( | ||
errors = listOf( | ||
ErrorDetail( | ||
detail = "Could not verify Authorization header." | ||
) | ||
) | ||
) | ||
) | ||
return | ||
} | ||
|
||
val exchanges = exchangesApi.getExchange(call.parameters["exchangeId"]!!) | ||
|
||
if (callback != null) { | ||
// TODO: figure out what to do with callback result. should we pass through the exchanges we've fetched | ||
// and allow the callback to modify what's returned? (issue #10) | ||
val result = callback.invoke(call, GetExchangesFilter()) | ||
} | ||
|
||
call.respond(HttpStatusCode.OK, GetExchangeResponse(data = exchanges)) | ||
} |
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
Oops, something went wrong.