Skip to content

Commit 2beba3d

Browse files
committed
Remove deprecated client-based fetchMetadata functions
1 parent b75eca5 commit 2beba3d

File tree

3 files changed

+51
-82
lines changed

3 files changed

+51
-82
lines changed

Sources/App/Core/Dependencies/HTTPClient.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ extension HTTPClient.Response {
106106
self.init(host: "host", status: status, version: version, headers: headers, body: body)
107107
}
108108

109-
static var ok: Self { .init(status: .ok) }
110-
static var notFound: Self { .init(status: .notFound) }
111109
static var badRequest: Self { .init(status: .badRequest) }
110+
static var notFound: Self { .init(status: .notFound) }
111+
static var tooManyRequests: Self { .init(status: .tooManyRequests) }
112+
static var ok: Self { .init(status: .ok) }
112113
}
113114
#endif

Sources/App/Core/Github.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,6 @@ extension Github {
289289
}
290290
}
291291

292-
@available(*, deprecated)
293-
static func fetchMetadata(client: Client, owner: String, repository: String) async throws(Github.Error) -> Metadata {
294-
struct Response<T: Decodable & Equatable>: Decodable, Equatable {
295-
var data: T
296-
}
297-
return try await fetchResource(Response<Metadata>.self,
298-
client: client,
299-
query: Metadata.query(owner: owner, repository: repository))
300-
.data
301-
}
302-
303292
static func fetchMetadata(owner: String, repository: String) async throws(Github.Error) -> Metadata {
304293
struct Response<T: Decodable & Equatable>: Decodable, Equatable {
305294
var data: T
@@ -309,11 +298,6 @@ extension Github {
309298
.data
310299
}
311300

312-
static func fetchMetadata(client: Client, packageUrl: String) async throws -> Metadata {
313-
let (owner, name) = try parseOwnerName(url: packageUrl)
314-
return try await fetchMetadata(client: client, owner: owner, repository: name)
315-
}
316-
317301
}
318302

319303

Tests/AppTests/GithubTests.swift

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -199,65 +199,48 @@ class GithubTests: AppTestCase {
199199
}
200200
}
201201

202-
func test_fetchMetadata_badUrl() async throws {
203-
let pkg = Package(url: "https://foo/bar")
204-
let client = MockClient { _, resp in
205-
resp.status = .ok
206-
}
207-
do {
208-
_ = try await Github.fetchMetadata(client: client, packageUrl: pkg.url)
209-
XCTFail("expected error to be thrown")
210-
} catch {
211-
guard case Github.Error.invalidURL = error else {
212-
XCTFail("unexpected error: \(error.localizedDescription)")
213-
return
214-
}
215-
}
216-
}
217-
218202
func test_fetchMetadata_badData() async throws {
219203
// setup
220204
Current.githubToken = { "secr3t" }
221-
let pkg = Package(url: "https://github.com/foo/bar")
222-
let client = MockClient { _, resp in
223-
resp.status = .ok
224-
resp.body = makeBody("bad data")
225-
}
226205

227-
// MUT
228-
do {
229-
_ = try await Github.fetchMetadata(client: client, packageUrl: pkg.url)
230-
XCTFail("expected error to be thrown")
231-
} catch let Github.Error.decodeContentFailed(uri, error) {
232-
// validation
233-
XCTAssertEqual(uri, "https://api.github.com/graphql")
234-
guard case DecodingError.dataCorrupted = error else {
235-
XCTFail("unexpected error: \(error.localizedDescription)")
236-
return
206+
await withDependencies {
207+
$0.httpClient.post = { @Sendable _, _, _ in .ok(body: "bad data") }
208+
} operation: {
209+
// MUT
210+
do {
211+
_ = try await Github.fetchMetadata(owner: "foo", repository: "bar")
212+
XCTFail("expected error to be thrown")
213+
} catch let Github.Error.decodeContentFailed(uri, error) {
214+
// validation
215+
XCTAssertEqual(uri, "https://api.github.com/graphql")
216+
guard case DecodingError.dataCorrupted = error else {
217+
XCTFail("unexpected error: \(error.localizedDescription)")
218+
return
219+
}
220+
} catch {
221+
XCTFail("Unexpected error: \(error)")
237222
}
238-
} catch {
239-
XCTFail("Unexpected error: \(error)")
240223
}
241224
}
242225

243226
func test_fetchMetadata_rateLimiting_429() async throws {
244227
// Github doesn't actually send a 429 when you hit the rate limit
245228
// setup
246229
Current.githubToken = { "secr3t" }
247-
let pkg = Package(url: "https://github.com/foo/bar")
248-
let client = MockClient { _, resp in
249-
resp.status = .tooManyRequests
250-
}
251230

252-
// MUT
253-
do {
254-
_ = try await Github.fetchMetadata(client: client, packageUrl: pkg.url)
255-
XCTFail("expected error to be thrown")
256-
} catch {
257-
// validation
258-
guard case Github.Error.requestFailed(.tooManyRequests) = error else {
259-
XCTFail("unexpected error: \(error.localizedDescription)")
260-
return
231+
await withDependencies {
232+
$0.httpClient.post = { @Sendable _, _, _ in .tooManyRequests }
233+
} operation: {
234+
// MUT
235+
do {
236+
_ = try await Github.fetchMetadata(owner: "foo", repository: "bar")
237+
XCTFail("expected error to be thrown")
238+
} catch {
239+
// validation
240+
guard case Github.Error.requestFailed(.tooManyRequests) = error else {
241+
XCTFail("unexpected error: \(error.localizedDescription)")
242+
return
243+
}
261244
}
262245
}
263246
}
@@ -297,26 +280,27 @@ class GithubTests: AppTestCase {
297280
// Ensure we record it as a rate limit error and raise a Rollbar item
298281
// setup
299282
Current.githubToken = { "secr3t" }
300-
let pkg = Package(url: "https://github.com/foo/bar")
301-
let client = MockClient { _, resp in
302-
resp.status = .forbidden
303-
resp.headers.add(name: "X-RateLimit-Remaining", value: "0")
304-
}
305283

306-
// MUT
307-
do {
308-
_ = try await Github.fetchMetadata(client: client, packageUrl: pkg.url)
309-
XCTFail("expected error to be thrown")
310-
} catch {
311-
// validation
312-
logger.logs.withValue { logs in
313-
XCTAssertEqual(logs, [
314-
.init(level: .critical, message: "rate limited while fetching resource Response<Metadata>")
315-
])
284+
await withDependencies {
285+
$0.httpClient.post = { @Sendable _, _, _ in
286+
.init(status: .forbidden, headers: ["X-RateLimit-Remaining": "0"])
316287
}
317-
guard case Github.Error.requestFailed(.tooManyRequests) = error else {
318-
XCTFail("unexpected error: \(error.localizedDescription)")
319-
return
288+
} operation: {
289+
// MUT
290+
do {
291+
_ = try await Github.fetchMetadata(owner: "foo", repository: "bar")
292+
XCTFail("expected error to be thrown")
293+
} catch {
294+
// validation
295+
logger.logs.withValue { logs in
296+
XCTAssertEqual(logs, [
297+
.init(level: .critical, message: "rate limited while fetching resource Response<Metadata>")
298+
])
299+
}
300+
guard case Github.Error.requestFailed(.tooManyRequests) = error else {
301+
XCTFail("unexpected error: \(error.localizedDescription)")
302+
return
303+
}
320304
}
321305
}
322306
}

0 commit comments

Comments
 (0)