@@ -78,21 +78,78 @@ extension GitHub.Client<GitHub.PersonalAccessToken>.Connection
78
78
}
79
79
}
80
80
}
81
- extension GitHub . Client < GitHub . OAuth > . Connection
81
+ extension GitHub . Client . Connection
82
82
{
83
- /// Run a REST API request.
83
+ @ discardableResult
84
84
@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
87
87
{
88
- try await self . get ( from: endpoint, with: . basic ( self . app ) )
88
+ try await self . post ( expecting : Never . self , from: endpoint, with: authorization ) . 0
89
89
}
90
- }
91
- extension GitHub . Client . Connection
92
- {
93
- /// Run a REST API request with the given credentials.
90
+
94
91
@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,
96
153
from endpoint: String ,
97
154
with authorization: GitHub . ClientAuthorization ) async throws -> Response
98
155
where Response: JSONDecodable
@@ -151,3 +208,13 @@ extension GitHub.Client.Connection
151
208
throw HTTP . StatusError. init ( code: status)
152
209
}
153
210
}
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