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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2023 Ikerian AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use cratedisco_api;
use crate;
use GraphQLQuery;
use Value as JSON;
use Arc;
disco_api!;
disco_api!;
/// Creates a new file entry in the specified workbook and returns a signed URL for uploading the file.
///
/// # Arguments
///
/// * `qc` - An `Arc<QueryClient>` object that represents the connection to the Connect server.
/// * `workbook_uuid` - The UUID of the workbook in which to create the file.
/// * `path` - The path to the file, relative to the workbook's root directory.
///
/// # Returns
///
/// A `Result` containing either the response data on success (`create_file::ResponseData`),
/// or an `reqwest::Error` on failure. The response data contains:
/// - the UUID of the newly created file
/// - a signed URL for uploading the file
///
/// # Errors
///
/// Returns an `Err` of type `reqwest::Error` if the file creation request fails or if the server
/// response status is not OK.
///
/// # Example
///
/// ```
/// use discovery_connect::file::{CreateFile, create_file};
/// use discovery_connect::{QueryClient};
/// use std::sync::Arc;
///
/// let qc = Arc::new(QueryClient::new(
/// "https://api.example.discovery.retinai.com",
/// "client_id",
/// "client_secret",
/// "user@example",
/// "password123",
/// std::time::Duration::from_secs(10)));
/// async {
/// let res = create_file(qc, "123e4567-e89b-12d3-a456-426614174000", "path/to/file").await;
/// match res {
/// Ok(data) => println!("File created: {}", data.create_file.unwrap().uuid.unwrap()),
/// Err(e) => println!("Error: {:?}", e),
/// };
/// };
///
/// ```
pub async
/// Registers the status of an uploaded file in the system, marking it as successfully uploaded or failed.
///
/// If successful, the server will start processing the file.
///
/// # Arguments
///
/// * `qc` - An `Arc<QueryClient>` object representing the connection to the Connect server.
/// * `uuid` - A String reference representing the UUID of the file whose upload status is being registered.
/// * `upload_failed` - An `Option<bool>` indicating whether the upload failed (Some(true)) or succeeded (Some(false) or None).
///
/// # Returns
///
/// A `Result` containing either the response data on success (`register_uploaded_file::ResponseData`),
/// or an `reqwest::Error` on failure. The response data typically includes confirmation of the registration.
///
/// # Errors
///
/// Returns an `Err` of type `reqwest::Error` if the registration request fails or if the server
/// response status is not OK.
///
/// # Example
///
/// ```
/// use discovery_connect::file::{RegisterUploadedFile, register_uploaded_file};
/// use discovery_connect::{QueryClient};
/// use std::sync::Arc;
///
/// let qc = Arc::new(QueryClient::new(
/// "https://api.example.discovery.retinai.com",
/// "client_id",
/// "client_secret",
/// "user@example",
/// "password123",
/// std::time::Duration::from_secs(10)));
/// let file_uuid = "123e4567-e89b-12d3-a456-426614174000".to_string();
/// let upload_status = Some(false);
/// async {
/// let res = register_uploaded_file(qc.clone(), &file_uuid, upload_status).await;
/// match res {
/// Ok(response) => println!("File registration successful"),
/// Err(e) => println!("Error registering file: {}", e),
/// }
/// };
/// ```
pub async
/// Streams a file to AWS S3 using a pre-signed URL.
///
/// # Arguments
///
/// * `qc` - An `Arc<QueryClient>` object that represents the connection to the Connect server.
/// * `signed_url` - The pre-signed URL provided by AWS S3 for file upload.
/// * `filename` - The local path to the file that needs to be uploaded.
///
/// # Returns
///
/// A `Result` containing either a `reqwest::Response` on success, or a `Box<dyn std::error::Error>` on failure.
/// The successful response indicates that the file has been successfully uploaded to the specified S3 location.
///
/// # Errors
///
/// Returns an `Err` of type `Box<dyn std::error::Error>` if there's an error in reading the file, preparing the HTTP request,
/// or if the server response status is not OK. Includes a `reqwest::Error` if the upload fails.
///
/// # Notes
///
/// Currently, this function has a known issue with streaming from disk to S3 (returns a 501 error).
/// This needs investigation and fixing. It works correctly in the C# client implementation.
///
/// # Example
///
/// ```
/// use discovery_connect::file::{create_file, stream_to_s3};
/// use discovery_connect::{QueryClient};
/// use std::sync::Arc;
///
/// let qc = Arc::new(QueryClient::new(
/// "https://api.example.discovery.retinai.com",
/// "client_id",
/// "client_secret",
/// "user@example",
/// "password123",
/// std::time::Duration::from_secs(10)));
/// async {
/// let filename = "path/to/local/file";
/// let file = create_file(qc.clone(), "123e4567-e89b-12d3-a456-426614174000", &filename).await.unwrap();
/// let binding = file.create_file.unwrap().signed_upload_url.unwrap();
/// let signed_url = binding.as_str();
///
/// let res = stream_to_s3(qc.clone(), signed_url, &filename).await;
/// match res {
/// Ok(response) => println!("File uploaded successfully: {}", response.status()),
/// Err(e) => println!("Error during file upload: {}", e),
/// }
/// };
/// ```
pub async