octocrab 0.54.2

A modern, extensible GitHub API client.
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
//! The GitHub Billing API.
//!
//! See: [GitHub REST API Documentation](https://docs.github.com/en/rest/billing?apiVersion=2022-11-28)

use crate::models::billing::{
    ActionsBillingUsage, BillingAiCreditUsageReport, BillingPremiumRequestUsageReport,
    BillingUsageReport, BillingUsageSummaryReport, Budget, CombinedBillingUsage, CreateBudget,
    CreateBudgetResponse, DeleteBudgetResponse, GetAllBudgets, PackagesBillingUsage, UpdateBudget,
    UpdateBudgetResponse,
};
use crate::{Octocrab, Result};

/// The target namespace/owner for billing operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BillingOwner {
    Org(String),
    User(String),
}

impl BillingOwner {
    /// Prefix for Actions, Packages, and Shared Storage endpoints:
    /// `/orgs/{org}/settings/billing` or `/users/{user}/settings/billing`
    pub(crate) fn legacy_prefix(&self) -> String {
        match self {
            Self::Org(org) => format!("/orgs/{org}/settings/billing"),
            Self::User(user) => format!("/users/{user}/settings/billing"),
        }
    }

    /// Prefix for Usage, Summaries, AI Credit, Premium Request, and Budgets:
    /// `/organizations/{org}/settings/billing` or `/users/{user}/settings/billing`
    pub(crate) fn usage_prefix(&self) -> String {
        match self {
            Self::Org(org) => format!("/organizations/{org}/settings/billing"),
            Self::User(user) => format!("/users/{user}/settings/billing"),
        }
    }
}

/// Entry-point handler for GitHub's Billing API.
///
/// Created with [`Octocrab::billing`].
pub struct BillingHandler<'octo> {
    crab: &'octo Octocrab,
}

impl<'octo> BillingHandler<'octo> {
    pub(crate) fn new(crab: &'octo Octocrab) -> Self {
        Self { crab }
    }

    /// Scope billing operations to an organization.
    pub fn org(&self, org: impl Into<String>) -> ScopedBillingHandler<'octo> {
        ScopedBillingHandler::new(self.crab, BillingOwner::Org(org.into()))
    }

    /// Scope billing operations to a user.
    pub fn user(&self, user: impl Into<String>) -> ScopedBillingHandler<'octo> {
        ScopedBillingHandler::new(self.crab, BillingOwner::User(user.into()))
    }
}

/// Handler for billing operations scoped to an organization or user.
///
/// Created via [`BillingHandler::org`], [`BillingHandler::user`],
/// [`OrgHandler::billing`][crate::api::orgs::OrgHandler::billing], or
/// [`UserHandler::billing`][crate::api::users::UserHandler::billing].
pub struct ScopedBillingHandler<'octo> {
    crab: &'octo Octocrab,
    owner: BillingOwner,
}

impl<'octo> ScopedBillingHandler<'octo> {
    pub(crate) fn new(crab: &'octo Octocrab, owner: BillingOwner) -> Self {
        Self { crab, owner }
    }

    /// Scope billing operations to another organization.
    pub fn org(&self, org: impl Into<String>) -> Self {
        Self::new(self.crab, BillingOwner::Org(org.into()))
    }

    /// Scope billing operations to another user.
    pub fn user(&self, user: impl Into<String>) -> Self {
        Self::new(self.crab, BillingOwner::User(user.into()))
    }

    /// Get GitHub Actions billing usage for the organization or user.
    ///
    /// See:
    /// - [Get GitHub Actions billing for an organization](https://docs.github.com/en/rest/billing?apiVersion=2022-11-28#get-github-actions-billing-for-an-organization)
    /// - [Get GitHub Actions billing for a user](https://docs.github.com/en/rest/billing?apiVersion=2022-11-28#get-github-actions-billing-for-a-user)
    pub async fn actions(&self) -> Result<ActionsBillingUsage> {
        let route = format!("{}/actions", self.owner.legacy_prefix());
        self.crab.get(route, None::<&()>).await
    }

