yandex-webmaster-api 1.0.5

Rust client for the Yandex Webmaster API
Documentation
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
use reqwest_middleware::ClientBuilder;
use serde_json::json;
use serde_qs::ArrayFormat;
use tracing::instrument;

use crate::{
    dto::*,
    error::{Result, YandexApiErrorResponse, YandexWebmasterError},
    middleware::AuthMiddleware,
};

/// Base URL for the Yandex Webmaster API
const API_BASE_URL: &str = "https://api.webmaster.yandex.net/v4";

/// Client for interacting with the Yandex Webmaster API
#[derive(Debug, Clone)]
pub struct YandexWebmasterClient {
    client: reqwest_middleware::ClientWithMiddleware,
    user_id: i64,
    qs: serde_qs::Config,
}

impl YandexWebmasterClient {
    /// Create OAuth url to get token
    pub fn oauth_url(client_id: &str) -> String {
        format!("https://oauth.yandex.ru/authorize?response_type=token&client_id={client_id}")
    }

    /// Creates a new Yandex Webmaster API client
    ///
    /// # Arguments
    ///
    /// * `oauth_token` - OAuth token for authentication
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The HTTP client cannot be created
    /// - The user information cannot be fetched
    /// - The OAuth token is invalid
    #[instrument(skip(oauth_token))]
    pub async fn new(oauth_token: String) -> Result<Self> {
        // Build the HTTP client with middleware
        let client = ClientBuilder::new(reqwest::Client::new())
            .with(AuthMiddleware::new(oauth_token))
            .build();

        // Fetch user information
        let user_response = Self::fetch_user(&client).await?;

        tracing::info!(
            user_id = user_response.user_id,
            "Successfully authenticated"
        );

        Ok(Self {
            client,
            user_id: user_response.user_id,
            qs: serde_qs::Config::new().array_format(ArrayFormat::Unindexed),
        })
    }

    /// Creates a new Yandex Webmaster API client
    ///
    /// # Arguments
    ///
    /// * `oauth_token` - OAuth token for authentication
    /// * `client` - Client builder with preconfigured middleware
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The HTTP client cannot be created
    /// - The user information cannot be fetched
    /// - The OAuth token is invalid
    #[instrument(skip(oauth_token, client))]
    pub async fn with_client(oauth_token: String, client: ClientBuilder) -> Result<Self> {
        // Build the HTTP client with middleware
        let client = client.with(AuthMiddleware::new(oauth_token)).build();

        // Fetch user information
        let user_response = Self::fetch_user(&client).await?;

        tracing::info!(
            user_id = user_response.user_id,
            "Successfully authenticated"
        );

        Ok(Self {
            client,
            user_id: user_response.user_id,
            qs: serde_qs::Config::new().array_format(ArrayFormat::Unindexed),
        })
    }

    /// Fetches user information from the API
    #[instrument(skip(client))]
    async fn fetch_user(client: &reqwest_middleware::ClientWithMiddleware) -> Result<UserResponse> {
        let url = format!("{}/user", API_BASE_URL);

        tracing::debug!(url = %url, "Fetching user information");

        let response = client.get(&url).send().await?;

        if !response.status().is_success() {
            return Err(Self::parse_error(response).await);
        }

        let user_response: UserResponse = response.json().await?;

        Ok(user_response)
    }

    /// Returns the user ID
    pub fn user_id(&self) -> i64 {
        self.user_id
    }

    // ============================================================================
    // Hosts Management
    // ============================================================================

    /// List all sites for the user
    #[instrument(skip(self))]
    pub async fn get_hosts(&self) -> Result<Vec<HostInfo>> {
        let url = format!("{}/user/{}/hosts", API_BASE_URL, self.user_id);
        let result: HostsResponse = self.get(&url).await?;
        Ok(result.hosts)
    }

    /// Add a new site
    #[instrument(skip(self))]
    pub async fn add_host(
        &self,
        host_url: &str,
        verification_type: VerificationType,
    ) -> Result<AddHostResponse> {
        let url = format!("{}/user/{}/hosts", API_BASE_URL, self.user_id);
        self.post(
            &url,
            &json!({ "host_url": host_url.to_string(), "verification_type": verification_type }),
        )
        .await
    }

    /// Get information about a specific site
    #[instrument(skip(self))]
    pub async fn get_host(&self, host_id: &str) -> Result<FullHostInfo> {
        let url = format!("{}/user/{}/hosts/{}", API_BASE_URL, self.user_id, host_id);
        self.get(&url).await
    }

