Skip to content

Commit

Permalink
refactor: using resource::{PlaybookSpec, ActorSpec} instead of actor:…
Browse files Browse the repository at this point in the history
…:Actor and playbook::Playbook, bump version to 0.7.0
  • Loading branch information
wangeguo committed Jan 15, 2024
1 parent e84afe8 commit d9345e8
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 182 deletions.
4 changes: 2 additions & 2 deletions 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.6.4"
version = "0.7.0"
edition = "2021"
license = "Apache-2.0"
homepage = "https://amphitheatre.app"
Expand All @@ -11,7 +11,7 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.6.3" }
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.7.0" }
futures = "0.3"
reqwest-eventsource = "0.5.0"
serde = { version = "1.0", features = ["derive"] }
Expand Down
24 changes: 5 additions & 19 deletions src/actors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,21 @@
use std::collections::HashMap;

use amp_common::http::{Client, Endpoint, HTTPError};
use amp_common::resource::ActorSpec;
use amp_common::sync::Synchronization;
use reqwest_eventsource::EventSource;
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Actor {
/// The actor id
pub id: u64,
/// The title of the actor
pub title: String,
/// The description of the actor
pub description: String,
/// When the actor was created in Amphitheatre.
pub created_at: String,
/// When the actor was last updated in Amphitheatre.
pub updated_at: String,
}

struct ActorEndpoint;

impl Endpoint for ActorEndpoint {
type Output = Actor;
type Output = ActorSpec;
}

struct ActorsEndpoint;

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

