Browse Source

Consolidate fetch urls and access

main
Eric Amodio 1 year ago
parent
commit
a6dc6662ef
4 changed files with 66 additions and 40 deletions
  1. +7
    -10
      src/plus/gk/authenticationConnection.ts
  2. +51
    -12
      src/plus/gk/serverConnection.ts
  3. +7
    -12
      src/plus/subscription/subscriptionService.ts
  4. +1
    -6
      src/plus/workspaces/workspacesApi.ts

+ 7
- 10
src/plus/gk/authenticationConnection.ts View File

@ -44,11 +44,7 @@ export class AuthenticationConnection implements Disposable {
let rsp: Response; let rsp: Response;
try { try {
rsp = await this.connection.fetch(
Uri.joinPath(this.connection.baseApiUri, 'user').toString(),
undefined,
token,
);
rsp = await this.connection.fetchApi('user', undefined, token);
} catch (ex) { } catch (ex) {
Logger.error(ex, scope); Logger.error(ex, scope);
throw ex; throw ex;
@ -78,11 +74,12 @@ export class AuthenticationConnection implements Disposable {
), ),
); );
const uri = Uri.joinPath(this.connection.baseAccountUri, 'register').with({
query: `${
scopes.includes('gitlens') ? 'referrer=gitlens&' : ''
}pass-token=true&return-url=${encodeURIComponent(callbackUri.toString())}`,
});
const uri = this.connection.getAccountsUri(
'register',
`${scopes.includes('gitlens') ? 'referrer=gitlens&' : ''}pass-token=true&return-url=${encodeURIComponent(
callbackUri.toString(),
)}`,
);
void (await env.openExternal(uri)); void (await env.openExternal(uri));
// Ensure there is only a single listener for the URI callback, in case the user starts the login process multiple times before completing it // Ensure there is only a single listener for the URI callback, in case the user starts the login process multiple times before completing it

+ 51
- 12
src/plus/gk/serverConnection.ts View File

