@@ -8,10 +8,11 @@ import { getLogger, Logger } from '../logger'
88import { ResourceFetcher } from './resourcefetcher'
99import { Timeout , CancelEvent } from '../utilities/timeoutUtils'
1010import request , { RequestError } from '../request'
11+ import { withRetries } from '../utilities/functionUtils'
1112
1213type RequestHeaders = { eTag ?: string ; gZip ?: boolean }
1314
14- export class HttpResourceFetcher implements ResourceFetcher {
15+ export class HttpResourceFetcher implements ResourceFetcher < Response > {
1516 private readonly logger : Logger = getLogger ( )
1617
1718 /**
@@ -20,27 +21,27 @@ export class HttpResourceFetcher implements ResourceFetcher {
2021 * @param params Additional params for the fetcher
2122 * @param {boolean } params.showUrl Whether or not to the URL in log statements.
2223 * @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.
2424 * @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
2526 */
2627 public constructor (
2728 private readonly url : string ,
2829 private readonly params : {
2930 showUrl : boolean
3031 friendlyName ?: string
31- onSuccess ?( contents : string ) : void
3232 timeout ?: Timeout
33+ retries ?: number
3334 }
3435 ) { }
3536
3637 /**
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.
4039 */
41- public get ( ) : Promise < string | undefined > {
40+ public get ( ) : Promise < Response | undefined > {
4241 this . logger . verbose ( `downloading: ${ this . logText ( ) } ` )
43- return this . downloadRequest ( )
42+ return withRetries ( ( ) => this . downloadRequest ( ) , {
43+ maxRetries : this . params . retries ?? 1 ,
44+ } )
4445 }
4546
4647 /**
@@ -69,26 +70,16 @@ export class HttpResourceFetcher implements ResourceFetcher {
6970 this . logger . verbose ( `E-Tag, ${ eTagResponse } , matched. No content downloaded from: ${ this . url } ` )
7071 } else {
7172 this . logger . verbose ( `No E-Tag match. Downloaded content from: ${ this . logText ( ) } ` )
72- if ( this . params . onSuccess ) {
73- this . params . onSuccess ( contents )
74- }
7573 }
7674
7775 return { content : contents , eTag : eTagResponse }
7876 }
7977
80- private async downloadRequest ( ) : Promise < string | undefined > {
78+ private async downloadRequest ( ) : Promise < Response | undefined > {
8179 try {
82- // HACK(?): receiving JSON as a string without `toString` makes it so we can't deserialize later
8380 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-
8981 this . logger . verbose ( `downloaded: ${ this . logText ( ) } ` )
90-
91- return contents
82+ return resp
9283 } catch ( err ) {
9384 const error = err as RequestError
9485 this . logger . verbose (
@@ -150,7 +141,8 @@ export async function getPropertyFromJsonUrl(
150141 fetcher ?: HttpResourceFetcher
151142) : Promise < any | undefined > {
152143 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 ( )
154146 if ( result ) {
155147 try {
156148 const json = JSON . parse ( result )
0 commit comments