api-swift / Sources / IrdAPI / Generated / Credits.swift
1,485 words
  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
// Generated by scripts/gen-api.py from spec/openapi.json — do not edit.

import Foundation

// MARK: - Types

/// Used by GET /credits/balance.
public struct Balance: Codable, Sendable, Hashable {
    public var creditType: String
    public var balance: String

    public init(creditType: String, balance: String) {
        self.creditType = creditType
        self.balance = balance
    }

    enum CodingKeys: String, CodingKey {
        case creditType = "credit_type"
        case balance
    }
}

/// Used by GET /credits/balance.
public struct Spend: Codable, Sendable, Hashable {
    public var windowDays: Int
    public var total: String
    public var avgPerDay: String
    public var runwayDays: Int?

    public init(windowDays: Int, total: String, avgPerDay: String, runwayDays: Int? = nil) {
        self.windowDays = windowDays
        self.total = total
        self.avgPerDay = avgPerDay
        self.runwayDays = runwayDays
    }

    enum CodingKeys: String, CodingKey {
        case windowDays = "window_days"
        case total
        case avgPerDay = "avg_per_day"
        case runwayDays = "runway_days"
    }

    /// Explicit so required-but-nullable fields encode as `null` rather than vanishing.
    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(windowDays, forKey: .windowDays)
        try container.encode(total, forKey: .total)
        try container.encode(avgPerDay, forKey: .avgPerDay)
        try container.encode(runwayDays, forKey: .runwayDays)
    }
}

/// Used by GET /credits/balance.
public struct BalanceResponse: Codable, Sendable, Hashable {
    public var balances: [Balance]
    public var total: String
    public var spend: Spend

    public init(balances: [Balance], total: String, spend: Spend) {
        self.balances = balances
        self.total = total
        self.spend = spend
    }

    enum CodingKeys: String, CodingKey {
        case balances
        case total
        case spend
    }
}

/// Values this build does not know decode as `.unrecognized`.
public enum LedgerEntryKind: RawRepresentable, Codable, Sendable, Hashable {
    case grant
    case purchase
    case charge
    case adjustment
    case refund
    case expiry
    /// A value the server sent that this version of the package does not list.
    case unrecognized(String)

    /// Every value the spec listed when this file was generated.
    public static let known: [LedgerEntryKind] = [.grant, .purchase, .charge, .adjustment, .refund, .expiry]

    public init(rawValue: String) {
        switch rawValue {
        case "grant": self = .grant
        case "purchase": self = .purchase
        case "charge": self = .charge
        case "adjustment": self = .adjustment
        case "refund": self = .refund
        case "expiry": self = .expiry
        default: self = .unrecognized(rawValue)
        }
    }

    public var rawValue: String {
        switch self {
        case .grant: return "grant"
        case .purchase: return "purchase"
        case .charge: return "charge"
        case .adjustment: return "adjustment"
        case .refund: return "refund"
        case .expiry: return "expiry"
        case .unrecognized(let raw): return raw
        }
    }

    public init(from decoder: any Decoder) throws {
        self.init(rawValue: try decoder.singleValueContainer().decode(String.self))
    }

    public func encode(to encoder: any Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encode(rawValue)
    }
}

/// Used by GET /credits/ledger.
public struct LedgerResource: Codable, Sendable, Hashable {
    public var type: String?
    public var id: String?
    public var label: String?

    public init(type: String? = nil, id: String? = nil, label: String? = nil) {
        self.type = type
        self.id = id
        self.label = label
    }

    enum CodingKeys: String, CodingKey {
        case type
        case id
        case label
    }

    /// Explicit so required-but-nullable fields encode as `null` rather than vanishing.
    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(type, forKey: .type)
        try container.encode(id, forKey: .id)
        try container.encode(label, forKey: .label)
    }
}

/// Used by GET /credits/ledger.
public struct LedgerEntry: Codable, Sendable, Hashable, Identifiable {
    public var id: String
    public var seq: Int
    public var creditType: String
    public var amount: String
    public var kind: LedgerEntryKind
    public var reason: String?
    public var balanceAfter: String
    public var createdAt: String
    public var invoiceId: String?
    /// The resource a recurring (hourly) charge bills: its type (e.g. `dns/hosted-zone`, `dns/zone`, `vps/instance`), id, and a label when it has one (the hosted zone's domain).
    public var resource: LedgerResource?

    public init(id: String, seq: Int, creditType: String, amount: String, kind: LedgerEntryKind, reason: String? = nil, balanceAfter: String, createdAt: String, invoiceId: String? = nil, resource: LedgerResource? = nil) {
        self.id = id
        self.seq = seq
        self.creditType = creditType
        self.amount = amount
        self.kind = kind
        self.reason = reason
        self.balanceAfter = balanceAfter
        self.createdAt = createdAt
        self.invoiceId = invoiceId
        self.resource = resource
    }

    enum CodingKeys: String, CodingKey {
        case id
        case seq
        case creditType = "credit_type"
        case amount
        case kind
        case reason
        case balanceAfter = "balance_after"
        case createdAt = "created_at"
        case invoiceId = "invoice_id"
        case resource
    }

    /// Explicit so required-but-nullable fields encode as `null` rather than vanishing.
    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(seq, forKey: .seq)
        try container.encode(creditType, forKey: .creditType)
        try container.encode(amount, forKey: .amount)
        try container.encode(kind, forKey: .kind)
        try container.encode(reason, forKey: .reason)
        try container.encode(balanceAfter, forKey: .balanceAfter)
        try container.encode(createdAt, forKey: .createdAt)
        try container.encodeIfPresent(invoiceId, forKey: .invoiceId)
        try container.encodeIfPresent(resource, forKey: .resource)
    }
}

