Skip to content

Commit

Permalink
chore: Add refreshFactoryOauthToken API method (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Sep 22, 2022
1 parent 04b23a4 commit e1c5aa6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/rest/remote-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ export interface IRemoteAPI {
* Returns a factory resolver.
*/
getFactoryResolver<T = FactoryResolver>(url: string, overrideParams?: { [params: string]: string }): Promise<T>;

/**
* Check the factory related OAuth token and set (update) git-credentials.
*/
refreshFactoryOauthToken(url: string): Promise<void>;
generateSshKey<T = che.ssh.SshPair>(service: string, name: string): Promise<T>;
createSshKey(sshKeyPair: che.ssh.SshPair): Promise<void>;
getSshKey<T = che.ssh.SshPair>(service: string, name: string): Promise<T>;
Expand Down Expand Up @@ -374,6 +379,16 @@ export class RemoteAPI implements IRemoteAPI {
});
}

refreshFactoryOauthToken(url: string): Promise<void> {
return new Promise((resolve, reject) => {
this.remoteAPI.refreshFactoryOauthToken(url)
.then(() => resolve())
.catch((error: AxiosError) => {
reject(new RequestError(error));
});
});
}

public generateSshKey<T = che.ssh.SshPair>(service: string, name: string): Promise<T> {
return new Promise<T>((resolve, reject) => {
this.remoteAPI.generateSshKey<T>(service, name)
Expand Down
9 changes: 9 additions & 0 deletions src/rest/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface IResources {
stop(workspaceId: string): AxiosPromise<void>;
getSettings<T>(): AxiosPromise<T>;
getFactoryResolver<T>(url: string, overrideParams?: { [params: string]: string }): AxiosPromise<T>;
refreshFactoryOauthToken(url: string): AxiosPromise<void>;
generateSshKey<T>(service: string, name: string): AxiosPromise<T>;
createSshKey(sshKeyPair: che.ssh.SshPair): AxiosPromise<void>;
getSshKey<T>(service: string, name: string): AxiosPromise<T>;
Expand Down Expand Up @@ -204,6 +205,14 @@ export class Resources implements IResources {
});
}

public refreshFactoryOauthToken(url: string): AxiosPromise<void> {
return this.axios.request<void>({
method: 'POST',
baseURL: this.baseUrl,
url: `${this.factoryUrl}/token/refresh?url=${url}`
});
}

public generateSshKey<T>(service: string, name: string): AxiosPromise<T> {
return this.axios.request<T>({
method: 'POST',
Expand Down
12 changes: 12 additions & 0 deletions test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,16 @@ describe('RestAPI >', () => {
});
});

it('should call refresh factory token', async () => {
axios.request.mockImplementationOnce(() => Promise.resolve({status: 200}));
await restApi.refreshFactoryOauthToken('http://test-location');

expect(axios.request).toHaveBeenCalledTimes(1);
expect(axios.request).toHaveBeenCalledWith({
'baseURL': '/api',
'method': 'POST',
'url': '/factory/token/refresh?url=http://test-location'
});
});

});

0 comments on commit e1c5aa6

Please sign in to comment.