-
Notifications
You must be signed in to change notification settings - Fork 67
Working with the SDK
Resource classes represent blocks of related API endpoints, as described in the API docs. Each resource class is accessible via Coinbase
object and lazily initialized.
Here is an example of how to use user resource from CoinbaseSDK:
let userResource = coinbase.userResource
userResource.current { result in
switch result {
case .success(let user):
// ...
case .failure(let error):
// ...
}
}
All request methods expect completion closure parameter that takes result represented by a generic enum Result<Value>
when the request is completed.
Most of the requests return Result
containing only parsed model (Result<User>
). But for endpoints returning an object list, data will be presented as full response model (e.g. Result<ResponseModel<[Address]>>
) with additional information (see next section).
All GET endpoints which return an object list support cursor based pagination with pagination information presented by a pagination object (more info here).
For this type of endpoints Result
will be associated with ResponseModel<T>
type. This type has a pagination
field that contains a current page information (limit, previous and next item ids, order).
Every paginated request has PaginationParameters
. If not provided defaults are applied.
coinbase.accountResource.list { result in
switch result {
case .success(let responseModel):
// array of accounts can be accessed as responseModel.data
// ...
case .failure(let error):
// ...
}
}
Otherwise, you might specify pagination parameters:
let paginationParameters = PaginationParameters(limit: 50,
order: .asc)
coinbase.accountResource.list(page: paginationParameters) { result in
// ...
}
It is possible to retrieve pagination parameters for next and previous pages from response Pagination
object (order and limit will remain as they were set in original request):
if let nextPage = responseModel.pagination?.nextPage {
coinbase.accountResource.list(page: nextPage) { result in
// ...
}
}
If you reach the last page nextPage
will return nil
.
The same applies for previousPage
.
In case of error Result.failure
is passed to the request completion closure. This enum case has an associated error that caused the request to fail.
An associated error can be one of the next types:
NetworkError
OAuthError
ResponseSerializationError
DecodingError
- other error types.
API errors (server responded with the status code different from 2XX)
are represented by OAuthError.responseError
for TokenResource
and NetworkError.responseError
for other recources.
NetworkError.responseError
case has an associated instance of ErrorResponse
that contains an array of ErrorModel
.
OAuthError.responseError
case has an associated instace of OAuthErrorResponse
that contains the error ID and human-readable error description.