@ -13,33 +13,45 @@ export class ServerConnection implements Disposable {
dispose() {} dispose() {}
@memoize() @memoize()
get baseApiUri(): Uri {
private get accountsUri(): Uri {
if (this.container.env === 'staging') { if (this.container.env === 'staging') {
return Uri.parse('https://stagingapi.gitkraken.com');
return Uri.parse('https://stagingapp.gitkraken.com');
} }
if (this.container.env === 'dev') { if (this.container.env === 'dev') {
return Uri.parse('https://devapi.gitkraken.com');
return Uri.parse('https://devapp.gitkraken.com');
} }
return Uri.parse('https://api.gitkraken.com');
return Uri.parse('https://app.gitkraken.com');
}
getAccountsUri(path?: string, query?: string) {
let uri = path != null ? Uri.joinPath(this.accountsUri, path) : this.accountsUri;
if (query != null) {
uri = uri.with({ query: query });
}
return uri;
} }
@memoize() @memoize()
get baseAccountUri(): Uri {
private get baseApiUri(): Uri {
if (this.container.env === 'staging') { if (this.container.env === 'staging') {
return Uri.parse('https://stagingapp.gitkraken.com');
return Uri.parse('https://stagingapi.gitkraken.com');
} }
if (this.container.env === 'dev') { if (this.container.env === 'dev') {
return Uri.parse('https://devapp.gitkraken.com');
return Uri.parse('https://devapi.gitkraken.com');
} }
return Uri.parse('https://app.gitkraken.com');
return Uri.parse('https://api.gitkraken.com');
}
getApiUrl(...pathSegments: string[]) {
return Uri.joinPath(this.baseApiUri, ...pathSegments).toString();
} }
@memoize() @memoize()
get baseGkApiUri(): Uri {
private get baseGkDevApiUri(): Uri {
if (this.container.env === 'staging') { if (this.container.env === 'staging') {
return Uri.parse('https://staging-api.gitkraken.dev'); return Uri.parse('https://staging-api.gitkraken.dev');
} }
@ -51,8 +63,12 @@ export class ServerConnection implements Disposable {
return Uri.parse('https://api.gitkraken.dev'); return Uri.parse('https://api.gitkraken.dev');
} }
getGkDevApiUrl(...pathSegments: string[]) {
return Uri.joinPath(this.baseGkDevApiUri, ...pathSegments).toString();
}
@memoize() @memoize()
get baseSiteUri(): Uri {
get siteUri(): Uri {
const { env } = this.container; const { env } = this.container;
if (env === 'staging') { if (env === 'staging') {
return Uri.parse('https://staging.gitkraken.com'); return Uri.parse('https://staging.gitkraken.com');
@ -65,6 +81,14 @@ export class ServerConnection implements Disposable {
return Uri.parse('https://gitkraken.com'); return Uri.parse('https://gitkraken.com');
} }
getSiteUri(path?: string, query?: string) {
let uri = path != null ? Uri.joinPath(this.siteUri, path) : this.siteUri;
if (query != null) {
uri = uri.with({ query: query });
}
return uri;
}
@memoize() @memoize()
get userAgent(): string { get userAgent(): string {
// TODO@eamodio figure out standardized format/structure for our user agents // TODO@eamodio figure out standardized format/structure for our user agents
@ -86,6 +110,9 @@ export class ServerConnection implements Disposable {
...init?.headers, ...init?.headers,
}, },
}; };
// TODO@eamodio handle common response errors
return await _fetch(url, options); return await _fetch(url, options);
} catch (ex) { } catch (ex) {
Logger.error(ex, scope); Logger.error(ex, scope);
@ -93,14 +120,22 @@ export class ServerConnection implements Disposable {
} }
} }
async fetchGraphQL(url: RequestInfo, request: GraphQLRequest, init?: RequestInit) {
return this.fetch(url, {
async fetchApi(path: string, init?: RequestInit, token?: string): Promise<Response> {
return this.fetch(this.getApiUrl(path), init, token);
}
async fetchApiGraphQL(path: string, request: GraphQLRequest, init?: RequestInit) {
return this.fetchApi(path, {
method: 'POST', method: 'POST',
...init, ...init,
body: JSON.stringify(request), body: JSON.stringify(request),
}); });
} }
async fetchGkDevApi(path: string, init?: RequestInit, token?: string): Promise<Response> {
return this.fetch(this.getGkDevApiUrl(path), init, token);
}
private async getAccessToken() { private async getAccessToken() {
const session = await this.container.subscription.getAuthenticationSession(); const session = await this.container.subscription.getAuthenticationSession();
if (session != null) return session.accessToken; if (session != null) return session.accessToken;
@ -114,3 +149,7 @@ export interface GraphQLRequest {
operationName?: string; operationName?: string;
variables?: Record<string, unknown>; variables?: Record<string, unknown>;
} }
export function getUrl(base: Uri, ...pathSegments: string[]) {
return Uri.joinPath(base, ...pathSegments).toString();
}

+ 7
- 12
src/plus/subscription/subscriptionService.ts View File

@ -17,7 +17,6 @@ import {
ProgressLocation, ProgressLocation,
StatusBarAlignment, StatusBarAlignment,
ThemeColor, ThemeColor,
Uri,
window, window,
} from 'vscode'; } from 'vscode';
import { getPlatform } from '@env/platform'; import { getPlatform } from '@env/platform';
@ -324,7 +323,7 @@ export class SubscriptionService implements Disposable {
@log() @log()
manage(): void { manage(): void {
void env.openExternal(this.connection.baseAccountUri);
void env.openExternal(this.connection.getAccountsUri());
} }
@log() @log()
@ -334,11 +333,7 @@ export class SubscriptionService implements Disposable {
if (this._subscription.account == null) { if (this._subscription.account == null) {
this.showPlans(); this.showPlans();
} else { } else {
void env.openExternal(
Uri.joinPath(this.connection.baseAccountUri, 'subscription').with({
query: 'product=gitlens&license=PRO',
}),
);
void env.openExternal(this.connection.getAccountsUri('subscription', 'product=gitlens&license=PRO'));
} }
await this.showAccountView(); await this.showAccountView();
} }
@ -356,8 +351,8 @@ export class SubscriptionService implements Disposable {
if (session == null) return false; if (session == null) return false;
try { try {
const rsp = await this.connection.fetch(
Uri.joinPath(this.connection.baseApiUri, 'resend-email').toString(),
const rsp = await this.connection.fetchApi(
'resend-email',
{ {
method: 'POST', method: 'POST',
body: JSON.stringify({ id: session.account.id }), body: JSON.stringify({ id: session.account.id }),
@ -410,7 +405,7 @@ export class SubscriptionService implements Disposable {
} }
private showPlans(): void { private showPlans(): void {
void env.openExternal(Uri.joinPath(this.connection.baseSiteUri, 'gitlens/pricing'));
void env.openExternal(this.connection.getSiteUri('gitlens/pricing'));
} }
@gate() @gate()
@ -550,8 +545,8 @@ export class SubscriptionService implements Disposable {
previewExpiresOn: this._subscription.previewTrial?.expiresOn, previewExpiresOn: this._subscription.previewTrial?.expiresOn,
}; };
const rsp = await this.connection.fetch(
Uri.joinPath(this.connection.baseApiUri, 'gitlens/checkin').toString(),
const rsp = await this.connection.fetchApi(
'gitlens/checkin',
{ {
method: 'POST', method: 'POST',
body: JSON.stringify(checkInData), body: JSON.stringify(checkInData),

+ 1
- 6
src/plus/workspaces/workspacesApi.ts View File

@ -1,4 +1,3 @@
import { Uri } from 'vscode';
import type { RequestInit } from '@env/fetch'; import type { RequestInit } from '@env/fetch';
import type { Container } from '../../container'; import type { Container } from '../../container';
import { Logger } from '../../system/logger'; import { Logger } from '../../system/logger';
@ -478,10 +477,6 @@ export class WorkspacesApi {
} }
private async fetch(request: GraphQLRequest, init?: RequestInit) { private async fetch(request: GraphQLRequest, init?: RequestInit) {
return this.connection.fetchGraphQL(
Uri.joinPath(this.connection.baseApiUri, 'api/projects/graphql').toString(),
request,
init,
);
return this.connection.fetchApiGraphQL('api/projects/graphql', request, init);
} }
} }

Loading…
Cancel
Save