    /// Get GitHub Packages billing usage for the organization or user.
    ///
    /// See:
    /// - [Get GitHub Packages billing for an organization](https://docs.github.com/en/rest/billing?apiVersion=2022-11-28#get-github-packages-billing-for-an-organization)
    /// - [Get GitHub Packages billing for a user](https://docs.github.com/en/rest/billing?apiVersion=2022-11-28#get-github-packages-billing-for-a-user)
    pub async fn packages(&self) -> Result<PackagesBillingUsage> {
        let route = format!("{}/packages", self.owner.legacy_prefix());
        self.crab.get(route, None::<&()>).await
    }

    /// Get shared storage billing usage for the organization or user.
    ///
    /// See:
    /// - [Get shared storage billing for an organization](https://docs.github.com/en/rest/billing?apiVersion=2022-11-28#get-shared-storage-billing-for-an-organization)
    /// - [Get shared storage billing for a user](https://docs.github.com/en/rest/billing?apiVersion=2022-11-28#get-shared-storage-billing-for-a-user)
    pub async fn shared_storage(&self) -> Result<CombinedBillingUsage> {
        let route = format!("{}/shared-storage", self.owner.legacy_prefix());
        self.crab.get(route, None::<&()>).await
    }

    /// Builder for getting billing usage report for the organization or user.
    ///
    /// See:
    /// - [Get billing usage report for an organization](https://docs.github.com/en/rest/billing/usage#get-billing-usage-report-for-an-organization)
    /// - [Get billing usage report for a user](https://docs.github.com/en/rest/billing/usage#get-billing-usage-report-for-a-user)
    pub fn usage(&self, year: u32) -> GetBillingUsageBuilder<'octo, '_> {
        GetBillingUsageBuilder::new(self, year)
    }

    /// Builder for getting billing usage summary for the organization or user.
    ///
    /// See:
    /// - [Get billing usage summary for an organization](https://docs.github.com/en/rest/billing/usage#get-billing-usage-summary-for-an-organization)
    /// - [Get billing usage summary for a user](https://docs.github.com/en/rest/billing/usage#get-billing-usage-summary-for-a-user)
    pub fn usage_summary(&self, year: u32) -> GetBillingUsageSummaryBuilder<'octo, '_> {
        GetBillingUsageSummaryBuilder::new(self, year)
    }

    /// Builder for getting billing AI credit usage report for the organization or user.
    ///
    /// See:
    /// - [Get billing AI credit usage report for an organization](https://docs.github.com/en/rest/billing/usage#get-billing-ai-credit-usage-report-for-an-organization)
    /// - [Get billing AI credit usage report for a user](https://docs.github.com/en/rest/billing/usage#get-billing-ai-credit-usage-report-for-a-user)
    pub fn ai_credit_usage(&self, year: u32) -> GetBillingAiCreditUsageBuilder<'octo, '_> {
        GetBillingAiCreditUsageBuilder::new(self, year)
    }

    /// Builder for getting billing premium request usage report for the organization or user.
    ///
    /// See:
    /// - [Get billing premium request usage report for an organization](https://docs.github.com/en/rest/billing/usage#get-billing-premium-request-usage-report-for-an-organization)
    /// - [Get billing premium request usage report for a user](https://docs.github.com/en/rest/billing/usage#get-billing-premium-request-usage-report-for-a-user)
    pub fn premium_request_usage(
        &self,
        year: u32,
    ) -> GetBillingPremiumRequestUsageBuilder<'octo, '_> {
        GetBillingPremiumRequestUsageBuilder::new(self, year)
    }

    /// Access organization budgets management API.
    ///
    /// Note: Budgets are an organization-level feature.
    ///
    /// See: [Budgets](https://docs.github.com/en/rest/billing/budgets)
    pub fn budgets(&self) -> OrgBudgetsHandler<'octo, '_> {
        OrgBudgetsHandler::new(self)
    }
}

/// Builder for getting billing usage reports.
#[derive(serde::Serialize)]
pub struct GetBillingUsageBuilder<'octo, 'r> {
    #[serde(skip)]
    handler: &'r ScopedBillingHandler<'octo>,
    year: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    month: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    day: Option<u8>,
}

impl<'octo, 'r> GetBillingUsageBuilder<'octo, 'r> {
    pub(crate) fn new(handler: &'r ScopedBillingHandler<'octo>, year: u32) -> Self {
        Self {
            handler,
            year,
            month: None,
            day: None,
        }
    }

    pub fn month(mut self, month: impl Into<u8>) -> Self {
        self.month = Some(month.into());
        self
    }

    pub fn day(mut self, day: impl Into<u8>) -> Self {
        self.day = Some(day.into());
        self
    }

