@@ -118,6 +118,8 @@ public class GitHubClient {
118
118
private static final int FORBIDDEN = 403 ;
119
119
120
120
private final URI baseUrl ;
121
+
122
+ private final Optional <URI > graphqlUrl ;
121
123
private final Json json = Json .create ();
122
124
private final OkHttpClient client ;
123
125
private final String token ;
@@ -131,11 +133,13 @@ public class GitHubClient {
131
133
private GitHubClient (
132
134
final OkHttpClient client ,
133
135
final URI baseUrl ,
136
+ final URI graphqlUrl ,
134
137
final String accessToken ,
135
138
final byte [] privateKey ,
136
139
final Integer appId ,
137
140
final Integer installationId ) {
138
141
this .baseUrl = baseUrl ;
142
+ this .graphqlUrl = Optional .ofNullable (graphqlUrl );
139
143
this .token = accessToken ;
140
144
this .client = client ;
141
145
this .privateKey = privateKey ;
@@ -152,7 +156,11 @@ private GitHubClient(
152
156
* @return github api client
153
157
*/
154
158
public static GitHubClient create (final URI baseUrl , final String token ) {
155
- return new GitHubClient (new OkHttpClient (), baseUrl , token , null , null , null );
159
+ return new GitHubClient (new OkHttpClient (), baseUrl , null , token , null , null , null );
160
+ }
161
+
162
+ public static GitHubClient create (final URI baseUrl , final URI graphqlUri , final String token ) {
163
+ return new GitHubClient (new OkHttpClient (), baseUrl , graphqlUri , token , null , null , null );
156
164
}
157
165
158
166
/**
@@ -164,7 +172,7 @@ public static GitHubClient create(final URI baseUrl, final String token) {
164
172
* @return github api client
165
173
*/
166
174
public static GitHubClient create (final URI baseUrl , final File privateKey , final Integer appId ) {
167
- return createOrThrow (new OkHttpClient (), baseUrl , privateKey , appId , null );
175
+ return createOrThrow (new OkHttpClient (), baseUrl , null , privateKey , appId , null );
168
176
}
169
177
170
178
/**
@@ -176,7 +184,7 @@ public static GitHubClient create(final URI baseUrl, final File privateKey, fina
176
184
* @return github api client
177
185
*/
178
186
public static GitHubClient create (final URI baseUrl , final byte [] privateKey , final Integer appId ) {
179
- return new GitHubClient (new OkHttpClient (), baseUrl , null , privateKey , appId , null );
187
+ return new GitHubClient (new OkHttpClient (), baseUrl , null , null , privateKey , appId , null );
180
188
}
181
189
182
190
/**
@@ -190,7 +198,7 @@ public static GitHubClient create(final URI baseUrl, final byte[] privateKey, fi
190
198
*/
191
199
public static GitHubClient create (
192
200
final URI baseUrl , final File privateKey , final Integer appId , final Integer installationId ) {
193
- return createOrThrow (new OkHttpClient (), baseUrl , privateKey , appId , installationId );
201
+ return createOrThrow (new OkHttpClient (), baseUrl , null , privateKey , appId , installationId );
194
202
}
195
203
196
204
/**
@@ -204,7 +212,7 @@ public static GitHubClient create(
204
212
*/
205
213
public static GitHubClient create (
206
214
final URI baseUrl , final byte [] privateKey , final Integer appId , final Integer installationId ) {
207
- return new GitHubClient (new OkHttpClient (), baseUrl , null , privateKey , appId , installationId );
215
+ return new GitHubClient (new OkHttpClient (), baseUrl , null , null , privateKey , appId , installationId );
208
216
}
209
217
210
218
/**
@@ -221,7 +229,25 @@ public static GitHubClient create(
221
229
final URI baseUrl ,
222
230
final File privateKey ,
223
231
final Integer appId ) {
224
- return createOrThrow (httpClient , baseUrl , privateKey , appId , null );
232
+ return createOrThrow (httpClient , baseUrl , null , privateKey , appId , null );
233
+ }
234
+
235
+ /**
236
+ * Create a github api client with a given base URL and a path to a key.
237
+ *
238
+ * @param httpClient an instance of OkHttpClient
239
+ * @param baseUrl base URL
240
+ * @param privateKey the private key PEM file
241
+ * @param appId the github app ID
242
+ * @return github api client
243
+ */
244
+ public static GitHubClient create (
245
+ final OkHttpClient httpClient ,
246
+ final URI baseUrl ,
247
+ final URI graphqlUrl ,
248
+ final File privateKey ,
249
+ final Integer appId ) {
250
+ return createOrThrow (httpClient , baseUrl , graphqlUrl , privateKey , appId , null );
225
251
}
226
252
227
253
/**
@@ -238,9 +264,11 @@ public static GitHubClient create(
238
264
final URI baseUrl ,
239
265
final byte [] privateKey ,
240
266
final Integer appId ) {
241
- return new GitHubClient (httpClient , baseUrl , null , privateKey , appId , null );
267
+ return new GitHubClient (httpClient , baseUrl , null , null , privateKey , appId , null );
242
268
}
243
269
270
+
271
+
244
272
/**
245
273
* Create a github api client with a given base URL and a path to a key.
246
274
*
@@ -256,7 +284,7 @@ public static GitHubClient create(
256
284
final File privateKey ,
257
285
final Integer appId ,
258
286
final Integer installationId ) {
259
- return createOrThrow (httpClient , baseUrl , privateKey , appId , installationId );
287
+ return createOrThrow (httpClient , baseUrl , null , privateKey , appId , installationId );
260
288
}
261
289
262
290
/**
@@ -274,7 +302,7 @@ public static GitHubClient create(
274
302
final byte [] privateKey ,
275
303
final Integer appId ,
276
304
final Integer installationId ) {
277
- return new GitHubClient (httpClient , baseUrl , null , privateKey , appId , installationId );
305
+ return new GitHubClient (httpClient , baseUrl , null , null , privateKey , appId , installationId );
278
306
}
279
307
280
308
/**
@@ -287,7 +315,12 @@ public static GitHubClient create(
287
315
*/
288
316
public static GitHubClient create (
289
317
final OkHttpClient httpClient , final URI baseUrl , final String token ) {
290
- return new GitHubClient (httpClient , baseUrl , token , null , null , null );
318
+ return new GitHubClient (httpClient , baseUrl , null , token , null , null , null );
319
+ }
320
+
321
+ public static GitHubClient create (
322
+ final OkHttpClient httpClient , final URI baseUrl , final URI graphqlUrl , final String token ) {
323
+ return new GitHubClient (httpClient , baseUrl , graphqlUrl , token , null , null , null );
291
324
}
292
325
293
326
/**
@@ -306,6 +339,7 @@ public static GitHubClient scopeForInstallationId(
306
339
client .client ,
307
340
client .baseUrl ,
308
341
null ,
342
+ null ,
309
343
client .getPrivateKey ().get (),
310
344
client .appId ,
311
345
installationId );
@@ -327,6 +361,7 @@ public GitHubClient withScopeForInstallationId(final int installationId) {
327
361
client ,
328
362
baseUrl ,
329
363
null ,
364
+ null ,
330
365
privateKey ,
331
366
appId ,
332
367
installationId );
@@ -565,6 +600,23 @@ <T> CompletableFuture<T> post(final String path, final String data, final Class<
565
600
response -> json ().fromJsonUncheckedNotNull (responseBodyUnchecked (response ), clazz ));
566
601
}
567
602
603
+ /**
604
+ * Make a POST request to the graphql endpoint of Github
605
+ *
606
+ * @param data request body as stringified JSON
607
+ * @return response
608
+ *
609
+ * @see "https://docs.github.com/en/[email protected] /graphql/guides/forming-calls-with-graphql#communicating-with-graphql"
610
+ */
611
+ public CompletableFuture <Response > postGraphql (final String data ) {
612
+ final Request request =
613
+ graphqlRequestBuilder ()
614
+ .method ("POST" , RequestBody .create (parse (MediaType .APPLICATION_JSON ), data ))
615
+ .build ();
616
+ log .info ("Making POST request to {}" , request .url ());
617
+ return call (request );
618
+ }
619
+
568
620
/**
569
621
* Make an http PUT request for the given path with provided JSON body.
570
622
*
@@ -698,6 +750,22 @@ private Request.Builder requestBuilder(final String path) {
698
750
return builder ;
699
751
}
700
752
753
+ private Request .Builder graphqlRequestBuilder () {
754
+ URI url = graphqlUrl .orElseThrow (() -> new IllegalStateException ("No graphql url set" ));
755
+ final Request .Builder builder =
756
+ new Request .Builder ()
757
+ .url (url .toString ())
758
+ .addHeader (HttpHeaders .ACCEPT , MediaType .APPLICATION_JSON )
759
+ .addHeader (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON );
760
+ builder .addHeader (HttpHeaders .AUTHORIZATION , getAuthorizationHeader ("/graphql" ));
761
+ return builder ;
762
+ }
763
+
764
+ public boolean isGraphqlEnabled () {
765
+ return graphqlUrl .isPresent ();
766
+ }
767
+
768
+
701
769
/*
702
770
Generates the Authentication header, given the API endpoint and the credentials provided.
703
771
@@ -870,9 +938,9 @@ CompletableFuture<Response> processPossibleRedirects(
870
938
/**
871
939
* Wrapper to Constructors that expose File object for the privateKey argument
872
940
* */
873
- private static GitHubClient createOrThrow (final OkHttpClient httpClient , final URI baseUrl , final File privateKey , final Integer appId , final Integer installationId ) {
941
+ private static GitHubClient createOrThrow (final OkHttpClient httpClient , final URI baseUrl , final URI graphqlUrl , final File privateKey , final Integer appId , final Integer installationId ) {
874
942
try {
875
- return new GitHubClient (httpClient , baseUrl , null , FileUtils .readFileToByteArray (privateKey ), appId , installationId );
943
+ return new GitHubClient (httpClient , baseUrl , graphqlUrl , null , FileUtils .readFileToByteArray (privateKey ), appId , installationId );
876
944
} catch (IOException e ) {
877
945
throw new RuntimeException ("There was an error generating JWT token" , e );
878
946
}
0 commit comments