Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5894279
adding balance, fixing offerings. wip
Mar 26, 2024
0dba7c2
adding optional success field to close message
Mar 26, 2024
f21cdf0
fixing test data message and resource constructions. more tests are p…
Mar 26, 2024
35aa08d
in the middle of refactoring - all tests using test vectors are still…
Mar 27, 2024
a1d2f91
adding a todo
Mar 27, 2024
09f4762
Merge branch 'main' of github.com:TBD54566975/tbdex-kt into 207-proto…
Mar 29, 2024
494156b
refactoring method name in vector test
Mar 29, 2024
6a57244
bumping submodule commit
Mar 29, 2024
6b74577
adding ktdocs
Mar 29, 2024
7a0ae63
more ktdocs
Mar 29, 2024
18dd13f
adding balance and resource tests for balance. adding balance test ve…
Mar 29, 2024
ceadf77
adding new client method to fetch balances
Mar 29, 2024
0ce5957
wrote client tests
Mar 29, 2024
da2aafe
adding server endpoint for balance api
Mar 29, 2024
15d18d3
Merge branch 'main' of github.com:TBD54566975/tbdex-kt into 133-get-b…
Mar 30, 2024
8a31cda
adding detekt fixes. still 1 test failing due to rfq schema error
Mar 30, 2024
61a1446
Merge branch 'main' of github.com:TBD54566975/tbdex-kt into 133-get-b…
Mar 31, 2024
393e9eb
adding requesterDid to balanceApi.getBalance param. doing an auth tok…
Mar 31, 2024
1fad2ed
refactoring existing tests. add new test for getbalance
Mar 31, 2024
01d6a12
adding ktdoc for fakeBalancesApi#addBalance
Mar 31, 2024
9f90c3c
Apply suggestions from code review
Mar 31, 2024
0e8d498
adding try catch around parsing balance and offering
Mar 31, 2024
c67f00a
Update httpserver/src/main/kotlin/tbdex/sdk/httpserver/TbdexHttpServe…
Mar 31, 2024
43bf4c8
adding balancesEnabled config
Mar 31, 2024
f500e35
aligning with tbdex-js way of making balancesapi optional
Mar 31, 2024
c71048d
removed testserver
Mar 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adding new client method to fetch balances
  • Loading branch information
Jiyoon Koo committed Mar 29, 2024
commit ceadf772c75ca206896ce7fe19f3523eedaca863
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import tbdex.sdk.httpclient.models.GetExchangesFilter
import tbdex.sdk.httpclient.models.GetOfferingsFilter
import tbdex.sdk.httpclient.models.TbdexResponseException
import tbdex.sdk.protocol.Validator
import tbdex.sdk.protocol.models.Balance
import tbdex.sdk.protocol.models.Close
import tbdex.sdk.protocol.models.Message
import tbdex.sdk.protocol.models.Offering
Expand Down Expand Up @@ -72,6 +73,42 @@ object TbdexHttpClient {
}
}

/**
* Fetches balances from a PFI.
*
* @param pfiDid The decentralized identifier of the PFI.
* @param requesterDid The decentralized identifier of the entity requesting the balances.
* @return A list of [Balance] matching the request.
* @throws TbdexResponseException for request or response errors.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking out loud: in tbdex-js, we have both TbdexRequestError and TbdexResponseError, representing errors that happen before and after the client call respectively. It's a slight misnomer to throw a RequestException for both request and response errors, so I'm making a mental note that in my next tidy-up PR, I may explore introducing a TbdexRequestException class. In any case, it's relatively low priority and outside the scope of this PR.

*/
fun getBalances(pfiDid: String, requesterDid: BearerDid): List<Balance> {
val pfiServiceEndpoint = getPfiServiceEndpoint(pfiDid)
val baseUrl = "$pfiServiceEndpoint/balances/"
val requestToken = RequestToken.generate(requesterDid, pfiDid)

val request = Request.Builder()
.url(baseUrl)
.addHeader("Content-Type", JSON_HEADER)
.addHeader("Authorization", "Bearer $requestToken")
.get()
.build()

val response: Response = client.newCall(request).execute()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah does this throw an exception if the request fails? e.g. timeout, EHANGUP etc.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct. from the docs:

Throws:
IOException - if the request could not be executed due to cancellation, 
a connectivity problem or timeout. Because networks can fail during an exchange,
it is possible that the remote server accepted the request before the failure.
IllegalStateException - when the call has already been executed.

when {
response.isSuccessful -> {
val responseString = response.body?.string()
// response body is an object with a data field
val jsonNode = jsonMapper.readTree(responseString)
return jsonNode.get("data").elements()
.asSequence()
.map { Balance.parse(it.toString()) }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit/suggestion: Wrap this Balance.parse() in a try/catch that catches and re-throws as a TbdexResponseException.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

.toList()
}

else -> throw buildResponseException(response)
}
}

/**
* Send RFQ message to the PFI.
*
Expand Down