    pub async fn send(self) -> Result<BillingUsageReport> {
        let route = format!("{}/usage", self.handler.owner.usage_prefix());
        self.handler.crab.get(route, Some(&self)).await
    }
}

/// Builder for getting billing usage summary reports.
#[derive(serde::Serialize)]
pub struct GetBillingUsageSummaryBuilder<'octo, 'r> {
    #[serde(skip)]
    handler: &'r ScopedBillingHandler<'octo>,
    year: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    month: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    day: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    repository: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    product: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    sku: Option<String>,
}

impl<'octo, 'r> GetBillingUsageSummaryBuilder<'octo, 'r> {
    pub(crate) fn new(handler: &'r ScopedBillingHandler<'octo>, year: u32) -> Self {
        Self {
            handler,
            year,
            month: None,
            day: None,
            repository: None,
            product: None,
            sku: None,
        }
    }

    pub fn month(mut self, month: impl Into<u8>) -> Self {
        self.month = Some(month.into());
        self
    }

    pub fn day(mut self, day: impl Into<u8>) -> Self {
        self.day = Some(day.into());
        self
    }

    pub fn repository(mut self, repository: impl Into<String>) -> Self {
        self.repository = Some(repository.into());
        self
    }

    pub fn product(mut self, product: impl Into<String>) -> Self {
        self.product = Some(product.into());
        self
    }

    pub fn sku(mut self, sku: impl Into<String>) -> Self {
        self.sku = Some(sku.into());
        self
    }

    pub async fn send(self) -> Result<BillingUsageSummaryReport> {
        let route = format!("{}/usage/summary", self.handler.owner.usage_prefix());
        self.handler.crab.get(route, Some(&self)).await
    }
}

/// Builder for getting billing AI credit usage reports.
#[derive(serde::Serialize)]
pub struct GetBillingAiCreditUsageBuilder<'octo, 'r> {
    #[serde(skip)]
    handler: &'r ScopedBillingHandler<'octo>,
    year: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    month: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    day: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    user: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    model: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    product: Option<String>,
}

impl<'octo, 'r> GetBillingAiCreditUsageBuilder<'octo, 'r> {
    pub(crate) fn new(handler: &'r ScopedBillingHandler<'octo>, year: u32) -> Self {
        Self {
            handler,
            year,
            month: None,
            day: None,
            user: None,
            model: None,
            product: None,
        }
    }

    pub fn month(mut self, month: impl Into<u8>) -> Self {
        self.month = Some(month.into());
        self
    }

    pub fn day(mut self, day: impl Into<u8>) -> Self {
        self.day = Some(day.into());
        self
    }

    pub fn user(mut self, user: impl Into<String>) -> Self {
        self.user = Some(user.into());
        self
    }

    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    pub fn product(mut self, product: impl Into<String>) -> Self {
        self.product = Some(product.into());
        self
    }

    pub async fn send(self) -> Result<BillingAiCreditUsageReport> {
        let route = format!("{}/ai_credit/usage", self.handler.owner.usage_prefix());
        self.handler.crab.get(route, Some(&self)).await
    }
}

/// Builder for getting billing premium request usage reports.
#[derive(serde::Serialize)]
pub struct GetBillingPremiumRequestUsageBuilder<'octo, 'r> {
    #[serde(skip)]
    handler: &'r ScopedBillingHandler<'octo>,
    year: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    month: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    day: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    user: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    model: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    product: Option<String>,
}

impl<'octo, 'r> GetBillingPremiumRequestUsageBuilder<'octo, 'r> {
    pub(crate) fn new(handler: &'r ScopedBillingHandler<'octo>, year: u32) -> Self {
        Self {
            handler,
            year,
            month: None,
            day: None,
            user: None,
            model: None,
            product: None,
        }
    }

    pub fn month(mut self, month: impl Into<u8>) -> Self {
        self.month = Some(month.into());
        self
    }

    pub fn day(mut self, day: impl Into<u8>) -> Self {
        self.day = Some(day.into());
        self
    }

    pub fn user(mut self, user: impl Into<String>) -> Self {
        self.user = Some(user.into());
        self
    }

    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    pub fn product(mut self, product: impl Into<String>) -> Self {
        self.product = Some(product.into());
        self
    }

