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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use crate::{utils::ReadJsonTreeSteps, Shopify, ShopifyAPIError};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum StagedUploadTargetGenerateUploadResourceInput {
    #[serde(rename = "BULK_MUTATION_VARIABLES")]
    BulkMutationVariables,
    #[serde(rename = "COLLECTION_IMAGE")]
    CollectionImage,
    #[serde(rename = "FILE")]
    File,
    #[serde(rename = "IMAGE")]
    Image,
    #[serde(rename = "MODEL_3D")]
    Model3D,
    #[serde(rename = "PRODUCT_IMAGE")]
    ProductImage,
    #[serde(rename = "RETURN_LABEL")]
    ReturnLabel,
    #[serde(rename = "URL_REDIRECT_IMPORT")]
    UrlRedirectImport,
    #[serde(rename = "VIDEO")]
    Video,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct StagedUploadParameter {
    pub name: String,
    pub value: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct StagedMediaUploadTarget {
    pub parameters: Vec<StagedUploadParameter>,
    #[serde(rename = "resourceUrl")]
    pub resource_url: String,
    pub url: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ShopifyStagedUploadsCreateInputQuery {
    #[serde(rename = "stagedTargets")]
    pub staged_targets: Option<Vec<StagedMediaUploadTarget>>,
    #[serde(rename = "userErrors")]
    pub user_errors: Option<Vec<ShopifyUserError>>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct StagedUploadsCreateInput {
    #[serde(rename = "fileSize")]
    pub file_size: Option<u64>,
    pub filename: String,

    #[serde(rename = "httpMethod")]
    pub http_method: Option<String>, // POST OR PUT

    #[serde(rename = "mimeType")]
    pub mime_type: String,

    pub resource: StagedUploadTargetGenerateUploadResourceInput,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum ShopifyBulkErrorCode {
    #[serde(rename = "ACCESS_DENIED")]
    AccessDenied,
    #[serde(rename = "INTERNAL_SERVER_ERROR")]
    InternalServerError,
    #[serde(rename = "TIMEOUT")]
    Timeout,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub enum ShopifyBulkStatus {
    #[serde(rename = "CANCELED")]
    Canceled,
    #[serde(rename = "CANCELING")]
    Canceling,
    #[serde(rename = "COMPLETED")]
    Completed,
    #[serde(rename = "CREATED")]
    Created,
    #[serde(rename = "EXPIRED")]
    Expired,
    #[serde(rename = "FAILED")]
    Failed,
    #[serde(rename = "RUNNING")]
    Running,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ShopifyBulk {
    pub id: Option<String>,
    pub url: Option<String>,
    #[serde(rename = "partialDataUrl")]
    pub partial_data_url: Option<String>,
    pub status: ShopifyBulkStatus,
    #[serde(rename = "errorCode")]
    pub error_code: Option<ShopifyBulkErrorCode>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ShopifyUserError {
    pub field: Vec<String>,
    pub message: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ShopifyBulkOperationRunQuery {
    #[serde(rename = "bulkOperation")]
    pub bulk_operation: Option<ShopifyBulk>,
    #[serde(rename = "userErrors")]
    pub user_errors: Option<Vec<ShopifyUserError>>,
}

impl Shopify {
    /// Query graphql shopify api
    /// # Example
    /// ```
    /// use shopify_api::*;
    /// use shopify_api::utils::ReadJsonTreeSteps;
    /// use serde::{Deserialize};
    ///
    /// #[derive(Deserialize)]
    /// struct Shop {
    ///    name: String,
    /// }
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///   let shopify = Shopify::new(env!("TEST_SHOP_NAME"), env!("TEST_KEY"), String::from("2024-04"), None);
    ///   let graphql_query = r#"{
    ///      products {
    ///         edges {
    ///          node {
    ///             id
    ///             title
    ///          }
    ///         }
    ///     }
    ///   }"#;
    ///
    ///   let variables = serde_json::json!({});
    ///   let products_bulk = shopify.make_bulk_query(graphql_query).await.unwrap();
    ///
    ///   shopify.wait_for_bulk(&products_bulk.bulk_operation.as_ref().unwrap().id.as_ref().unwrap()).await.unwrap();
    ///
    ///   let bulk_status = shopify.get_bulk_by_id(&products_bulk.bulk_operation.unwrap().id.unwrap()).await.unwrap();
    /// }
    ///
    ///
    /// ```
    pub async fn get_bulk_by_id(&self, id: &str) -> Option<ShopifyBulk> {
        let json: ShopifyBulk = self
            .graphql_query(
                r#"
                query($id: ID!) {
                    node(id: $id) {
                        ... on BulkOperation {
                            id
                            url
                            partialDataUrl
                            status
                        }
                    }
                }
            "#,
                &json!({ "id": id }),
                &vec![
                    ReadJsonTreeSteps::Key("data"),
                    ReadJsonTreeSteps::Key("node"),
                ],
            )
            .await
            .unwrap();

        Some(json)
    }
    /// Executes a GraphQL bulk query on the Shopify API.
    ///
    /// # Arguments
    /// * `query` - The GraphQL query to execute in bulk.
    ///
    /// # Returns
    /// `Result<ShopifyBulkOperationRunQuery, ShopifyAPIError>` - A result containing the bulk operation or an error.
    ///
    /// # Example
    /// ```ts,no_run
    /// let bulk_query = "{ products { edges { node { id title } } } }";
    /// let result = shopify.make_bulk_query(bulk_query).await?;
    /// ```
    pub async fn make_bulk_query(
        &self,
        query: &str,
    ) -> Result<ShopifyBulkOperationRunQuery, crate::ShopifyAPIError> {
        let bulk_query = format!(
            r#"
            mutation {{
                bulkOperationRunQuery(
                    query: """
                    {query}
                    """
                ) {{
                    bulkOperation {{
                        id
                        url
                        partialDataUrl
                        status
                    }}
                    userErrors {{
                        field
                        message
                    }}
                }}
            }}"#
        );

        let result: ShopifyBulkOperationRunQuery = self
            .graphql_query(
                &bulk_query,
                &json!({}),
                &vec![
                    ReadJsonTreeSteps::Key("data"),
                    ReadJsonTreeSteps::Key("bulkOperationRunQuery"),
                ],
            )
            .await?;

        Ok(result)
    }
    /// Executes a bulk mutation on the Shopify API.
    ///
    /// # Arguments
    /// * `mutation_string` - The string representing the mutation.
    /// * `staged_upload_path` - The path for the staged upload.
    ///
    /// # Returns
    /// `Result<ShopifyBulkOperationRunQuery, ShopifyAPIError>` - A result containing the bulk operation or an error.
    ///
    /// # Example
    /// ```ts,no_run
    /// let mutation_string = "{ updateProducts(...) { ... } }";
    /// let staged_upload_path = "/path/to/upload";
    /// let result = shopify.make_bulk_mutation(mutation_string, staged_upload_path).await?;
    /// ```
    pub async fn make_bulk_mutation(
        &self,
        mutation_string: &str,
        staged_upload_path: &str,
    ) -> Result<ShopifyBulkOperationRunQuery, crate::ShopifyAPIError> {
        let bulk_mutation = r#"
            mutation bulkOperationRunMutation($mutation: String!, $stagedUploadPath: String!) {
                bulkOperationRunMutation(
                    mutation: $mutation,
                    stagedUploadPath: $stagedUploadPath,
                ) {
                    bulkOperation {
                        id
                        url
                        partialDataUrl
                        status
                    }
                    userErrors {
                        field
                        message
                    }
                }
            }"#;

        let result: ShopifyBulkOperationRunQuery = self
            .graphql_query(
                bulk_mutation,
                &json!({
                    "mutation": mutation_string,
                    "stagedUploadPath": staged_upload_path,
                }),
                &vec![
                    ReadJsonTreeSteps::Key("data"),
                    ReadJsonTreeSteps::Key("bulkOperationRunMutation"),
                ],
            )
            .await?;

        Ok(result)
    }

    /// Waits for the specified bulk operation to complete.
    ///
    /// # Arguments
    /// * `id` - The identifier of the bulk operation.
    ///
    /// # Returns
    /// `Result<ShopifyBulk, ShopifyAPIError>` - The final status of the bulk operation or an error.
    ///
    /// # Example
    /// ```ts,no_run
    /// let bulk_id = "123456";
    /// let bulk_status = shopify.wait_for_bulk(bulk_id);
    /// ```
    pub async fn wait_for_bulk(&self, id: &str) -> Result<ShopifyBulk, crate::ShopifyAPIError> {
        let mut get_bulk = self.get_bulk_by_id(id).await;

        if get_bulk.is_none() {
            return Err(crate::ShopifyAPIError::Other(
                "Bulk operation not found".to_string(),
            ));
        }

        let mut bulk = get_bulk.unwrap();

        while bulk.status == ShopifyBulkStatus::Running {
            get_bulk = self.get_bulk_by_id(id).await;

            if get_bulk.is_none() {
                return Err(crate::ShopifyAPIError::Other(
                    "Bulk operation not found".to_string(),
                ));
            }

            bulk = get_bulk.unwrap();
            tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
        }

        Ok(bulk)
    }

    /// Downloads data from a Shopify bulk operation.
    ///
    /// # Arguments
    /// * `url` - The URL to download the bulk operation data.
    ///
    /// # Returns
    /// `Result<Vec<serde_json::Value>, ShopifyAPIError>` - The downloaded data or an error.
    ///
    /// # Example
    /// ```ts,no_run
    /// let url = "https://shopify.com/bulk/123456";
    /// let data = Shopify::download_bulk(url).await?;
    /// ```
    pub async fn download_bulk(url: &str) -> Result<Vec<serde_json::Value>, ShopifyAPIError> {
        let resp = reqwest::get(url).await?;
        let body = resp.text().await?;

        body.split('\n')
            .filter(|line| !line.is_empty())
            .map(|s| serde_json::from_str(s).map_err(ShopifyAPIError::JsonParseError))
            .collect::<Result<Vec<serde_json::Value>, _>>()
    }

    /// Prepares a staged upload for a bulk operation.
    ///
    /// # Arguments
    /// * `params` - Parameters for the staged upload.
    ///
    /// # Returns
    /// `Result<ShopifyStagedUploadsCreateInputQuery, ShopifyAPIError>` - The result of the upload preparation or an error.
    ///
    /// # Example
    /// ```ts,no_run
    /// let params = vec![StagedUploadsCreateInput { /* ... */ }];
    /// let staged_upload = shopify.stage_upload_prepare(params).await?;
    /// ```
    pub async fn stage_upload_prepare(
        &self,
        params: Vec<StagedUploadsCreateInput>,
    ) -> Result<ShopifyStagedUploadsCreateInputQuery, ShopifyAPIError> {
        self.graphql_query(
            r#"
        mutation stagedUploadsCreate($input: [StagedUploadInput!]!) {
            stagedUploadsCreate(input: $input) {
              stagedTargets {
                url
                resourceUrl
                parameters {
                  name
                  value
                }
              }
              userErrors {
                field
                message
              }
            }
          }
          "#,
            &json!({
                "input": params
            }),
            &vec![
                ReadJsonTreeSteps::Key("data"),
                ReadJsonTreeSteps::Key("stagedUploadsCreate"),
            ],
        )
        .await
    }

    /// Generates a URL for a staged upload.
    ///
    /// # Arguments
    /// * `filename` - The name of the file to upload.
    /// * `mime_type` - The MIME type of the file.
    ///
    /// # Returns
    /// `Result<StagedMediaUploadTarget, ShopifyAPIError>` - The generated URL for the upload or an error.
    ///
    /// # Example
    /// ```ts,no_run
    /// let filename = "data.json";
    /// let mime_type = "application/json";
    /// let upload_url = shopify.generate_staged_upload_url(filename, mime_type).await?;
    /// ```
    pub async fn generate_staged_upload_url(
        &self,
        filename: &str,
        mime_type: &str,
    ) -> Result<StagedMediaUploadTarget, ShopifyAPIError> {
        let staged_upload_input = StagedUploadsCreateInput {
            file_size: None,
            filename: filename.to_string(),
            http_method: Some("POST".to_string()),
            mime_type: mime_type.to_string(),
            resource: StagedUploadTargetGenerateUploadResourceInput::BulkMutationVariables,
        };

        let response = self.stage_upload_prepare(vec![staged_upload_input]).await?;

        if let Some(errors) = response.user_errors {
            if !errors.is_empty() {
                return Err(ShopifyAPIError::Other(
                    "Error while creating staged upload URL".to_string(),
                ));
            }
        }

        if let Some(staged_targets) = response.staged_targets {
            if let Some(target) = staged_targets.first() {
                return Ok(target.clone());
            }
        }

        Err(ShopifyAPIError::Other(
            "Unable to generate staged upload URL".to_string(),
        ))
    }

    /// Uploads a JSON file for a bulk operation.
    ///
    /// # Arguments
    /// * `data` - The JSON data to upload.
    ///
    /// # Returns
    /// `Result<String, ShopifyAPIError>` - The key of the uploaded object or an error.
    ///
    /// # Example
    /// ```ts,no_run
    /// let data = vec![json!({ "key": "value" })];
    /// let key = shopify.stage_upload_json(data).await?;
    /// ```
    pub async fn stage_upload_json(&self, data: Vec<Value>) -> Result<String, ShopifyAPIError> {
        let jsonl_data = data
            .into_iter()
            .map(|item| serde_json::to_string(&item).unwrap())
            .collect::<Vec<String>>()
            .join("\n");

        let upload_url = self
            .generate_staged_upload_url("bulk_op_vars", "application/jsonl")
            .await?;

        let mut form = reqwest::multipart::Form::new();

        let mut key = String::from("");

        for param in upload_url.parameters {
            if param.name == "key" {
                key = param.value.clone();
            }

            form = form.text(param.name, param.value);
        }

        let file_part = reqwest::multipart::Part::bytes(jsonl_data.as_bytes().to_vec())
            .file_name("bulk_op_vars")
            .mime_str("application/jsonl")
            .unwrap();

        form = form.part("file", file_part);

        let client = reqwest::Client::new();
        client
            .post(&upload_url.url)
            .multipart(form)
            .send()
            .await?
            .error_for_status()?;

        Ok(key)
    }
}