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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::borrow::Cow;
use std::io::Read;
use std::path::Path;
use std::ffi::OsStr;
use std::fs::read_to_string;
use reqwest::Method;
use super::{ Request, RequestBody };
use crate::{
settings::Settings,
multipart::FormFile,
job::{ JobId, JobStatus },
error::Result,
};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SubmitId {
#[serde(rename = "pdbName")]
pub pdb_id: String,
#[serde(flatten, default)]
pub settings: Settings,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SubmitStructure {
#[serde(rename = "file")]
pub pdb_structure: FormFile,
#[serde(rename = "fileName", default, skip_serializing_if = "Option::is_none")]
pub file_name: Option<String>,
#[serde(flatten, default)]
pub settings: Settings,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SubmitResponse {
#[serde(rename = "jobid")]
pub job_id: JobId,
pub status: JobStatus,
}
impl SubmitId {
pub fn with_pdb_id<T: Into<String>>(pdb_id: T) -> Self {
SubmitId {
pdb_id: pdb_id.into(),
settings: Settings::default(),
}
}
}
impl SubmitStructure {
pub fn with_pdb_structure<T: Into<String>>(pdb_structure: T) -> Self {
SubmitStructure {
pdb_structure: FormFile::with_contents_and_file_name(
pdb_structure.into(),
String::from("rust_ring_api_dummy.pdb"),
),
file_name: None,
settings: Settings::default(),
}
}
pub fn with_reader<R: Read>(mut reader: R) -> Result<Self> {
let mut structure = String::new();
reader.read_to_string(&mut structure)?;
Ok(Self::with_pdb_structure(structure))
}
pub fn with_pdb_file<P: AsRef<Path>>(file: P) -> Result<Self> {
let path = file.as_ref();
let maybe_file_name = path
.file_name()
.and_then(OsStr::to_str)
.map(Into::into);
let structure = read_to_string(path)?;
Ok(SubmitStructure {
pdb_structure: FormFile::with_contents_and_file_name(
structure,
maybe_file_name.clone().unwrap_or_else(
|| String::from("rust_ring_api_dummy.pdb")
)
),
file_name: maybe_file_name,
settings: Settings::default(),
})
}
pub fn file_name<T: Into<String>>(mut self, file_name: T) -> Self {
let file_name = file_name.into();
self.file_name.replace(file_name.clone());
self.pdb_structure.set_file_name(file_name);
self
}
pub fn or_file_name<T: Into<String>>(self, file_name: T) -> Self {
if self.file_name.is_none() {
self.file_name(file_name)
} else {
self
}
}
pub fn settings(self, settings: Settings) -> Self {
SubmitStructure { settings, ..self }
}
}
impl Request for SubmitId {
type Body = Self;
type Response = SubmitResponse;
const METHOD: Method = Method::POST;
fn endpoint(&self) -> Cow<str> {
Cow::from("/submit")
}
fn body(&self) -> RequestBody<&Self::Body> {
RequestBody::Json(self)
}
}
impl Request for SubmitStructure {
type Body = Self;
type Response = SubmitResponse;
const METHOD: Method = Method::POST;
fn endpoint(&self) -> Cow<str> {
Cow::from("/submit")
}
fn body(&self) -> RequestBody<&Self::Body> {
RequestBody::Multipart(self)
}
}