Skip to content

Commit

Permalink
Alternative refactor of api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dadlerj committed Jul 28, 2023
1 parent 12edc7a commit 5ca7f44
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/shared/src/sourcegraph-api/graphql/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ interface event {
hashedLicenseKey?: string
}

private enum apiDestination {

Check failure on line 171 in lib/shared/src/sourcegraph-api/graphql/client.ts

View workflow job for this annotation

GitHub Actions / build

'private' modifier cannot appear on a module or namespace element.
DOTCOM,
LOCAL,
}

export class SourcegraphGraphQLAPIClient {
private dotcomUrl = 'https://sourcegraph.com'

Expand Down Expand Up @@ -335,11 +340,11 @@ export class SourcegraphGraphQLAPIClient {
return {}
}
if (this.config.serverEndpoint === this.dotcomUrl) {
return this.sendEventLogRequestToDotComAPI(event)
return this.sendEventLogRequestToAPI(apiDestination.DOTCOM, event)
}
return Promise.all([
this.sendEventLogRequestToAPI(event),
this.sendEventLogRequestToDotComAPI(event),
this.sendEventLogRequestToAPI(apiDestination.LOCAL, event),
this.sendEventLogRequestToAPI(apiDestination.DOTCOM, event),
]).then(responses => {
if (isError(responses[0]) && isError(responses[1])) {
return new Error('Errors logging events: ' + responses[0].toString() + ', ' + responses[1].toString())
Expand All @@ -354,25 +359,24 @@ export class SourcegraphGraphQLAPIClient {
})
}

private async sendEventLogRequestToDotComAPI(event: event): Promise<LogEventResponse | Error> {
return this.fetchSourcegraphDotcomAPI<APIResponse<LogEventResponse>>(LOG_EVENT_MUTATION, event).then(
response => extractDataOrError(response, data => data)
)
private async sendEventLogRequestToAPI(destination: apiDestination, event: event): Promise<LogEventResponse | Error> {
// retryOnFail is set to TRUE if this is sent to a local database, since we can't guarantee a matching version in a self-hosted Sourcegraph deployment.
return this.sendEventLogRequestToAPIWithRetry(destination, event, LOG_EVENT_MUTATION, destination == apiDestination.LOCAL)
}

private async sendEventLogRequestToAPI(event: event): Promise<LogEventResponse | Error> {
const initialAttempt = await this.fetchSourcegraphAPI<APIResponse<LogEventResponse>>(
LOG_EVENT_MUTATION,
private async sendEventLogRequestToAPIWithRetry(destination: apiDestination, event: event, mutation: string, retryOnFail: boolean): Promise<LogEventResponse | Error> {
const api = destination == apiDestination.DOTCOM ? this.fetchSourcegraphDotcomAPI : this.fetchSourcegraphAPI

const response = await api<APIResponse<LogEventResponse>>(
mutation,
event
).then(response => extractDataOrError(response, data => data))

if (isError(initialAttempt)) {
return this.fetchSourcegraphAPI<APIResponse<LogEventResponse>>(LOG_EVENT_MUTATION_DEPRECATED, event).then(
response => extractDataOrError(response, data => data)
)
if (retryOnFail && isError(response)) {
return this.sendEventLogRequestToAPIWithRetry(destination, event, LOG_EVENT_MUTATION_DEPRECATED, false)
}

return initialAttempt
return response
}

public async getCodyContext(
Expand Down

0 comments on commit 5ca7f44

Please sign in to comment.