Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.

Commit aec1da8

Browse files
committed
implement server channel create route
1 parent 2fadd8b commit aec1da8

File tree

7 files changed

+148
-6
lines changed

7 files changed

+148
-6
lines changed

http/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ edition = "2021"
88
license = "ISC"
99
repository = "https://github.com/HarTexTeam/guilded-rs"
1010
rust-version = "1.57.0"
11-
version = "0.1.0-dev.6"
11+
version = "0.1.0-dev.7"
1212

1313
[dependencies]
1414
guilded_model = "0.1.0-dev.14"
15-
guilded_validation = "0.1.0-dev.1"
15+
guilded_validation = "0.1.0-dev.2"
1616

1717
hyper = { default-features = false, features = [ "client", "http1", "http2", "runtime" ], version = "0.14.19" }
1818
hyper-rustls = { default-features = false, optional = true, features = [ "http1", "http2" ], version = "0.23.0" }

http/src/client/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::sync::atomic::AtomicBool;
44
use std::sync::Arc;
55
use std::time::Duration;
66

7+
use guilded_model::channel::ServerChannelType;
8+
use guilded_validation::channel::ChannelValidationError;
79
use hyper::client::Client as Hyper;
810
use hyper::header::{HeaderValue, ACCEPT, AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE};
911
use hyper::{Body, Request as HyperRequest};
@@ -12,6 +14,7 @@ use tokio::time;
1214
use crate::client::builder::ClientBuilder;
1315
use crate::client::connector::Connector;
1416
use crate::error::{Error, ErrorType};
17+
use crate::request::server::server_channel_create::ServerChannelCreate;
1518
use crate::request::{Method, Request};
1619
use crate::response::future::ResponseFuture;
1720
use crate::API_VERSION;
@@ -41,6 +44,14 @@ impl Client {
4144
self.token.as_deref()
4245
}
4346

