Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Copy Exchange" command #1313

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@
},
"category": "Rest Client"
},
{
"command": "rest-client.copy-exchange",
"title": "Copy Exchange",
"icon": {
"light": "./images/copy.svg",
"dark": "./images/copy-inverse.svg"
},
"category": "Rest Client"
},
{
"command": "rest-client.generate-codesnippet",
"title": "Generate Code Snippet",
Expand Down Expand Up @@ -215,6 +224,10 @@
"command": "rest-client.copy-response-body",
"when": "httpResponsePreviewFocus"
},
{
"command": "rest-client.copy-exchange",
"when": "httpResponsePreviewFocus"
},
{
"command": "rest-client.copy-codesnippet",
"when": "codeSnippetPreviewFocus"
Expand All @@ -236,6 +249,11 @@
"command": "rest-client.copy-response-body",
"group": "navigation@3"
},
{
"when": "httpResponsePreviewFocus",
"command": "rest-client.copy-exchange",
"group": "navigation@4"
},
{
"when": "httpResponsePreviewFocus",
"command": "rest-client.fold-response",
Expand Down
26 changes: 25 additions & 1 deletion src/views/httpResponseWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class HttpResponseWebview extends BaseWebview {
this.context.subscriptions.push(commands.registerCommand('rest-client.copy-response-body', this.copyBody, this));
this.context.subscriptions.push(commands.registerCommand('rest-client.save-response', this.save, this));
this.context.subscriptions.push(commands.registerCommand('rest-client.save-response-body', this.saveBody, this));
this.context.subscriptions.push(commands.registerCommand('rest-client.copy-exchange', this.copyExchange, this));
}

public async render(response: HttpResponse, column: ViewColumn) {
Expand Down Expand Up @@ -161,6 +162,14 @@ export class HttpResponseWebview extends BaseWebview {
}
}

@trace('Copy Exchange')
private async copyExchange() {
if (this.activeResponse) {
const exchange = this.getFullExchangeString(this.activeResponse);
await this.clipboard.writeText(exchange);
}
}

@trace('Save Response')
private async save() {
if (this.activeResponse) {
Expand Down Expand Up @@ -210,6 +219,21 @@ export class HttpResponseWebview extends BaseWebview {
return `${prefix}(${response.timingPhases.total ?? 0}ms)`;
}

private getFullExchangeString(response: HttpResponse): string {
const requestString = this.getFullRequestString(response);
const responseString = this.getFullResponseString(response);

return `${requestString}${os.EOL}${os.EOL}${responseString}`;
}

private getFullRequestString(response: HttpResponse): string {
const request = response.request;
const statusLine = `${request.method} ${request.url} HTTP/1.1${os.EOL}`;
const headerString = formatHeaders(request.headers);
const body = request.body ? `${os.EOL}${request.body}` : '';
return `${statusLine}${headerString}${body}`;
}

private getFullResponseString(response: HttpResponse): string {
const statusLine = `HTTP/${response.httpVersion} ${response.statusCode} ${response.statusMessage}${os.EOL}`;
const headerString = formatHeaders(response.headers);
Expand Down Expand Up @@ -470,4 +494,4 @@ ${formatHeaders(response.headers)}`;
private static isHeadRequest({ request: { method } }: { request: HttpRequest }): boolean {
return method.toLowerCase() === 'head';
}
}
}