Skip to content

Commit

Permalink
Remove wrapper from output of endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Oct 19, 2023
1 parent 31b0ab1 commit b2a6e0a
Show file tree
Hide file tree
Showing 15 changed files with 27 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "amp-client"
description = "The Amphitheatre API client for Rust"
version = "0.3.3"
version = "0.4.0"
edition = "2021"
license = "Apache-2.0"
homepage = "https://amphitheatre.app"
Expand Down
6 changes: 2 additions & 4 deletions src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use amp_common::http::{Client, Endpoint, HTTPError};
use serde::{Deserialize, Serialize};

use crate::Wrapper;

#[derive(Debug, Deserialize, Serialize)]
pub struct Account {
/// The account ID
Expand All @@ -34,7 +32,7 @@ pub struct Account {
struct AccountEndpoint;

impl Endpoint for AccountEndpoint {
type Output = Wrapper<Account>;
type Output = Account;
}

/// The Accounts Service handles the account endpoint of the Amphitheatre API.
Expand All @@ -58,6 +56,6 @@ impl Accounts<'_> {
/// ```
pub fn me(&self) -> Result<Account, HTTPError> {
let res = self.client.get::<AccountEndpoint>("/me", None)?;
Ok(res.data.unwrap().data)
Ok(res.data.unwrap())
}
}
16 changes: 7 additions & 9 deletions src/actors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ use amp_common::sync::Synchronization;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::Wrapper;

#[derive(Debug, Deserialize, Serialize)]
pub struct Actor {
/// The actor id
Expand All @@ -38,19 +36,19 @@ pub struct Actor {
struct ActorEndpoint;

impl Endpoint for ActorEndpoint {
type Output = Wrapper<Actor>;
type Output = Actor;
}

struct ActorsEndpoint;

impl Endpoint for ActorsEndpoint {
type Output = Wrapper<Vec<Actor>>;
type Output = Vec<Actor>;
}

struct ValueEndpoint;

impl Endpoint for ValueEndpoint {
type Output = Wrapper<Value>;
type Output = Value;
}

/// The Actors Service handles the actors endpoint of the Amphitheatre API.
Expand All @@ -75,7 +73,7 @@ impl Actors<'_> {
) -> Result<Vec<Actor>, HTTPError> {
let path = format!("/playbooks/{}/actors", playbook_id);
let res = self.client.get::<ActorsEndpoint>(&path, options)?;
Ok(res.data.unwrap().data)
Ok(res.data.unwrap())
}

/// Retrieve a actor
Expand All @@ -87,7 +85,7 @@ impl Actors<'_> {
pub fn get(&self, pid: &str, name: &str) -> Result<Actor, HTTPError> {
let path = format!("/actors/{}/{}", pid, name);
let res = self.client.get::<ActorEndpoint>(&path, None)?;
Ok(res.data.unwrap().data)
Ok(res.data.unwrap())
}

/// Retrieve the log streams of actor
Expand All @@ -110,7 +108,7 @@ impl Actors<'_> {
pub fn info(&self, pid: &str, name: &str) -> Result<Value, HTTPError> {
let path = format!("/actors/{}/{}/info", pid, name);
let res = self.client.get::<ValueEndpoint>(&path, None)?;
Ok(res.data.unwrap().data)
Ok(res.data.unwrap())
}

/// Retrieve actor's stats
Expand All @@ -122,7 +120,7 @@ impl Actors<'_> {
pub fn stats(&self, pid: &str, name: &str) -> Result<Value, HTTPError> {
let path = format!("/actors/{}/{}/stats", pid, name);
let res = self.client.get::<ValueEndpoint>(&path, None)?;
Ok(res.data.unwrap().data)
Ok(res.data.unwrap())
}

/// Sync the actor's source code
Expand Down
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::playbooks::Playbooks;
/// Represents the Rust client for the Amphitheatre API
///
/// The client is your entrypoint to the Amphitheatre API. Using it you will be
/// able to call all the enpoints of the Amphitheatre API and their respective functions.
/// able to call all the endpoints of the Amphitheatre API and their respective functions.
///
/// # Examples
///
Expand Down
25 changes: 1 addition & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,10 @@
//
// Heavily inspired by https://github.com/dnsimple/dnsimple-rust

use serde::{Deserialize, Serialize};
// use serde::{Deserialize, Serialize};

pub mod accounts;
pub mod actors;
pub mod client;
pub mod oauth;
pub mod playbooks;

#[derive(Debug, Deserialize, Serialize)]
pub struct Wrapper<T> {
pub data: T,
/// Any API endpoint that returns a list of items requires pagination.
pub pagination: Option<Pagination>,
}

/// Any API endpoint that returns a list of items requires pagination.
/// By default we will return 30 records from any listing endpoint. If an API
/// endpoint returns a list of items, then it will include a pagination object
/// that contains pagination information.
#[derive(Serialize, Deserialize, Debug)]
pub struct Pagination {
/// The page currently returned (default: 1)
pub current_page: u64,
/// The number of entries returned per page (default: 30)
pub per_page: u64,
/// The Total number of entries available in the entrire collection.
pub total_entries: u64,
/// The total number of pages available given the current `per_page` value
pub total_pages: u64,
}
14 changes: 6 additions & 8 deletions src/playbooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use amp_common::{
};
use serde::{Deserialize, Serialize};

use crate::Wrapper;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Playbook {
/// The playbook ID in Amphitheatre.
Expand Down Expand Up @@ -49,13 +47,13 @@ pub struct PlaybookPayload {
struct PlaybookEndpoint;

impl Endpoint for PlaybookEndpoint {
type Output = Wrapper<Playbook>;
type Output = Playbook;
}

struct PlaybooksEndpoint;

impl Endpoint for PlaybooksEndpoint {
type Output = Wrapper<Vec<Playbook>>;
type Output = Vec<Playbook>;
}

/// The Playbooks Service handles the playbooks endpoint of the Amphitheatre API.
Expand All @@ -74,7 +72,7 @@ impl Playbooks<'_> {
/// - Sort: `id`, `label`, `email`
pub fn list(&self, options: Option<HashMap<String, String>>) -> Result<Vec<Playbook>, HTTPError> {
let res = self.client.get::<PlaybooksEndpoint>("/playbooks", options)?;
Ok(res.data.unwrap().data)
Ok(res.data.unwrap())
}

/// Create a playbook in the account.
Expand All @@ -87,7 +85,7 @@ impl Playbooks<'_> {
match serde_json::to_value(payload) {
Ok(json) => {
let res = self.client.post::<PlaybookEndpoint>("/playbooks", json)?;
Ok(res.data.unwrap().data)
Ok(res.data.unwrap())
}
Err(_) => Err(HTTPError::Deserialization(String::from(
"Cannot deserialize json payload",
Expand All @@ -103,7 +101,7 @@ impl Playbooks<'_> {
pub fn get(&self, pid: &str) -> Result<Playbook, HTTPError> {
let path = format!("/playbooks/{}", pid);
let res = self.client.get::<PlaybookEndpoint>(&path, None)?;
Ok(res.data.unwrap().data)
Ok(res.data.unwrap())
}

/// Update a playbook
Expand All @@ -118,7 +116,7 @@ impl Playbooks<'_> {
match serde_json::to_value(payload) {
Ok(json) => {
let res = self.client.patch::<PlaybookEndpoint>(&path, json)?;
Ok(res.data.unwrap().data)
Ok(res.data.unwrap())
}
Err(_) => Err(HTTPError::Deserialization(String::from(
"Cannot deserialize json payload",
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/v1/api/accounts/get-me-success.http
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ X-Request-Id: 15a7f3a5-7ee5-4e36-ac5a-8c21c2e1fffd
X-Runtime: 0.141588
Strict-Transport-Security: max-age=31536000

{"data":{"id":1,"email":"[email protected]","name":"example-account","created_at":"2015-09-18T23:04:37Z","updated_at":"2016-06-09T20:03:39Z"}}
{"id":1,"email":"[email protected]","name":"example-account","created_at":"2015-09-18T23:04:37Z","updated_at":"2016-06-09T20:03:39Z"}
2 changes: 1 addition & 1 deletion tests/fixtures/v1/api/actors/get-actor-info-success.http
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"data":{"environments":{"K3S_TOKEN":"RdqNLMXRiRsHJhmxKurR","K3S_KUBECONFIG_OUTPU":"/output/kubeconfig.yaml","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/bin/aux","CRI_CONFIG_FILE":"/var/lib/rancher/k3s/agent/etc/crictl.yaml"},"mounts":{"/VAR/LIB/RANCHER/K3S":"/var/lib/docker/volumes/a78bcb9f7654701e0cfaef4447ef61ced4864e5b93dee7102ec639afb5cf2e1d/_data","/VAR/LOG":"/var/lib/docker/volumes/f64c2f2cf81cfde89879f2a17924b31bd2f2e6a6a738f7df949bf6bd57102d25/_data","/VAR/LIB/CNI":"/var/lib/docker/volumes/00f49631b07ccd74de44d3047d5f889395ac871e05b622890b6dd788d34a59f4/_data","/VAR/LIB/KUBELET":"/var/lib/docker/volumes/bc1b16d39a0e204841695de857122412cfdefd0f672af185b1fa43e635397848/_data"},"port":{"6443/tcp":"0.0.0.0:42397"}}}
{"environments":{"K3S_TOKEN":"RdqNLMXRiRsHJhmxKurR","K3S_KUBECONFIG_OUTPU":"/output/kubeconfig.yaml","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/bin/aux","CRI_CONFIG_FILE":"/var/lib/rancher/k3s/agent/etc/crictl.yaml"},"mounts":{"/VAR/LIB/RANCHER/K3S":"/var/lib/docker/volumes/a78bcb9f7654701e0cfaef4447ef61ced4864e5b93dee7102ec639afb5cf2e1d/_data","/VAR/LOG":"/var/lib/docker/volumes/f64c2f2cf81cfde89879f2a17924b31bd2f2e6a6a738f7df949bf6bd57102d25/_data","/VAR/LIB/CNI":"/var/lib/docker/volumes/00f49631b07ccd74de44d3047d5f889395ac871e05b622890b6dd788d34a59f4/_data","/VAR/LIB/KUBELET":"/var/lib/docker/volumes/bc1b16d39a0e204841695de857122412cfdefd0f672af185b1fa43e635397848/_data"},"port":{"6443/tcp":"0.0.0.0:42397"}}
2 changes: 1 addition & 1 deletion tests/fixtures/v1/api/actors/get-actor-stats-success.http
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"data":{"CPU USAGE":"1.98%","DISK READ/WRITE":"5.3MB / 43.7 MB","MEMORY USAGE":"65.8MB","NETWORK I/O":"5.7 kB / 3 kB"}}
{"CPU USAGE":"1.98%","DISK READ/WRITE":"5.3MB / 43.7 MB","MEMORY USAGE":"65.8MB","NETWORK I/O":"5.7 kB / 3 kB"}
2 changes: 1 addition & 1 deletion tests/fixtures/v1/api/actors/get-actor-success.http
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"data":{"id":1,"title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}}
{"id":1,"title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}
2 changes: 1 addition & 1 deletion tests/fixtures/v1/api/actors/list-actors-success.http
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"data":[{"id":1,"title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"},{"id":2,"title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}]}
[{"id":1,"title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"},{"id":2,"title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}]
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"data":{"id":"1","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}}
{"id":"1","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}
2 changes: 1 addition & 1 deletion tests/fixtures/v1/api/playbooks/get-playbook-success.http
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"data":{"id":"1","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}}
{"id":"1","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"data":[{"id":"1","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"},{"id":"2","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}]}
[{"id":"1","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"},{"id":"2","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}]
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"data":{"id":"1","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}}
{"id":"1","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}

0 comments on commit b2a6e0a

Please sign in to comment.