@@ -67,7 +67,7 @@ func runGzipWithRetry(contentType, url string, buf io.Reader, gzReq, gzResp bool
67
67
return nil , err
68
68
}
69
69
req .Header .Add ("Content-Type" , contentType )
70
- req .Header .Set ("X-Dgraph-AccessToken" , token .AccessJwt )
70
+ req .Header .Set ("X-Dgraph-AccessToken" , token .getAccessJWTToken () )
71
71
72
72
if gzReq {
73
73
req .Header .Set ("Content-Encoding" , "gzip" )
@@ -79,15 +79,10 @@ func runGzipWithRetry(contentType, url string, buf io.Reader, gzReq, gzResp bool
79
79
80
80
resp , err = client .Do (req )
81
81
if err != nil && strings .Contains (err .Error (), "Token is expired" ) {
82
- newToken , err := testutil .HttpLogin (& testutil.LoginParams {
83
- Endpoint : addr + "/admin" ,
84
- RefreshJwt : token .RefreshToken ,
85
- })
82
+ err := token .refreshToken ()
86
83
if err != nil {
87
84
return nil , err
88
85
}
89
- token .AccessJwt = newToken .AccessJwt
90
- token .RefreshToken = newToken .RefreshToken
91
86
continue
92
87
} else if err != nil {
93
88
return nil , err
@@ -168,33 +163,8 @@ func queryWithGz(queryText, contentType, debug, timeout string, gzReq, gzResp bo
168
163
}
169
164
170
165
func queryWithTs (queryText , contentType , debug string , ts uint64 ) (string , uint64 , error ) {
171
- params := make ([]string , 0 , 2 )
172
- if debug != "" {
173
- params = append (params , "debug=" + debug )
174
- }
175
- if ts != 0 {
176
- params = append (params , fmt .Sprintf ("startTs=%v" , strconv .FormatUint (ts , 10 )))
177
- }
178
- url := addr + "/query?" + strings .Join (params , "&" )
179
-
180
- _ , body , err := runWithRetries ("POST" , contentType , url , queryText )
181
- if err != nil {
182
- return "" , 0 , err
183
- }
184
-
185
- var r res
186
- if err := json .Unmarshal (body , & r ); err != nil {
187
- return "" , 0 , err
188
- }
189
- startTs := r .Extensions .Txn .StartTs
190
-
191
- // Remove the extensions.
192
- r2 := res {
193
- Data : r .Data ,
194
- }
195
- output , err := json .Marshal (r2 )
196
-
197
- return string (output ), startTs , err
166
+ out , startTs , _ , err := queryWithTsForResp (queryText , contentType , debug , ts )
167
+ return out , startTs , err
198
168
}
199
169
200
170
// queryWithTsForResp query the dgraph and returns it's http response and result.
@@ -296,57 +266,34 @@ func createRequest(method, contentType, url string, body string) (*http.Request,
296
266
297
267
func runWithRetries (method , contentType , url string , body string ) (
298
268
* x.QueryResWithData , []byte , error ) {
299
-
300
- req , err := createRequest (method , contentType , url , body )
301
- if err != nil {
302
- return nil , nil , err
303
- }
304
-
305
- qr , respBody , err := runRequest (req )
306
- if err != nil && strings .Contains (err .Error (), "Token is expired" ) {
307
- token , err = testutil .HttpLogin (& testutil.LoginParams {
308
- Endpoint : addr + "/admin" ,
309
- RefreshJwt : token .RefreshToken ,
310
- })
311
- if err != nil {
312
- return nil , nil , err
313
- }
314
-
315
- // create a new request since the previous request would have been closed upon the err
316
- retryReq , err := createRequest (method , contentType , url , body )
317
- if err != nil {
318
- return nil , nil , err
319
- }
320
-
321
- return runRequest (retryReq )
322
- }
269
+ qr , respBody , _ , err := runWithRetriesForResp (method , contentType , url , body )
323
270
return qr , respBody , err
324
271
}
325
272
326
273
// attach the grootAccessJWT to the request and sends the http request
327
- func runRequest (req * http.Request ) (* x.QueryResWithData , []byte , error ) {
274
+ func runRequest (req * http.Request ) (* x.QueryResWithData , []byte , * http. Response , error ) {
328
275
client := & http.Client {}
329
- req .Header .Set ("X-Dgraph-AccessToken" , token .AccessJwt )
276
+ req .Header .Set ("X-Dgraph-AccessToken" , token .getAccessJWTToken () )
330
277
resp , err := client .Do (req )
331
278
if err != nil {
332
- return nil , nil , err
279
+ return nil , nil , resp , err
333
280
}
334
281
if status := resp .StatusCode ; status != http .StatusOK {
335
- return nil , nil , errors .Errorf ("Unexpected status code: %v" , status )
282
+ return nil , nil , resp , errors .Errorf ("Unexpected status code: %v" , status )
336
283
}
337
284
338
285
defer resp .Body .Close ()
339
286
body , err := ioutil .ReadAll (resp .Body )
340
287
if err != nil {
341
- return nil , nil , errors .Errorf ("unable to read from body: %v" , err )
288
+ return nil , nil , resp , errors .Errorf ("unable to read from body: %v" , err )
342
289
}
343
290
344
291
qr := new (x.QueryResWithData )
345
292
json .Unmarshal (body , qr ) // Don't check error.
346
293
if len (qr .Errors ) > 0 {
347
- return nil , nil , errors .New (qr .Errors [0 ].Message )
294
+ return nil , nil , resp , errors .New (qr .Errors [0 ].Message )
348
295
}
349
- return qr , body , nil
296
+ return qr , body , resp , nil
350
297
}
351
298
352
299
func runWithRetriesForResp (method , contentType , url string , body string ) (
@@ -357,12 +304,9 @@ func runWithRetriesForResp(method, contentType, url string, body string) (
357
304
return nil , nil , nil , err
358
305
}
359
306
360
- qr , respBody , resp , err := runRequestForResp (req )
307
+ qr , respBody , resp , err := runRequest (req )
361
308
if err != nil && strings .Contains (err .Error (), "Token is expired" ) {
362
- token , err = testutil .HttpLogin (& testutil.LoginParams {
363
- Endpoint : addr + "/admin" ,
364
- RefreshJwt : token .RefreshToken ,
365
- })
309
+ err = token .refreshToken ()
366
310
if err != nil {
367
311
return nil , nil , nil , err
368
312
}
@@ -373,37 +317,11 @@ func runWithRetriesForResp(method, contentType, url string, body string) (
373
317
return nil , nil , resp , err
374
318
}
375
319
376
- return runRequestForResp (retryReq )
320
+ return runRequest (retryReq )
377
321
}
378
322
return qr , respBody , resp , err
379
323
}
380
324
381
- // attach the grootAccessJWT to the request and sends the http request
382
- func runRequestForResp (req * http.Request ) (* x.QueryResWithData , []byte , * http.Response , error ) {
383
- client := & http.Client {}
384
- req .Header .Set ("X-Dgraph-AccessToken" , token .AccessJwt )
385
- resp , err := client .Do (req )
386
- if err != nil {
387
- return nil , nil , resp , err
388
- }
389
- if status := resp .StatusCode ; status != http .StatusOK {
390
- return nil , nil , resp , errors .Errorf ("Unexpected status code: %v" , status )
391
- }
392
-
393
- defer resp .Body .Close ()
394
- body , err := ioutil .ReadAll (resp .Body )
395
- if err != nil {
396
- return nil , nil , resp , errors .Errorf ("unable to read from body: %v" , err )
397
- }
398
-
399
- qr := new (x.QueryResWithData )
400
- json .Unmarshal (body , qr ) // Don't check error.
401
- if len (qr .Errors ) > 0 {
402
- return nil , nil , resp , errors .New (qr .Errors [0 ].Message )
403
- }
404
- return qr , body , resp , nil
405
- }
406
-
407
325
func commitWithTs (keys , preds []string , ts uint64 ) error {
408
326
url := addr + "/commit"
409
327
if ts != 0 {
@@ -421,7 +339,7 @@ func commitWithTs(keys, preds []string, ts uint64) error {
421
339
if err != nil {
422
340
return err
423
341
}
424
- _ , _ , err = runRequest (req )
342
+ _ , _ , _ , err = runRequest (req )
425
343
return err
426
344
}
427
345
@@ -439,7 +357,7 @@ func commitWithTsKeysOnly(keys []string, ts uint64) error {
439
357
if err != nil {
440
358
return err
441
359
}
442
- _ , _ , err = runRequest (req )
360
+ _ , _ , _ , err = runRequest (req )
443
361
return err
444
362
}
445
363
@@ -633,7 +551,7 @@ func TestTransactionBasicOldCommitFormat(t *testing.T) {
633
551
url := fmt .Sprintf ("%s/commit?startTs=%d&abort=true" , addr , ts )
634
552
req , err := http .NewRequest ("POST" , url , nil )
635
553
require .NoError (t , err )
636
- _ , _ , err = runRequest (req )
554
+ _ , _ , _ , err = runRequest (req )
637
555
require .NoError (t , err )
638
556
}
639
557
0 commit comments