-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathcreate_database.rs
More file actions
100 lines (85 loc) · 2.8 KB
/
Copy pathcreate_database.rs
File metadata and controls
100 lines (85 loc) · 2.8 KB
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
use super::data_structures::{D1Database, D1PrimaryLocationHint};
use crate::framework::endpoint::{EndpointSpec, Method, RequestBody};
use crate::framework::response::ApiSuccess;
use serde::Serialize;
/// Create a new D1 database
///
/// Creates a new D1 database with the specified name.
/// Database names must be unique within the account.
///
/// <https://api.cloudflare.com/#d1-create-database>
#[derive(Debug)]
pub struct CreateDatabase<'a> {
pub account_identifier: &'a str,
pub params: CreateDatabaseParams,
}
impl<'a> CreateDatabase<'a> {
pub fn new(account_identifier: &'a str, params: CreateDatabaseParams) -> Self {
Self {
account_identifier,
params,
}
}
}
impl EndpointSpec for CreateDatabase<'_> {
type JsonResponse = D1Database;
type ResponseType = ApiSuccess<Self::JsonResponse>;
fn method(&self) -> Method {
Method::POST
}
fn path(&self) -> String {
format!("accounts/{}/d1/database", self.account_identifier)
}
fn body(&self) -> Option<RequestBody> {
let body = serde_json::to_string(&self.params).unwrap();
Some(RequestBody::Json(body))
}
}
/// Parameters for creating a D1 database
#[serde_with::skip_serializing_none]
#[derive(Serialize, Clone, Debug, PartialEq, Eq)]
pub struct CreateDatabaseParams {
/// The name of the database to create
pub name: String,
/// Specify the region to create the D1 primary (optional)
pub primary_location_hint: Option<D1PrimaryLocationHint>,
}
impl CreateDatabaseParams {
pub fn new(name: String) -> Self {
Self {
name,
primary_location_hint: None,
}
}
pub fn with_location_hint(name: String, location_hint: D1PrimaryLocationHint) -> Self {
Self {
name,
primary_location_hint: Some(location_hint),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_database_params() {
let params = CreateDatabaseParams::new("test-db".to_string());
assert_eq!(params.name, "test-db");
assert_eq!(params.primary_location_hint, None);
let json = serde_json::to_string(¶ms).unwrap();
let expected = r#"{"name":"test-db"}"#;
assert_eq!(json, expected);
}
#[test]
fn test_create_database_params_with_location() {
let params = CreateDatabaseParams::with_location_hint(
"test-db".to_string(),
D1PrimaryLocationHint::Weur
);
assert_eq!(params.name, "test-db");
assert_eq!(params.primary_location_hint, Some(D1PrimaryLocationHint::Weur));
let json = serde_json::to_string(¶ms).unwrap();
let expected = r#"{"name":"test-db","primary_location_hint":"weur"}"#;
assert_eq!(json, expected);
}
}