Skip to content

Commit 5d4badd

Browse files
committed
Unified and moved request sending code to GitHubAPI.
1 parent 183c039 commit 5d4badd

File tree

4 files changed

+39
-57
lines changed

4 files changed

+39
-57
lines changed

CCMenu/Source/Pipeline Window/GitHub Sheets/GitHubBranchList.swift

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,13 @@ class GitHubBranchList: ObservableObject {
2727
}
2828

2929
private func fetchBranches(request: URLRequest) async -> [GitHubBranch] {
30-
do {
31-
let (data, response) = try await URLSession.feedSession.data(for: request)
32-
guard let response = response as? HTTPURLResponse else { throw URLError(.unsupportedURL) }
33-
if response.statusCode != 200 {
34-
return [GitHubBranch(message: HTTPURLResponse.localizedString(forStatusCode: response.statusCode))]
35-
}
36-
return try JSONDecoder().decode([GitHubBranch].self, from: data)
37-
} catch {
38-
return [GitHubBranch(message: error.localizedDescription)]
30+
let (repos, message): ([GitHubBranch]?, String) = await GitHubAPI.sendRequest(request: request)
31+
guard let repos else {
32+
return [GitHubBranch(message: message)]
3933
}
34+
return repos
4035
}
41-
36+
4237
func clearBranches() {
4338
items = [GitHubBranch()]
4439
}

CCMenu/Source/Pipeline Window/GitHub Sheets/GitHubRepositoryList.swift

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,47 +53,15 @@ class GitHubRepositoryList: ObservableObject {
5353
}
5454

5555
private func fetchUser(request: URLRequest) async -> (GitHubUser?, String) {
56-
do {
57-
let (data, response) = try await URLSession.feedSession.data(for: request)
58-
guard let response = response as? HTTPURLResponse else { throw URLError(.unsupportedURL) }
59-
// TODO: Somehow refactor this to use the same code as fetchRepositories
60-
if response.statusCode == 403 || response.statusCode == 429 {
61-
if let v = response.value(forHTTPHeaderField: "x-ratelimit-remaining"), Int(v) == 0 {
62-
// HTTPURLResponse doesn't have a specific message for code 429
63-
return (nil, "too many requests")
64-
} else {
65-
return (nil, HTTPURLResponse.localizedString(forStatusCode: response.statusCode))
66-
}
67-
}
68-
if response.statusCode != 200 {
69-
return (nil, HTTPURLResponse.localizedString(forStatusCode: response.statusCode))
70-
}
71-
return (try JSONDecoder().decode(GitHubUser.self, from: data), "OK")
72-
} catch {
73-
return (nil, error.localizedDescription)
74-
}
56+
return await GitHubAPI.sendRequest(request: request)
7557
}
7658

7759
private func fetchRepositories(request: URLRequest) async -> [GitHubRepository] {
78-
do {
79-
let (data, response) = try await URLSession.feedSession.data(for: request)
80-
guard let response = response as? HTTPURLResponse else { throw URLError(.unsupportedURL) }
81-
// TODO: Somehow refactor this to use the same code as feed reader
82-
if response.statusCode == 403 || response.statusCode == 429 {
83-
if let v = response.value(forHTTPHeaderField: "x-ratelimit-remaining"), Int(v) == 0 {
84-
// HTTPURLResponse doesn't have a specific message for code 429
85-
return [GitHubRepository(message: "too many requests")]
86-
} else {
87-
return [GitHubRepository(message: HTTPURLResponse.localizedString(forStatusCode: response.statusCode))]
88-
}
89-
}
90-
if response.statusCode != 200 {
91-
return [GitHubRepository(message: HTTPURLResponse.localizedString(forStatusCode: response.statusCode))]
92-
}
93-
return try JSONDecoder().decode([GitHubRepository].self, from: data)
94-
} catch {
95-
return [GitHubRepository(message: error.localizedDescription)]
60+
let (repos, message): ([GitHubRepository]?, String) = await GitHubAPI.sendRequest(request: request)
61+
guard let repos else {
62+
return [GitHubRepository(message: message)]
9663
}
64+
return repos
9765
}
9866

9967
func clearRepositories() {

CCMenu/Source/Pipeline Window/GitHub Sheets/GitHubWorkflowList.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,11 @@ class GitHubWorkflowList: ObservableObject {
3030
}
3131

3232
private func fetchWorkflows(request: URLRequest) async -> [GitHubWorkflow] {
33-
do {
34-
let (data, response) = try await URLSession.feedSession.data(for: request)
35-
guard let response = response as? HTTPURLResponse else { throw URLError(.unsupportedURL) }
36-
if response.statusCode != 200 {
37-
return [GitHubWorkflow(message: HTTPURLResponse.localizedString(forStatusCode: response.statusCode))]
38-
}
39-
let workflowResponse = try JSONDecoder().decode(WorflowResponse.self, from: data)
40-
return workflowResponse.workflows
41-
} catch {
42-
return [GitHubWorkflow(message: error.localizedDescription)]
33+
let (workflowResponse, message): (WorflowResponse?, String) = await GitHubAPI.sendRequest(request: request)
34+
guard let workflowResponse else {
35+
return [GitHubWorkflow(message: message)]
4336
}
37+
return workflowResponse.workflows
4438
}
4539

4640
struct WorflowResponse: Decodable {

CCMenu/Source/Server Monitor/GitHubAPI.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,31 @@ class GitHubAPI {
117117
}
118118

119119

120+
// MARK: - send requests
121+
122+
static func sendRequest<T>(request: URLRequest) async -> (T?, String) where T: Decodable {
123+
do {
124+
let (data, response) = try await URLSession.feedSession.data(for: request)
125+
guard let response = response as? HTTPURLResponse else { throw URLError(.unsupportedURL) }
126+
if response.statusCode == 403 || response.statusCode == 429 {
127+
if let v = response.value(forHTTPHeaderField: "x-ratelimit-remaining"), Int(v) == 0 {
128+
// HTTPURLResponse doesn't have a specific message for code 429
129+
return (nil, "too many requests")
130+
} else {
131+
return (nil, HTTPURLResponse.localizedString(forStatusCode: response.statusCode))
132+
}
133+
}
134+
if response.statusCode != 200 {
135+
return (nil, HTTPURLResponse.localizedString(forStatusCode: response.statusCode))
136+
}
137+
return (try JSONDecoder().decode(T.self, from: data), "OK")
138+
} catch {
139+
return (nil, error.localizedDescription)
140+
}
141+
}
142+
143+
144+
120145
// MARK: - helper functions
121146

122147
private static func baseURL(forAPI: Bool) -> String {

0 commit comments

Comments
 (0)