@@ -8,10 +8,11 @@ import { getLogger, Logger } from '../logger'
8
8
import { ResourceFetcher } from './resourcefetcher'
9
9
import { Timeout , CancelEvent } from '../utilities/timeoutUtils'
10
10
import request , { RequestError } from '../request'
11
+ import { withRetries } from '../utilities/functionUtils'
11
12
12
13
type RequestHeaders = { eTag ?: string ; gZip ?: boolean }
13
14
14
- export class HttpResourceFetcher implements ResourceFetcher {
15
+ export class HttpResourceFetcher implements ResourceFetcher < Response > {
15
16
private readonly logger : Logger = getLogger ( )
16
17
17
18
/**
@@ -20,27 +21,27 @@ export class HttpResourceFetcher implements ResourceFetcher {
20
21
* @param params Additional params for the fetcher
21
22
* @param {boolean } params.showUrl Whether or not to the URL in log statements.
22
23
* @param {string } params.friendlyName If URL is not shown, replaces the URL with this text.
23
- * @param {function } params.onSuccess Function to execute on successful request. No effect if piping to a location.
24
24
* @param {Timeout } params.timeout Timeout token to abort/cancel the request. Similar to `AbortSignal`.
25
+ * @param {number } params.retries The number of retries a get request should make if one fails
25
26
*/
26
27
public constructor (
27
28
private readonly url : string ,
28
29
private readonly params : {
29
30
showUrl : boolean
30
31
friendlyName ?: string
31
- onSuccess ?( contents : string ) : void
32
32
timeout ?: Timeout
33
+ retries ?: number
33
34
}
34
35
) { }
35
36
36
37
/**
37
- * Returns the contents of the resource, or undefined if the resource could not be retrieved.
38
- *
39
- * @param pipeLocation Optionally pipe the download to a file system location
38
+ * Returns the response of the resource, or undefined if the response failed could not be retrieved.
40
39
*/
41
- public get ( ) : Promise < string | undefined > {
40
+ public get ( ) : Promise < Response | undefined > {
42
41
this . logger . verbose ( `downloading: ${ this . logText ( ) } ` )
43
- return this . downloadRequest ( )
42
+ return withRetries ( ( ) => this . downloadRequest ( ) , {
43
+ maxRetries : this . params . retries ?? 1 ,
44
+ } )
44
45
}
45
46
46
47
/**
@@ -69,26 +70,16 @@ export class HttpResourceFetcher implements ResourceFetcher {
69
70
this . logger . verbose ( `E-Tag, ${ eTagResponse } , matched. No content downloaded from: ${ this . url } ` )
70
71
} else {
71
72
this . logger . verbose ( `No E-Tag match. Downloaded content from: ${ this . logText ( ) } ` )
72
- if ( this . params . onSuccess ) {
73
- this . params . onSuccess ( contents )
74
- }
75
73
}
76
74
77
75
return { content : contents , eTag : eTagResponse }
78
76
}
79
77
80
- private async downloadRequest ( ) : Promise < string | undefined > {
78
+ private async downloadRequest ( ) : Promise < Response | undefined > {
81
79
try {
82
- // HACK(?): receiving JSON as a string without `toString` makes it so we can't deserialize later
83
80
const resp = await this . getResponseFromGetRequest ( this . params . timeout )
84
- const contents = ( await resp . text ( ) ) . toString ( )
85
- if ( this . params . onSuccess ) {
86
- this . params . onSuccess ( contents )
87
- }
88
-
89
81
this . logger . verbose ( `downloaded: ${ this . logText ( ) } ` )
90
-
91
- return contents
82
+ return resp
92
83
} catch ( err ) {
93
84
const error = err as RequestError
94
85
this . logger . verbose (
@@ -150,7 +141,8 @@ export async function getPropertyFromJsonUrl(
150
141
fetcher ?: HttpResourceFetcher
151
142
) : Promise < any | undefined > {
152
143
const resourceFetcher = fetcher ?? new HttpResourceFetcher ( url , { showUrl : true } )
153
- const result = await resourceFetcher . get ( )
144
+ const resp = await resourceFetcher . get ( )
145
+ const result = await resp ?. text ( )
154
146
if ( result ) {
155
147
try {
156
148
const json = JSON . parse ( result )
0 commit comments