Trait osauth::PaginatedResource
source · pub trait PaginatedResource {
type Id: Debug + Serialize + Send;
type Root: DeserializeOwned + Send;
// Required method
fn resource_id(&self) -> Self::Id;
}
Expand description
A single resource.
This trait can normally be derived. You need to
- add a
#[resource_id]
attribute to the field that serves as a pagination marker - add a
#[collection_name = "resources"]
attribute to the structure with a name of the field that is returned in the collection (e.g. “servers” for Compute servers). - add a
#[flat_collection]
attribute to the structure if the root collection is flat, the listing API returns an array instead of an object.
use osauth::PaginatedResource;
use serde::Deserialize;
#[derive(Debug, Deserialize, PaginatedResource)]
#[collection_name = "servers"]
pub struct Server {
#[resource_id]
pub id: String,
pub name: String,
}
The trait and its dependencies can be implemented manually like this:
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Server {
pub id: String,
pub name: String,
}
#[derive(Debug, Deserialize)]
pub struct ServersRoot {
pub servers: Vec<Server>, // equivalent of #[collection_name = "servers"]
}
// This implementatin defines the relationship between the root resource and its items.
impl osauth::PaginatedResource for Server {
type Id = String;
type Root = ServersRoot;
fn resource_id(&self) -> Self::Id {
self.id.clone() // equivalent of #[resource_id] on the `id` field
}
}
// This is another required part of the pagination contract.
impl From<ServersRoot> for Vec<Server> {
fn from(value: ServersRoot) -> Vec<Server> {
value.servers
}
}
Required Associated Types§
sourcetype Root: DeserializeOwned + Send
type Root: DeserializeOwned + Send
Root type of the listing.
Required Methods§
sourcefn resource_id(&self) -> Self::Id
fn resource_id(&self) -> Self::Id
Retrieve a copy of the ID.