    pub async fn send(self) -> Result<BillingPremiumRequestUsageReport> {
        let route = format!(
            "{}/premium_request/usage",
            self.handler.owner.usage_prefix()
        );
        self.handler.crab.get(route, Some(&self)).await
    }
}

/// Handler for organization budget operations.
pub struct OrgBudgetsHandler<'octo, 'r> {
    handler: &'r ScopedBillingHandler<'octo>,
}

impl<'octo, 'r> OrgBudgetsHandler<'octo, 'r> {
    pub(crate) fn new(handler: &'r ScopedBillingHandler<'octo>) -> Self {
        Self { handler }
    }

    /// List all budgets for an organization.
    ///
    /// See: [Get all budgets for an organization](https://docs.github.com/en/rest/billing/budgets#get-all-budgets-for-an-organization)
    pub fn list(&self) -> ListBudgetsBuilder<'octo, 'r, '_> {
        ListBudgetsBuilder::new(self)
    }

    /// Create a budget for an organization.
    ///
    /// See: [Create a budget for an organization](https://docs.github.com/en/rest/billing/budgets#create-a-budget-for-an-organization)
    pub async fn create(&self, body: &CreateBudget) -> Result<CreateBudgetResponse> {
        let route = format!("{}/budgets", self.handler.owner.usage_prefix());
        self.handler.crab.post(route, Some(body)).await
    }

    /// Get a budget by ID for an organization.
    ///
    /// See: [Get a budget by ID for an organization](https://docs.github.com/en/rest/billing/budgets#get-a-budget-by-id-for-an-organization)
    pub async fn get(&self, budget_id: impl AsRef<str>) -> Result<Budget> {
        let route = format!(
            "{}/budgets/{}",
            self.handler.owner.usage_prefix(),
            budget_id.as_ref()
        );
        self.handler.crab.get(route, None::<&()>).await
    }

    /// Update a budget for an organization.
    ///
    /// See: [Update a budget for an organization](https://docs.github.com/en/rest/billing/budgets#update-a-budget-for-an-organization)
    pub async fn update(
        &self,
        budget_id: impl AsRef<str>,
        body: &UpdateBudget,
    ) -> Result<UpdateBudgetResponse> {
        let route = format!(
            "{}/budgets/{}",
            self.handler.owner.usage_prefix(),
            budget_id.as_ref()
        );
        self.handler.crab.patch(route, Some(body)).await
    }

    /// Delete a budget for an organization.
    ///
    /// See: [Delete a budget for an organization](https://docs.github.com/en/rest/billing/budgets#delete-a-budget-for-an-organization)
    pub async fn delete(&self, budget_id: impl AsRef<str>) -> Result<DeleteBudgetResponse> {
        let route = format!(
            "{}/budgets/{}",
            self.handler.owner.usage_prefix(),
            budget_id.as_ref()
        );
        self.handler.crab.delete(route, None::<&()>).await
    }
}

/// Builder for listing budgets for an organization.
#[derive(serde::Serialize)]
pub struct ListBudgetsBuilder<'octo, 'r, 'b> {
    #[serde(skip)]
    budgets_handler: &'b OrgBudgetsHandler<'octo, 'r>,
    #[serde(skip_serializing_if = "Option::is_none")]
    page: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    per_page: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    scope: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    user: Option<String>,
}

impl<'octo, 'r, 'b> ListBudgetsBuilder<'octo, 'r, 'b> {
    pub(crate) fn new(budgets_handler: &'b OrgBudgetsHandler<'octo, 'r>) -> Self {
        Self {
            budgets_handler,
            page: None,
            per_page: None,
            scope: None,
            user: None,
        }
    }

    pub fn page(mut self, page: impl Into<u32>) -> Self {
        self.page = Some(page.into());
        self
    }

    pub fn per_page(mut self, per_page: impl Into<u8>) -> Self {
        self.per_page = Some(per_page.into());
        self
    }

    pub fn scope(mut self, scope: impl Into<String>) -> Self {
        self.scope = Some(scope.into());
        self
    }

    pub fn user(mut self, user: impl Into<String>) -> Self {
        self.user = Some(user.into());
        self
    }

    pub async fn send(self) -> Result<GetAllBudgets> {
        let route = format!(
            "{}/budgets",
            self.budgets_handler.handler.owner.usage_prefix()
        );
        self.budgets_handler
            .handler
            .crab
            .get(route, Some(&self))
            .await
    }
}