struct ValueEndpoint;
Expand Down Expand Up @@ -71,7 +57,7 @@ impl Actors<'_> {
&self,
playbook_id: &str,
options: Option<HashMap<String, String>>,
) -> Result<Vec<Actor>, HTTPError> {
) -> Result<Vec<ActorSpec>, HTTPError> {
let path = format!("/playbooks/{}/actors", playbook_id);
let res = self.client.get::<ActorsEndpoint>(&path, options)?;
Ok(res.data.unwrap())
Expand All @@ -83,7 +69,7 @@ impl Actors<'_> {
///
/// `pid`: The ID of the playbook
/// `name`: The name of the actor
pub fn get(&self, pid: &str, name: &str) -> Result<Actor, HTTPError> {
pub fn get(&self, pid: &str, name: &str) -> Result<ActorSpec, HTTPError> {
let path = format!("/actors/{}/{}", pid, name);
let res = self.client.get::<ActorEndpoint>(&path, None)?;
Ok(res.data.unwrap())
Expand Down
28 changes: 7 additions & 21 deletions src/playbooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,10 @@ use std::collections::HashMap;

use amp_common::{
http::{Client, Endpoint, HTTPError},
resource::Preface,
resource::{PlaybookSpec, Preface},
};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Playbook {
/// The playbook ID in Amphitheatre.
pub id: String,
/// The title of the playbook.
pub title: String,
/// The description of the playbook.
pub description: String,
/// When the playbook was created in Amphitheatre.
pub created_at: String,
/// When the playbook was last updated in Amphitheatre.
pub updated_at: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct PlaybookPayload {
/// The title of the playbook
Expand All @@ -47,13 +33,13 @@ pub struct PlaybookPayload {
struct PlaybookEndpoint;

impl Endpoint for PlaybookEndpoint {
type Output = Playbook;
type Output = PlaybookSpec;
}

struct PlaybooksEndpoint;

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

/// The Playbooks Service handles the playbooks endpoint of the Amphitheatre API.
Expand All @@ -70,7 +56,7 @@ impl Playbooks<'_> {
///
/// `options`: The `RequestOptions`
/// - Sort: `id`, `label`, `email`
pub fn list(&self, options: Option<HashMap<String, String>>) -> Result<Vec<Playbook>, HTTPError> {
pub fn list(&self, options: Option<HashMap<String, String>>) -> Result<Vec<PlaybookSpec>, HTTPError> {
let res = self.client.get::<PlaybooksEndpoint>("/playbooks", options)?;
Ok(res.data.unwrap())
}
Expand All @@ -81,7 +67,7 @@ impl Playbooks<'_> {
///
/// `payload`: the `PlaybookPayload` with the information needed to create
/// the playbook
pub fn create(&self, payload: PlaybookPayload) -> Result<Playbook, HTTPError> {
pub fn create(&self, payload: PlaybookPayload) -> Result<PlaybookSpec, HTTPError> {
match serde_json::to_value(payload) {
Ok(json) => {
let res = self.client.post::<PlaybookEndpoint>("/playbooks", json)?;
Expand All @@ -98,7 +84,7 @@ impl Playbooks<'_> {
/// # Arguments
///
/// `pid`: The ID of the playbook we want to retrieve
pub fn get(&self, pid: &str) -> Result<Playbook, HTTPError> {
pub fn get(&self, pid: &str) -> Result<PlaybookSpec, HTTPError> {
let path = format!("/playbooks/{}", pid);
let res = self.client.get::<PlaybookEndpoint>(&path, None)?;
Ok(res.data.unwrap())
Expand All @@ -110,7 +96,7 @@ impl Playbooks<'_> {
///
/// `pid`: The playbook id
/// `payload`: The `PlaybookPayload` with the information needed to update
pub fn update(&self, pid: &str, payload: PlaybookPayload) -> Result<Playbook, HTTPError> {
pub fn update(&self, pid: &str, payload: PlaybookPayload) -> Result<PlaybookSpec, HTTPError> {
let path = format!("/playbooks/{}", pid);

match serde_json::to_value(payload) {
Expand Down
14 changes: 3 additions & 11 deletions tests/actors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ fn list_actors_test() {

let actors = client.actors().list(pid, None).unwrap();

assert_eq!(2, actors.len());
assert_eq!(1, actors.len());

let actor = actors.first().unwrap();

assert_eq!(1, actor.id);
assert_eq!("Default", actor.title);
assert_eq!("First", actor.description);
assert_eq!("2016-01-19T20:50:26Z", actor.created_at);
assert_eq!("2016-01-19T20:50:26Z", actor.updated_at);
assert_eq!("amp-example-go", actor.name);
}

#[test]
Expand All @@ -51,11 +47,7 @@ fn get_actor_test() {

let actor = client.actors().get(pid, name).unwrap();

assert_eq!(1, actor.id);
assert_eq!("Default", actor.title);
assert_eq!("First", actor.description);
assert_eq!("2016-01-19T20:50:26Z", actor.created_at);
assert_eq!("2016-01-19T20:50:26Z", actor.updated_at);
assert_eq!("amp-example-go", actor.name);
}

#[tokio::test]
Expand Down
22 changes: 6 additions & 16 deletions tests/fixtures/v1/api/actors/get-actor-success.http
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 19 Jan 2016 20:50:26 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
x-ratelimit-limit: 4000
x-ratelimit-remaining: 3997
x-ratelimit-after: 1453239045
ETag: W/"165299b0ea3e5c1c80f1ae622146626f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"id":1,"title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}
HTTP/1.1 200 OK
content-type: application/json
content-length: 545
date: Sun, 14 Jan 2024 10:22:26 GMT

{"name":"amp-example-go","character":{"meta":{"name":"amp-example-go","version":"0.0.3","authors":["Eguo Wang <wangeguo@gmail.com>"],"description":"A simple Golang example app","readme":"README.md","homepage":"https://github.com/amphitheatre-app/amp-example-go","repository":"https://github.com/amphitheatre-app/amp-example-go","license":"Apache-2.0","keywords":["example","golang","getting-started"],"categories":["example"]},"partners":{},"live":true,"once":true},"image":"index.docker.io/wangeguo/amp-example-go:live","live":true,"once":true}
22 changes: 6 additions & 16 deletions tests/fixtures/v1/api/actors/list-actors-success.http
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 19 Jan 2016 20:50:26 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
x-ratelimit-limit: 4000
x-ratelimit-remaining: 3997
x-ratelimit-after: 1453239045
ETag: W/"165299b0ea3e5c1c80f1ae622146626f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

[{"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"}]
HTTP/1.1 200 OK
content-type: application/json
content-length: 547
date: Sun, 14 Jan 2024 10:25:24 GMT

[{"name":"amp-example-go","character":{"meta":{"name":"amp-example-go","version":"0.0.3","authors":["Eguo Wang <wangeguo@gmail.com>"],"description":"A simple Golang example app","readme":"README.md","homepage":"https://github.com/amphitheatre-app/amp-example-go","repository":"https://github.com/amphitheatre-app/amp-example-go","license":"Apache-2.0","keywords":["example","golang","getting-started"],"categories":["example"]},"partners":{},"live":true,"once":true},"image":"index.docker.io/wangeguo/amp-example-go:live","live":true,"once":true}]
22 changes: 6 additions & 16 deletions tests/fixtures/v1/api/playbooks/create-playbook-created.http
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
HTTP/1.1 201 Created
Server: nginx
Date: Tue, 19 Jan 2016 20:50:26 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
x-ratelimit-limit: 4000
x-ratelimit-remaining: 3997
x-ratelimit-after: 1453239045
ETag: W/"165299b0ea3e5c1c80f1ae622146626f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"id":"1","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}
HTTP/1.1 200 OK
content-type: application/json
content-length: 1012
date: Sun, 14 Jan 2024 10:20:36 GMT

{"title":"Untitled","description":"","namespace":"amp-a82abba3-df2f-4608-b1a5-9e058ff80468","preface":{"name":"amp-example-go","manifest":{"meta":{"name":"amp-example-go","version":"0.0.3","authors":["Eguo Wang <wangeguo@gmail.com>"],"description":"A simple Golang example app","readme":"README.md","homepage":"https://github.com/amphitheatre-app/amp-example-go","repository":"https://github.com/amphitheatre-app/amp-example-go","license":"Apache-2.0","keywords":["example","golang","getting-started"],"categories":["example"]},"partners":{},"live":true,"once":true}},"characters":[{"meta":{"name":"amp-example-go","version":"0.0.3","authors":["Eguo Wang <wangeguo@gmail.com>"],"description":"A simple Golang example app","readme":"README.md","homepage":"https://github.com/amphitheatre-app/amp-example-go","repository":"https://github.com/amphitheatre-app/amp-example-go","license":"Apache-2.0","keywords":["example","golang","getting-started"],"categories":["example"]},"partners":{},"live":true,"once":true}]}
22 changes: 6 additions & 16 deletions tests/fixtures/v1/api/playbooks/get-playbook-success.http
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 19 Jan 2016 20:50:26 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
x-ratelimit-limit: 4000
x-ratelimit-remaining: 3997
x-ratelimit-after: 1453239045
ETag: W/"165299b0ea3e5c1c80f1ae622146626f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"id":"1","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}
HTTP/1.1 200 OK
content-type: application/json
content-length: 1012
date: Sun, 14 Jan 2024 10:20:36 GMT

{"title":"Untitled","description":"","namespace":"amp-a82abba3-df2f-4608-b1a5-9e058ff80468","preface":{"name":"amp-example-go","manifest":{"meta":{"name":"amp-example-go","version":"0.0.3","authors":["Eguo Wang <wangeguo@gmail.com>"],"description":"A simple Golang example app","readme":"README.md","homepage":"https://github.com/amphitheatre-app/amp-example-go","repository":"https://github.com/amphitheatre-app/amp-example-go","license":"Apache-2.0","keywords":["example","golang","getting-started"],"categories":["example"]},"partners":{},"live":true,"once":true}},"characters":[{"meta":{"name":"amp-example-go","version":"0.0.3","authors":["Eguo Wang <wangeguo@gmail.com>"],"description":"A simple Golang example app","readme":"README.md","homepage":"https://github.com/amphitheatre-app/amp-example-go","repository":"https://github.com/amphitheatre-app/amp-example-go","license":"Apache-2.0","keywords":["example","golang","getting-started"],"categories":["example"]},"partners":{},"live":true,"once":true}]}
22 changes: 6 additions & 16 deletions tests/fixtures/v1/api/playbooks/list-playbooks-success.http
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 19 Jan 2016 20:50:26 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
x-ratelimit-limit: 4000
x-ratelimit-remaining: 3997
x-ratelimit-after: 1453239045
ETag: W/"165299b0ea3e5c1c80f1ae622146626f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

[{"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"}]
HTTP/1.1 200 OK
content-type: application/json
content-length: 1014
date: Sun, 14 Jan 2024 10:19:55 GMT

[{"title":"Untitled","description":"","namespace":"amp-a82abba3-df2f-4608-b1a5-9e058ff80468","preface":{"name":"amp-example-go","manifest":{"meta":{"name":"amp-example-go","version":"0.0.3","authors":["Eguo Wang <wangeguo@gmail.com>"],"description":"A simple Golang example app","readme":"README.md","homepage":"https://github.com/amphitheatre-app/amp-example-go","repository":"https://github.com/amphitheatre-app/amp-example-go","license":"Apache-2.0","keywords":["example","golang","getting-started"],"categories":["example"]},"partners":{},"live":true,"once":true}},"characters":[{"meta":{"name":"amp-example-go","version":"0.0.3","authors":["Eguo Wang <wangeguo@gmail.com>"],"description":"A simple Golang example app","readme":"README.md","homepage":"https://github.com/amphitheatre-app/amp-example-go","repository":"https://github.com/amphitheatre-app/amp-example-go","license":"Apache-2.0","keywords":["example","golang","getting-started"],"categories":["example"]},"partners":{},"live":true,"once":true}]}]
22 changes: 6 additions & 16 deletions tests/fixtures/v1/api/playbooks/update-playbook-success.http
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 19 Jan 2016 20:50:26 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
x-ratelimit-limit: 4000
x-ratelimit-remaining: 3997
x-ratelimit-after: 1453239045
ETag: W/"165299b0ea3e5c1c80f1ae622146626f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"id":"1","title":"Default","description":"First","created_at":"2016-01-19T20:50:26Z","updated_at":"2016-01-19T20:50:26Z"}
HTTP/1.1 200 OK
content-type: application/json
content-length: 1012
date: Sun, 14 Jan 2024 10:20:36 GMT

{"title":"Untitled","description":"","namespace":"amp-a82abba3-df2f-4608-b1a5-9e058ff80468","preface":{"name":"amp-example-go","manifest":{"meta":{"name":"amp-example-go","version":"0.0.3","authors":["Eguo Wang <wangeguo@gmail.com>"],"description":"A simple Golang example app","readme":"README.md","homepage":"https://github.com/amphitheatre-app/amp-example-go","repository":"https://github.com/amphitheatre-app/amp-example-go","license":"Apache-2.0","keywords":["example","golang","getting-started"],"categories":["example"]},"partners":{},"live":true,"once":true}},"characters":[{"meta":{"name":"amp-example-go","version":"0.0.3","authors":["Eguo Wang <wangeguo@gmail.com>"],"description":"A simple Golang example app","readme":"README.md","homepage":"https://github.com/amphitheatre-app/amp-example-go","repository":"https://github.com/amphitheatre-app/amp-example-go","license":"Apache-2.0","keywords":["example","golang","getting-started"],"categories":["example"]},"partners":{},"live":true,"once":true}]}
Loading

0 comments on commit d9345e8

Please sign in to comment.