47+
pub fn server_channel_create<'a>(
48+
&'a self,
49+
name: &'a str,
50+
r#type: ServerChannelType,
51+
) -> Result<ServerChannelCreate<'a>, ChannelValidationError> {
52+
ServerChannelCreate::new(self, name, r#type)
53+
}
54+
4455
pub fn request<T>(&self, request: Request) -> ResponseFuture<T> {
4556
match self.try_request(request) {
4657
Ok(future) => future,

http/src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub enum ErrorType {
1616
name: String,
1717
},
1818
HttpRequestBuild,
19+
Json,
1920
Parsing {
2021
body: Vec<u8>,
2122
},

http/src/json.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use serde::de::DeserializeOwned;
22
use serde_json::Result as JsonResult;
33

4+
pub use serde_json::to_vec;
5+
46
pub fn from_bytes<T: DeserializeOwned>(bytes: &[u8]) -> JsonResult<T> {
57
serde_json::from_slice(bytes)
68
}

http/src/request/mod.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
use hyper::header::HeaderValue;
1+
use hyper::header::{HeaderName, HeaderValue};
22
use hyper::{HeaderMap, Method as HyperMethod};
3+
use serde::Serialize;
4+
5+
use crate::error::{Error, ErrorType};
6+
use crate::route::Route;
37

48
pub mod server;
59

@@ -12,6 +16,61 @@ pub struct Request {
1216
pub(crate) use_authorization_token: bool,
1317
}
1418

19+
impl Request {
20+
pub fn builder(route: &Route<'_>) -> RequestBuilder {
21+
RequestBuilder::new(route)
22+
}
23+
24+
pub fn from_route(route: &Route<'_>) -> Self {
25+
Self {
26+
body: None,
27+
headers: None,
28+
method: route.method(),
29+
path: route.to_string(),
30+
use_authorization_token: true,
31+
}
32+
}
33+
}
34+
35+
pub struct RequestBuilder(Request);
36+
37+
impl RequestBuilder {
38+
pub fn new(route: &Route<'_>) -> Self {
39+
Self(Request::from_route(route))
40+
}
41+
42+
pub fn build(self) -> Request {
43+
self.0
44+
}
45+
46+
pub fn body(mut self, body: Vec<u8>) -> Self {
47+
self.0.body = Some(body);
48+
49+
self
50+
}
51+
52+
pub fn headers(mut self, iter: impl Iterator<Item = (HeaderName, HeaderValue)>) -> Self {
53+
self.0.headers.replace(iter.collect());
54+
55+
self
56+
}
57+
58+
pub fn json(self, serialize: &impl Serialize) -> Result<Self, Error> {
59+
let bytes = crate::json::to_vec(serialize).map_err(|source| Error {
60+
source: Some(Box::new(source)),
61+
r#type: ErrorType::Json,
62+
})?;
63+
64+
Ok(self.body(bytes))
65+
}
66+
67+
pub const fn use_authorization_token(mut self, use_authorization_token: bool) -> Self {
68+
self.0.use_authorization_token = use_authorization_token;
69+
70+
self
71+
}
72+
}
73+
1574
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
1675
#[non_exhaustive]
1776
pub enum Method {

http/src/request/server/server_channel_create.rs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
use crate::client::Client;
22

3-
use guilded_model::channel::ServerChannelType;
3+
use crate::error::Error;
4+
use crate::request::Request;
5+
use guilded_model::channel::{ServerChannel, ServerChannelType};
6+
use guilded_model::id::{
7+
marker::{CategoryMarker, GroupMarker, ServerMarker},
8+
Id,
9+
};
410
use guilded_validation::channel::{self, ChannelValidationError};
511
use serde::Serialize;
612

13+
use crate::response::future::ResponseFuture;
14+
use crate::route::Route;
15+
716
#[must_use = "requests must be configured and executed"]
817
pub struct ServerChannelCreate<'a> {
918
client: &'a Client,
@@ -21,18 +30,78 @@ impl<'a> ServerChannelCreate<'a> {
2130
Ok(Self {
2231
client,
2332
fields: ServerChannelCreateFields {
33+
category_id: None,
34+
group_id: None,
2435
name,
36+
is_public: None,
37+
server_id: None,
2538
topic: None,
2639
r#type,
27-
}
40+
},
2841
})
2942
}
43+
44+
pub fn category_id(mut self, category_id: Id<CategoryMarker>) -> Self {
45+
self.fields.category_id.replace(category_id);
46+
self
47+
}
48+
49+
pub fn group_id(mut self, group_id: Id<GroupMarker>) -> Self {
50+
self.fields.group_id.replace(group_id);
51+
self
52+
}
53+
54+
pub fn is_public(mut self, is_public: bool) -> Self {
55+
self.fields.is_public.replace(is_public);
56+
self
57+
}
58+
59+
pub fn server_id(mut self, server_id: Id<ServerMarker>) -> Self {
60+
self.fields.server_id.replace(server_id);
61+
self
62+
}
63+
64+
pub fn topic(mut self, topic: &'a str) -> Result<Self, ChannelValidationError> {
65+
channel::validate_topic_length(topic)?;
66+
67+
self.fields.topic.replace(topic);
68+
Ok(self)
69+
}
70+
71+
pub fn finish(self) -> ResponseFuture<ServerChannel> {
72+
let client = self.client;
73+
74+
match self.try_into() {
75+
Ok(request) => client.request(request),
76+
Err(source) => ResponseFuture::error(source),
77+
}
78+
}
79+
}
80+
81+
impl TryInto<Request> for ServerChannelCreate<'_> {
82+
type Error = Error;
83+
84+
fn try_into(self) -> Result<Request, Self::Error> {
85+
let mut request = Request::builder(&Route::ServerChannelCreate);
86+
request = request.json(&self.fields)?;
87+
88+
Ok(request.build())
89+
}
3090
}
3191

3292
#[derive(Serialize)]
93+
#[serde(rename_all = "camelCase")]
3394
struct ServerChannelCreateFields<'a> {
95+
#[serde(skip_serializing_if = "Option::is_none")]
96+
category_id: Option<Id<CategoryMarker>>,
97+
#[serde(skip_serializing_if = "Option::is_none")]
98+
group_id: Option<Id<GroupMarker>>,
3499
name: &'a str,
35100
#[serde(skip_serializing_if = "Option::is_none")]
101+
is_public: Option<bool>,
102+
#[serde(skip_serializing_if = "Option::is_none")]
103+
server_id: Option<Id<ServerMarker>>,
104+
#[serde(skip_serializing_if = "Option::is_none")]
36105
topic: Option<&'a str>,
37106
r#type: ServerChannelType,
38107
}

http/src/route.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! HTTP Routes of the Guilded API.
22
3-
use std::fmt::{Display, Formatter, Result as FmtResult, Write};
3+
use std::fmt::{Display, Formatter, Result as FmtResult};
44

55
use guilded_model::datetime::Timestamp;
66

0 commit comments

Comments
 (0)