tallgrass/common/resource
Types
The type of response returned by paginated endpoints. count
is the total number of records for that type of resource, results
is a list of
fetcheable resources on the current page, and the next
and previous
links can be used to traverse the rest of the pages.
pub type PaginatedResource {
PaginatedResource(
count: Int,
next: Option(String),
previous: Option(String),
results: List(Resource),
)
}
Constructors
-
PaginatedResource( count: Int, next: Option(String), previous: Option(String), results: List(Resource), )
The majority of PokeAPI resources are of type NamedResource
, however there are a few of type Resource
that are composed only of a URL.
Because both variants include a URL, fetching the full resource is simply a matter of passing it to a fetch_resource
function.
Example
let client = client.new()
use res <- result.try(client |> pokemon.fetch())
let assert Ok(resource) = res.results |> list.first
// most resources will take this shape
let assert NamedResource(url, name) = resource
// matching on both variants is safer
case resource {
NamedResource(url, name) -> {todo} // do something with name
Resource(url) -> {todo} // do something with url
}
// fetch the full resource to get more than just the name and url
client |> pokemon.fetch_resource(resource)
pub type Resource {
Resource(url: String)
NamedResource(url: String, name: String)
}
Constructors
-
Resource(url: String)
-
NamedResource(url: String, name: String)