Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace to HTTPTypes.HTTPRequest from Helpers.HTTPRequest #568

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading