Skip to content

Commit

Permalink
Retrieve the log streams of actor
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Oct 25, 2023
1 parent 1c27314 commit 9793eca
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
5 changes: 4 additions & 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.4.1"
version = "0.4.2"
edition = "2021"
license = "Apache-2.0"
homepage = "https://amphitheatre.app"
Expand All @@ -12,8 +12,11 @@ readme = "README.md"

[dependencies]
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.5.3" }
futures = "0.3"
reqwest-eventsource = "0.5.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] }
tokio = { version = "1.33.0", features = ["full"] }

[dev-dependencies]
mockito = "1.1.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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
9 changes: 5 additions & 4 deletions src/actors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::collections::HashMap;

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

Expand Down Expand Up @@ -94,12 +95,12 @@ impl Actors<'_> {
///
/// `pid`: The ID of the playbook
/// `name`: The name of the actor
pub fn logs(&self, _pid: &str, _name: &str) -> String {
// let path = format!("/actors/{}/{}/logs", pid, name);
String::from("event stream (JSON)")
pub fn logs(&self, pid: &str, name: &str) -> EventSource {
let path = format!("/actors/{}/{}/logs", pid, name);
EventSource::get(self.client.url(&path))
}

/// Retrieve actor's info, including enviroments, volumes...
/// Retrieve actor's info, including environments, volumes...
///
/// # Arguments
///
Expand Down
21 changes: 16 additions & 5 deletions tests/actors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.

use amp_common::sync::{EventKinds, Path, Synchronization};
use futures::StreamExt;
use reqwest_eventsource::Event;

use crate::common::setup_mock_for;
mod common;
Expand Down Expand Up @@ -56,16 +58,25 @@ fn get_actor_test() {
assert_eq!("2016-01-19T20:50:26Z", actor.updated_at);
}

#[test]
fn get_actor_logs() {
#[tokio::test]
async fn get_actor_logs() {
let setup = setup_mock_for("/actors/1/hello/logs", "actors/get-actor-logs-success", "GET");
let client = setup.0;
let pid = "1";
let name = "hello";

let response = client.actors().logs(pid, name);

assert_eq!(String::from("event stream (JSON)"), response);
let mut es = client.actors().logs(pid, name);

while let Some(event) = es.next().await {
match event {
Ok(Event::Open) => println!("Connection Open!"),
Ok(Event::Message(message)) => println!("Message: {:#?}", message),
Err(err) => {
println!("Error: {}", err);
es.close();
}
}
}
}

#[test]
Expand Down
27 changes: 12 additions & 15 deletions tests/fixtures/v1/api/actors/get-actor-logs-success.http
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
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
HTTP/1.1 200 OK
content-type: text/event-stream
cache-control: no-cache
transfer-encoding: chunked
date: Wed, 25 Oct 2023 13:55:53 GMT

data:2023-10-25T13:55:44.364123031Z Hello world!

{"data":"event stream (JSON)"}
data:2023-10-25T13:55:45.365054370Z Hello world!

data:2023-10-25T13:55:46.366121857Z Hello world!

data:2023-10-25T13:55:47.367230463Z Hello world!

0 comments on commit 9793eca

Please sign in to comment.