/// Used by GET /credits/ledger.
public struct LedgerResponse: Codable, Sendable, Hashable {
    public var entries: [LedgerEntry]
    public var nextCursor: Int?

    public init(entries: [LedgerEntry], nextCursor: Int? = nil) {
        self.entries = entries
        self.nextCursor = nextCursor
    }

    enum CodingKeys: String, CodingKey {
        case entries
        case nextCursor = "next_cursor"
    }

    /// Explicit so required-but-nullable fields encode as `null` rather than vanishing.
    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(entries, forKey: .entries)
        try container.encode(nextCursor, forKey: .nextCursor)
    }
}

/// Used by GET /credits/prices.
public struct RegistrarPrices: Codable, Sendable, Hashable {
    /// The admin-tunable cap on `years` in a single register, renew, transfer-in or trade order; above it those endpoints answer 400. It bounds ONE order, not how far ahead a domain may be held — at a cap of 1, nine consecutive renewals still carry a domain nine years forward. At most 10.
    public var maxYears: Int

    public init(maxYears: Int) {
        self.maxYears = maxYears
    }

    enum CodingKeys: String, CodingKey {
        case maxYears = "max_years"
    }
}

/// Used by GET /credits/prices.
public struct PricesResponse: Codable, Sendable, Hashable {
    public var version: Int
    public var createdAt: String
    public var prices: [String: [String: JSONValue]]
    /// Purchase constraints that travel with the price book, so a client can size its year selectors from the same call that prices them.
    public var registrar: RegistrarPrices

    public init(version: Int, createdAt: String, prices: [String: [String: JSONValue]], registrar: RegistrarPrices) {
        self.version = version
        self.createdAt = createdAt
        self.prices = prices
        self.registrar = registrar
    }

    enum CodingKeys: String, CodingKey {
        case version
        case createdAt = "created_at"
        case prices
        case registrar
    }
}

/// Query parameters of `ledger`; unset fields are omitted.
public struct LedgerQuery: Sendable, Hashable {
    /// Page size, capped at 200
    public var limit: Int?
    /// Opaque `seq` cursor from a previous page's `next_cursor`
    public var cursor: Int?
    /// Filter by credit type
    public var type: String?
    public var kind: LedgerEntryKind?
    /// Filter by the contract that produced the charge (dotted form, e.g. `domains.search`); empty string matches entries without one
    public var contract: String?
    /// Filter by the id of the resource a recurring charge bills (the `resource.id` of an entry); empty string matches entries without one
    public var resource: String?
    /// Restrict to one statement day (ISO date, Europe/Bucharest)
    public var day: String?

    public init(limit: Int? = nil, cursor: Int? = nil, type: String? = nil, kind: LedgerEntryKind? = nil, contract: String? = nil, resource: String? = nil, day: String? = nil) {
        self.limit = limit
        self.cursor = cursor
        self.type = type
        self.kind = kind
        self.contract = contract
        self.resource = resource
        self.day = day
    }

    /// The items in spec order.
    public var queryItems: [QueryItem] {
        var items: [QueryItem] = []
        if let value = limit { items.append(QueryItem("limit", String(value))) }
        if let value = cursor { items.append(QueryItem("cursor", String(value))) }
        if let value = type { items.append(QueryItem("type", value)) }
        if let value = kind { items.append(QueryItem("kind", value.rawValue)) }
        if let value = contract { items.append(QueryItem("contract", value)) }
        if let value = resource { items.append(QueryItem("resource", value)) }
        if let value = day { items.append(QueryItem("day", value)) }
        return items
    }
}

// MARK: - Operations

extension IrdOperation {
    public static let credits_balance = IrdOperation(method: .get, pathTemplate: "/credits/balance", module: "credits", name: "balance", security: .keyOrJwt)
    public static let credits_ledger = IrdOperation(method: .get, pathTemplate: "/credits/ledger", module: "credits", name: "ledger", security: .keyOrJwt)
    public static let credits_prices = IrdOperation(method: .get, pathTemplate: "/credits/prices", module: "credits", name: "prices", security: .keyOrJwt)

    static let creditsOperations: [IrdOperation] = [.credits_balance, .credits_ledger, .credits_prices]
}

/// The `credits` operations (3).
public struct CreditsAPI: Sendable {
    let client: IrdClient

    /// Credit balance
    /// The authenticated account's current balance per credit type plus the total, and a spend summary over the last 30 complete Europe/Bucharest calendar days: total spend (charges net of refunds), average per day, and `runway_days` — how many da…
    /// `GET /credits/balance` — 200
    public func balance() async throws -> BalanceResponse {
        try await client.request(.credits_balance)
    }

    /// Credit ledger history
    /// The authenticated account's append-only ledger history, newest first, cursor-paginated on `seq`.
    /// `GET /credits/ledger` — 200
    public func ledger(query: LedgerQuery = LedgerQuery()) async throws -> LedgerResponse {
        try await client.request(.credits_ledger, query: query.queryItems)
    }

    /// Current usage prices
    /// The active price book: what each endpoint and job type costs in credits (1 credit = 1 RON).
    /// `GET /credits/prices` — 200
    public func prices() async throws -> PricesResponse {
        try await client.request(.credits_prices)
    }
}

extension IrdClient {
    public var credits: CreditsAPI { CreditsAPI(client: self) }
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/api-swift.git