    /// Delete a site
    #[instrument(skip(self))]
    pub async fn delete_host(&self, host_id: &str) -> Result<()> {
        let url = format!("{}/user/{}/hosts/{}", API_BASE_URL, self.user_id, host_id);
        self.delete(&url).await
    }

    // ============================================================================
    // Host Verification
    // ============================================================================

    /// Get verification status for a site
    #[instrument(skip(self))]
    pub async fn get_verification_status(&self, host_id: &str) -> Result<HostVerificationResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/verification",
            API_BASE_URL, self.user_id, host_id
        );
        self.get(&url).await
    }

    /// Initiate verification procedure for a site
    #[instrument(skip(self))]
    pub async fn verify_host(
        &self,
        host_id: &str,
        verification_type: ExplicitVerificationType,
    ) -> Result<HostVerificationResponse> {
        let serialized = serde_json::to_value(verification_type)?;
        let verification_type = serialized.clone().as_str().unwrap_or("").to_owned();

        let url = format!(
            "{}/user/{}/hosts/{}/verification?verification_type={}",
            API_BASE_URL, self.user_id, host_id, verification_type
        );
        self.post(&url, &()).await
    }

    /// Get list of verified owners for a site
    #[instrument(skip(self))]
    pub async fn get_owners(&self, host_id: &str) -> Result<Vec<Owner>> {
        let url = format!(
            "{}/user/{}/hosts/{}/owners",
            API_BASE_URL, self.user_id, host_id
        );
        let result: OwnersResponse = self.get(&url).await?;
        Ok(result.users)
    }

    // ============================================================================
    // Site Statistics
    // ============================================================================

    /// Get site summary statistics
    #[instrument(skip(self))]
    pub async fn get_host_summary(&self, host_id: &str) -> Result<HostSummaryResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/summary",
            API_BASE_URL, self.user_id, host_id
        );
        self.get(&url).await
    }

    /// Get site quality index history
    #[instrument(skip(self))]
    pub async fn get_sqi_history(
        &self,
        host_id: &str,
        req: SqiHistoryRequest,
    ) -> Result<Vec<SqiPoint>> {
        let url = format!(
            "{}/user/{}/hosts/{}/sqi-history?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(&req)?
        );
        let result: SqiHistoryResponse = self.get(&url).await?;
        Ok(result.points)
    }

    // ============================================================================
    // Search Queries
    // ============================================================================

    /// Get popular search queries for a site
    #[instrument(skip(self))]
    pub async fn get_popular_queries(
        &self,
        host_id: &str,
        request: &PopularQueriesRequest,
    ) -> Result<PopularQueriesResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/search-queries/popular?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get overall query statistics history
    #[instrument(skip(self))]
    pub async fn get_query_analytics(
        &self,
        host_id: &str,
        request: &QueryAnalyticsRequest,
    ) -> Result<QueryAnalyticsResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/search-queries/all/history?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get statistics for a specific query
    #[instrument(skip(self))]
    pub async fn get_query_history(
        &self,
        host_id: &str,
        query_id: &str,
        request: &QueryHistoryRequest,
    ) -> Result<QueryHistoryResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/search-queries/{}/history?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            query_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    // ============================================================================
    // Sitemaps
    // ============================================================================

    /// Get list of all sitemap files
    #[instrument(skip(self))]
    pub async fn get_sitemaps(
        &self,
        host_id: &str,
        request: &GetSitemapsRequest,
    ) -> Result<SitemapsResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/sitemaps?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get details of a specific sitemap
    #[instrument(skip(self))]
    pub async fn get_sitemap(&self, host_id: &str, sitemap_id: &str) -> Result<SitemapInfo> {
        let url = format!(
            "{}/user/{}/hosts/{}/sitemaps/{}",
            API_BASE_URL, self.user_id, host_id, sitemap_id
        );
        self.get(&url).await
    }

    /// Get list of user-submitted sitemaps
    #[instrument(skip(self))]
    pub async fn get_user_sitemaps(
        &self,
        host_id: &str,
        request: &GetUserSitemapsRequest,
    ) -> Result<UserSitemapsResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/user-added-sitemaps?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Add a new sitemap file
    #[instrument(skip(self))]
    pub async fn add_sitemap(&self, host_id: &str, url: &str) -> Result<AddSitemapResponse> {
        let body = json!({ "url": url.to_string() });
        let url = format!(
            "{}/user/{}/hosts/{}/user-added-sitemaps",
            API_BASE_URL, self.user_id, host_id
        );
        self.post(&url, &body).await
    }

    /// Get user-submitted sitemap details
    #[instrument(skip(self))]
    pub async fn get_user_sitemap(
        &self,
        host_id: &str,
        sitemap_id: &str,
    ) -> Result<UserSitemapInfo> {
        let url = format!(
            "{}/user/{}/hosts/{}/user-added-sitemaps/{}",
            API_BASE_URL, self.user_id, host_id, sitemap_id
        );
        self.get(&url).await
    }

    /// Delete a user-submitted sitemap
    #[instrument(skip(self))]
    pub async fn delete_sitemap(&self, host_id: &str, sitemap_id: &str) -> Result<()> {
        let url = format!(
            "{}/user/{}/hosts/{}/user-added-sitemaps/{}",
            API_BASE_URL, self.user_id, host_id, sitemap_id
        );
        self.delete(&url).await
    }

    // ============================================================================
    // Indexing
    // ============================================================================

    /// Get indexing history
    #[instrument(skip(self))]
    pub async fn get_indexing_history(
        &self,
        host_id: &str,
        request: &IndexingHistoryRequest,
    ) -> Result<IndexingHistoryResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/indexing/history?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get sample indexed pages
    #[instrument(skip(self))]
    pub async fn get_indexing_samples(
        &self,
        host_id: &str,
        request: &GetIndexingSamplesRequest,
    ) -> Result<IndexingSamplesResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/indexing/samples?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get pages in search history
    #[instrument(skip(self))]
    pub async fn get_search_urls_history(
        &self,
        host_id: &str,
        request: &IndexingHistoryRequest,
    ) -> Result<SearchUrlsHistoryResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/search-urls/in-search/history?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get sample pages in search
    #[instrument(skip(self))]
    pub async fn get_search_urls_samples(
        &self,
        host_id: &str,
        request: &GetSearchUrlsSamplesRequest,
    ) -> Result<SearchUrlsSamplesResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/search-urls/in-search/samples?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get page appearance/removal history
    #[instrument(skip(self))]
    pub async fn get_search_events_history(
        &self,
        host_id: &str,
        request: &IndexingHistoryRequest,
    ) -> Result<SearchEventsHistoryResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/search-urls/events/history?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get sample page changes
    #[instrument(skip(self))]
    pub async fn get_search_events_samples(
        &self,
        host_id: &str,
        request: &GetSearchEventsSamplesRequest,
    ) -> Result<SearchEventsSamplesResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/search-urls/events/samples?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    // ============================================================================
    // Important URLs
    // ============================================================================

    /// Get list of important URLs
    #[instrument(skip(self))]
    pub async fn get_important_urls(&self, host_id: &str) -> Result<ImportantUrlsResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/important-urls",
            API_BASE_URL, self.user_id, host_id
        );
        self.get(&url).await
    }

    /// Get important URLs history
    #[instrument(skip(self))]
    pub async fn get_important_urls_history(
        &self,
        host_id: &str,
        url_param: &str,
    ) -> Result<ImportantUrlHistoryResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/important-urls/history?url={}",
            API_BASE_URL,
            self.user_id,
            host_id,
            urlencoding::encode(url_param)
        );
        self.get(&url).await
    }

    // ============================================================================
    // Recrawl Management
    // ============================================================================

    /// Request page recrawl
    #[instrument(skip(self))]
    pub async fn recrawl_urls(&self, host_id: &str, url: &str) -> Result<RecrawlResponse> {
        let body = json!({ "url": url });
        let url = format!(
            "{}/user/{}/hosts/{}/recrawl/queue",
            API_BASE_URL, self.user_id, host_id
        );
        self.post(&url, &body).await
    }

    /// Get list of recrawl tasks
    #[instrument(skip(self))]
    pub async fn get_recrawl_tasks(
        &self,
        host_id: &str,
        request: &GetRecrawlTasksRequest,
    ) -> Result<RecrawlTasksResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/recrawl/queue?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get recrawl task status
    #[instrument(skip(self))]
    pub async fn get_recrawl_task(&self, host_id: &str, task_id: &str) -> Result<RecrawlTask> {
        let url = format!(
            "{}/user/{}/hosts/{}/recrawl/queue/{}",
            API_BASE_URL, self.user_id, host_id, task_id
        );
        self.get(&url).await
    }

    /// Get recrawl quota
    #[instrument(skip(self))]
    pub async fn get_recrawl_quota(&self, host_id: &str) -> Result<RecrawlQuotaResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/recrawl/quota",
            API_BASE_URL, self.user_id, host_id
        );
        self.get(&url).await
    }

    // ============================================================================
    // Links
    // ============================================================================

    /// Get broken internal links samples
    #[instrument(skip(self))]
    pub async fn get_broken_links(
        &self,
        host_id: &str,
        request: &BrokenLinksRequest,
    ) -> Result<BrokenLinksResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/links/internal/broken/samples?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get broken links history
    #[instrument(skip(self))]
    pub async fn get_broken_links_history(
        &self,
        host_id: &str,
        request: &BrokenLinkHistoryRequest,
    ) -> Result<BrokenLinkHistoryResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/links/internal/broken/history?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get external backlinks samples
    #[instrument(skip(self))]
    pub async fn get_external_links(
        &self,
        host_id: &str,
        request: &ExternalLinksRequest,
    ) -> Result<ExternalLinksResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/links/external/samples?{}",
            API_BASE_URL,
            self.user_id,
            host_id,
            self.qs.serialize_string(request)?
        );
        self.get(&url).await
    }

    /// Get backlinks history
    #[instrument(skip(self))]
    pub async fn get_external_links_history(
        &self,
        host_id: &str,
    ) -> Result<ExternalLinksHistoryResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/links/external/history?indicator=LINKS_TOTAL_COUNT",
            API_BASE_URL, self.user_id, host_id
        );
        self.get(&url).await
    }

    // ============================================================================
    // Diagnostics
    // ============================================================================

    /// Get site diagnostic report
    #[instrument(skip(self))]
    pub async fn get_diagnostics(&self, host_id: &str) -> Result<DiagnosticsResponse> {
        let url = format!(
            "{}/user/{}/hosts/{}/diagnostics",
            API_BASE_URL, self.user_id, host_id
        );
        self.get(&url).await
    }

    // ============================================================================
    // Helper Methods
    // ============================================================================

    /// Generic GET request helper
    #[instrument(skip(self))]
    async fn get<T: serde::de::DeserializeOwned>(&self, url: &str) -> Result<T> {
        tracing::debug!(url = %url, "Making GET request");

        let response = self.client.get(url).send().await?;

        Self::handle_response(response).await
    }

    /// Generic POST request helper
    #[instrument(skip(self, body))]
    async fn post<B: serde::Serialize, T: serde::de::DeserializeOwned>(
        &self,
        url: &str,
        body: &B,
    ) -> Result<T> {
        tracing::debug!(url = %url, "Making POST request");

        let json_body = serde_json::to_string(body)?;

        let response = self
            .client
            .post(url)
            .header("Content-Type", "application/json")
            .body(json_body)
            .send()
            .await?;

        Self::handle_response(response).await
    }

    /// Generic DELETE request helper
    #[instrument(skip(self))]
    async fn delete(&self, url: &str) -> Result<()> {
        tracing::debug!(url = %url, "Making DELETE request");

        let response = self.client.delete(url).send().await?;

        if !response.status().is_success() {
            return Err(Self::parse_error(response).await);
        }

        Ok(())
    }

    /// Parse API error response
    #[instrument(skip(response))]
    async fn parse_error(response: reqwest::Response) -> YandexWebmasterError {
        let status = response.status();
        let status_code = status.as_u16();

        // Try to parse structured error response
        match response.text().await {
            Ok(error_text) => {
                // Try to parse as structured Yandex API error
                match serde_json::from_str::<YandexApiErrorResponse>(&error_text) {
                    Ok(api_error) => {
                        tracing::error!(
                            status = %status,
                            error_code = %api_error.error_code,
                            error_message = %api_error.error_message,
                            "Structured API error"
                        );
                        YandexWebmasterError::ApiError {
                            status: status_code,
                            response: api_error,
                        }
                    }
                    Err(_) => {
                        // Fallback to generic error
                        tracing::error!(
                            status = %status,
                            error = %error_text,
                            "API request failed with unstructured error"
                        );
                        YandexWebmasterError::GenericApiError(format!(
                            "Status: {}, Error: {}",
                            status, error_text
                        ))
                    }
                }
            }
            Err(e) => {
                tracing::error!(
                    status = %status,
                    error = %e,
                    "Failed to read error response"
                );
                YandexWebmasterError::GenericApiError(format!(
                    "Status: {}, Failed to read error response: {}",
                    status, e
                ))
            }
        }
    }

    /// Handle API response
    #[instrument(skip(response))]
    async fn handle_response<T: serde::de::DeserializeOwned>(
        response: reqwest::Response,
    ) -> Result<T> {
        if !response.status().is_success() {
            return Err(Self::parse_error(response).await);
        }

        let data: T = response.json().await?;
        Ok(data)
    }
}