Skip to content

Commit

Permalink
replace HTTPRequest
Browse files Browse the repository at this point in the history
format
  • Loading branch information
zunda-pixel committed Nov 30, 2024
1 parent 9eac916 commit 1b370ff
Show file tree
Hide file tree
Showing 56 changed files with 1,182 additions and 995 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ let package = Package(
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "HTTPTypesFoundation", package: "swift-http-types"),
]
),
.testTarget(
Expand Down
45 changes: 25 additions & 20 deletions Sources/Auth/AuthAdmin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//

import Foundation
import Helpers
import HTTPTypes
import Helpers

public struct AuthAdmin: Sendable {
let clientID: AuthClientID
Expand All @@ -25,12 +25,12 @@ public struct AuthAdmin: Sendable {
/// - Warning: Never expose your `service_role` key on the client.
public func deleteUser(id: String, shouldSoftDelete: Bool = false) async throws {
_ = try await api.execute(
HTTPRequest(
url: configuration.url.appendingPathComponent("admin/users/\(id)"),
for: HTTPRequest(
method: .delete,
body: encoder.encode(
DeleteUserRequest(shouldSoftDelete: shouldSoftDelete)
)
url: configuration.url.appendingPathComponent("admin/users/\(id)")
),
from: encoder.encode(
DeleteUserRequest(shouldSoftDelete: shouldSoftDelete)
)
)
}
Expand All @@ -46,30 +46,35 @@ public struct AuthAdmin: Sendable {
let aud: String
}

let httpResponse = try await api.execute(
HTTPRequest(
url: configuration.url.appendingPathComponent("admin/users"),
let (data, response) = try await api.execute(
for: HTTPRequest(
method: .get,
query: [
URLQueryItem(name: "page", value: params?.page?.description ?? ""),
URLQueryItem(name: "per_page", value: params?.perPage?.description ?? ""),
]
)
url: configuration.url
.appendingPathComponent("admin/users")
.appendingQueryItems([
URLQueryItem(name: "page", value: params?.page?.description ?? ""),
URLQueryItem(name: "per_page", value: params?.perPage?.description ?? ""),
])
),
from: nil
)

let response = try httpResponse.decoded(as: Response.self, decoder: configuration.decoder)
let responseData = try configuration.decoder.decode(Response.self, from: data)

var pagination = ListUsersPaginatedResponse(
users: response.users,
aud: response.aud,
users: responseData.users,
aud: responseData.aud,
lastPage: 0,
total: httpResponse.headers[.xTotalCount].flatMap(Int.init) ?? 0
total: response.headerFields[.xTotalCount].flatMap(Int.init) ?? 0
)

let links = httpResponse.headers[.link]?.components(separatedBy: ",") ?? []
let links = response.headerFields[.link]?.components(separatedBy: ",") ?? []
if !links.isEmpty {
for link in links {
let page = link.components(separatedBy: ";")[0].components(separatedBy: "=")[1].prefix(while: \.isNumber)
let page = link
.components(separatedBy: ";")[0]
.components(separatedBy: "=")[1]
.prefix(while: \.isNumber)
let rel = link.components(separatedBy: ";")[1].components(separatedBy: "=")[1]

if rel == "\"last\"", let lastPage = Int(page) {
Expand Down
Loading

0 comments on commit 1b370ff

Please sign in to comment.