requestService.ts 2.1 KB
Newer Older
S
Sandeep Somavarapu 已提交
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { IRequestOptions, IRequestContext } from 'vs/platform/request/common/request';
import { CancellationToken } from 'vs/base/common/cancellation';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ILogService } from 'vs/platform/log/common/log';
import { RequestChannelClient } from 'vs/platform/request/common/requestIpc';
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
S
Sandeep Somavarapu 已提交
12
import { RequestService as BrowserRequestService } from 'vs/platform/request/browser/requestService';
S
Sandeep Somavarapu 已提交
13

S
Sandeep Somavarapu 已提交
14
export class RequestService extends BrowserRequestService {
S
Sandeep Somavarapu 已提交
15 16 17 18

	private readonly remoteRequestChannel: RequestChannelClient | null;

	constructor(
19
		private readonly requestHandler: ((options: IRequestOptions) => Promise<IRequestContext>) | undefined,
S
Sandeep Somavarapu 已提交
20 21 22 23 24 25 26 27 28 29
		@IRemoteAgentService remoteAgentService: IRemoteAgentService,
		@IConfigurationService configurationService: IConfigurationService,
		@ILogService logService: ILogService
	) {
		super(configurationService, logService);
		const connection = remoteAgentService.getConnection();
		this.remoteRequestChannel = connection ? new RequestChannelClient(connection.getChannel('request')) : null;
	}

	async request(options: IRequestOptions, token: CancellationToken): Promise<IRequestContext> {
30 31 32
		if (this.requestHandler) {
			return this.requestHandler(options);
		}
S
Sandeep Somavarapu 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
		try {
			const context = await super.request(options, token);
			if (this.remoteRequestChannel && context.res.statusCode === 405) {
				return this.remoteRequestChannel.request(options, token);
			}
			return context;
		} catch (error) {
			if (this.remoteRequestChannel) {
				const result = await this.remoteRequestChannel.request(options, token);
				return result;
			}
			throw error;
		}
	}

}