Skip to content

Commit fab89c9

Browse files
committed
add POST methods to GitHub.Client.Connection, and start specifying the x-github-api-version
1 parent b26774a commit fab89c9

File tree

1 file changed

+77
-10
lines changed

1 file changed

+77
-10
lines changed

Sources/GitHubClient/GitHub.Client.Connection.swift

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,78 @@ extension GitHub.Client<GitHub.PersonalAccessToken>.Connection
7878
}
7979
}
8080
}
81-
extension GitHub.Client<GitHub.OAuth>.Connection
81+
extension GitHub.Client.Connection
8282
{
83-
/// Run a REST API request.
83+
@discardableResult
8484
@inlinable public
85-
func get<Response>(_:Response.Type = Response.self,
86-
from endpoint:String) async throws -> Response where Response:JSONDecodable
85+
func post(to endpoint:String,
86+
with authorization:GitHub.ClientAuthorization) async throws -> UInt
8787
{
88-
try await self.get(from: endpoint, with: .basic(self.app))
88+
try await self.post(expecting: Never.self, from: endpoint, with: authorization).0
8989
}
90-
}
91-
extension GitHub.Client.Connection
92-
{
93-
/// Run a REST API request with the given credentials.
90+
9491
@inlinable public
95-
func get<Response>(_:Response.Type = Response.self,
92+
func post<Response>(expecting _:Response.Type = Response.self,
93+
from endpoint:String,
94+
with authorization:GitHub.ClientAuthorization) async throws -> Response
95+
where Response:JSONDecodable
96+
{
97+
let (status, response):(UInt, Response?) = try await self.post(expecting: Response.self,
98+
from: endpoint,
99+
with: authorization)
100+
101+
if let response:Response
102+
{
103+
return response
104+
}
105+
else
106+
{
107+
throw HTTP.StatusError.init(code: status)
108+
}
109+
}
110+
111+
@inlinable
112+
func post<Response>(expecting _:Response.Type,
113+
from endpoint:String,
114+
with authorization:GitHub.ClientAuthorization) async throws -> (UInt, Response?)
115+
where Response:JSONDecodable
116+
{
117+
let response:HTTP.Client2.Facet = try await self.http2.fetch([
118+
":method": "POST",
119+
":scheme": "https",
120+
":authority": self.http2.remote,
121+
":path": endpoint,
122+
123+
"authorization": authorization.header,
124+
// GitHub will reject the API request if the user-agent is not set.
125+
"user-agent": self.agent,
126+
"accept": "application/vnd.github+json",
127+
"x-github-api-version": "2022-11-28",
128+
])
129+
130+
guard
131+
let status:UInt = response.status,
132+
case 200 ... 299 = status
133+
else
134+
{
135+
throw HTTP.StatusError.init(code: response.status,
136+
message: String.init(decoding: response.body, as: Unicode.UTF8.self))
137+
}
138+
139+
if response.body.isEmpty
140+
{
141+
return (status, nil)
142+
}
143+
else
144+
{
145+
let json:JSON = .init(utf8: response.body[...])
146+
return (status, try json.decode())
147+
}
148+
}
149+
150+
/// Run a REST API request with the given credentials, following up to one redirect.
151+
@inlinable public
152+
func get<Response>(expecting _:Response.Type = Response.self,
96153
from endpoint:String,
97154
with authorization:GitHub.ClientAuthorization) async throws -> Response
98155
where Response:JSONDecodable
@@ -151,3 +208,13 @@ extension GitHub.Client.Connection
151208
throw HTTP.StatusError.init(code: status)
152209
}
153210
}
211+
extension GitHub.Client<GitHub.OAuth>.Connection
212+
{
213+
/// Run a REST API request.
214+
@inlinable public
215+
func get<Response>(expecting _:Response.Type = Response.self,
216+
from endpoint:String) async throws -> Response where Response:JSONDecodable
217+
{
218+
try await self.get(from: endpoint, with: .basic(self.app))
219+
}
220+
}

0 commit comments

Comments
 (0)