1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use crate::channel::Channel;
use crate::error::{Result, UrbitAPIError};
use json::JsonValue;
use reqwest::blocking::{Client, Response};
use reqwest::header::{HeaderValue, COOKIE};
#[derive(Debug, Clone)]
pub struct ShipInterface {
pub url: String,
pub session_auth: HeaderValue,
pub ship_name: String,
req_client: Client,
}
impl ShipInterface {
pub fn new(ship_url: &str, ship_code: &str) -> Result<ShipInterface> {
let client = Client::new();
let login_url = format!("{}/~/login", ship_url);
let resp = client
.post(&login_url)
.body("password=".to_string() + &ship_code)
.send()?;
if resp.status().as_u16() != 204 {
return Err(UrbitAPIError::FailedToLogin);
}
let session_auth = resp
.headers()
.get("set-cookie")
.ok_or(UrbitAPIError::FailedToLogin)?;
let auth_string = session_auth
.to_str()
.map_err(|_| UrbitAPIError::FailedToLogin)?;
let ship_name = &auth_string[9..auth_string.find('=').unwrap()];
Ok(ShipInterface {
url: ship_url.to_string(),
session_auth: session_auth.clone(),
ship_name: ship_name.to_string(),
req_client: client,
})
}
pub fn create_channel(&self) -> Result<Channel> {
Channel::new(self.clone())
}
pub fn send_put_request(&self, url: &str, body: &JsonValue) -> Result<Response> {
let json = body.dump();
let resp = self
.req_client
.put(url)
.header(COOKIE, self.session_auth.clone())
.header("Content-Type", "application/json")
.body(json);
Ok(resp.send()?)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::subscription::Subscription;
#[test]
fn can_login() {
let ship_interface =
ShipInterface::new("http://0.0.0.0:8080", "lidlut-tabwed-pillex-ridrup").unwrap();
}
#[test]
fn can_create_channel() {
let ship_interface =
ShipInterface::new("http://0.0.0.0:8080", "lidlut-tabwed-pillex-ridrup").unwrap();
let channel = ship_interface.create_channel().unwrap();
channel.delete_channel();
}
#[test]
fn can_subscribe() {
let ship_interface =
ShipInterface::new("http://0.0.0.0:8080", "lidlut-tabwed-pillex-ridrup").unwrap();
let mut channel = ship_interface.create_channel().unwrap();
channel
.create_new_subscription("chat-view", "/primary")
.unwrap();
channel.find_subscription("chat-view", "/primary");
channel.unsubscribe("chat-view", "/primary");
channel.delete_channel();
}
#[test]
fn can_poke() {
let ship_interface =
ShipInterface::new("http://0.0.0.0:8080", "lidlut-tabwed-pillex-ridrup").unwrap();
let mut channel = ship_interface.create_channel().unwrap();
let poke_res = channel
.poke("hood", "helm-hi", "A poke has been made")
.unwrap();
assert!(poke_res.status().as_u16() == 204);
channel.delete_channel();
}
}