-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
281 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
export const ExtensionId: string = 'humao.rest-client'; | ||
export const HistoryFileName: string = 'history.json'; | ||
export const HistoryItemsMaxCount: number = 50; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"use strict"; | ||
|
||
import { window, workspace, QuickPickItem, OutputChannel } from 'vscode'; | ||
import { PersistUtility } from '../persistUtility' | ||
import { HttpRequest } from '../models/httpRequest' | ||
import { HistoryQuickPickItem } from '../models/historyQuickPickItem' | ||
import { EOL } from 'os'; | ||
import * as fs from 'fs' | ||
|
||
var tmp = require('tmp') | ||
|
||
export class HistoryController { | ||
private _outputChannel: OutputChannel; | ||
|
||
constructor() { | ||
this._outputChannel = window.createOutputChannel('REST'); | ||
} | ||
|
||
run(): any { | ||
PersistUtility.load().then(requests => { | ||
if (!requests || requests.length <= 0) { | ||
window.showInformationMessage("No request history items are found!"); | ||
return; | ||
} | ||
var itemPickList: HistoryQuickPickItem[] = requests.map(request => { | ||
// TODO: add headers and body in pick item? | ||
let item = new HistoryQuickPickItem(); | ||
item.label = `${request.method.toUpperCase()} ${request.url}`; | ||
item.rawRequest = request; | ||
return item; | ||
}); | ||
|
||
window.showQuickPick(itemPickList, { placeHolder: "" }).then(item => { | ||
if (!item) { | ||
return; | ||
} | ||
this.createRequestInTempFile(item.rawRequest).then(path => { | ||
workspace.openTextDocument(path).then(d => { | ||
window.showTextDocument(d); | ||
}); | ||
}); | ||
}) | ||
}).catch(error => this.errorHandler(error)); | ||
} | ||
|
||
private createRequestInTempFile(request: HttpRequest): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
tmp.file({ prefix: 'vscode-restclient-' }, function _tempFileCreated(err, tmpFilePath, fd) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
let output = `${request.method.toUpperCase()} ${request.url}${EOL}`; | ||
if (request.headers) { | ||
for (var header in request.headers) { | ||
if (request.headers.hasOwnProperty(header)) { | ||
var value = request.headers[header]; | ||
output += `${header}: ${value}${EOL}`; | ||
} | ||
} | ||
} | ||
if (request.body) { | ||
output += `${EOL}${request.body}`; | ||
} | ||
fs.writeFile(tmpFilePath, output, error => { | ||
reject(error); | ||
return; | ||
}); | ||
resolve(tmpFilePath); | ||
}); | ||
}); | ||
} | ||
|
||
private errorHandler(error: any) { | ||
this._outputChannel.appendLine(error); | ||
this._outputChannel.show(); | ||
window.showErrorMessage("There was an error, please view details in output log"); | ||
} | ||
|
||
dispose() { | ||
this._outputChannel.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
"use strict"; | ||
|
||
import { window, workspace, Uri, StatusBarItem, StatusBarAlignment, OutputChannel } from 'vscode'; | ||
import { RequestParser } from '../parser' | ||
import { MimeUtility } from '../mimeUtility' | ||
import { HttpClient } from '../httpClient' | ||
import { HttpRequest } from '../models/httpRequest' | ||
import { RestClientSettings } from '../models/configurationSettings' | ||
import { PersistUtility } from '../persistUtility' | ||
|
||
export class RequestController { | ||
private _outputChannel: OutputChannel; | ||
private _statusBarItem: StatusBarItem; | ||
private _restClientSettings: RestClientSettings; | ||
private _httpClient: HttpClient; | ||
|
||
constructor() { | ||
this._outputChannel = window.createOutputChannel('REST'); | ||
this._statusBarItem = window.createStatusBarItem(StatusBarAlignment.Left); | ||
this._restClientSettings = new RestClientSettings(); | ||
this._httpClient = new HttpClient(this._restClientSettings); | ||
} | ||
|
||
run() { | ||
let editor = window.activeTextEditor; | ||
if (!editor || !editor.document) { | ||
return; | ||
} | ||
|
||
// Get selected text of selected lines or full document | ||
let selectedText: string; | ||
if (editor.selection.isEmpty) { | ||
selectedText = editor.document.getText(); | ||
} else { | ||
selectedText = editor.document.getText(editor.selection); | ||
} | ||
|
||
if (selectedText === '') { | ||
return; | ||
} | ||
|
||
if (this._restClientSettings.clearOutput) { | ||
this._outputChannel.clear(); | ||
} | ||
|
||
// clear status bar | ||
this._statusBarItem.text = `$(cloud-upload)`; | ||
this._statusBarItem.show(); | ||
|
||
// parse http request | ||
let httpRequest = RequestParser.parseHttpRequest(selectedText); | ||
if (!httpRequest) { | ||
return; | ||
} | ||
|
||
// set http request | ||
this._httpClient.send(httpRequest) | ||
.then(response => { | ||
let output = `HTTP/${response.httpVersion} ${response.statusCode} ${response.statusMessage}\n` | ||
for (var header in response.headers) { | ||
if (response.headers.hasOwnProperty(header)) { | ||
var value = response.headers[header]; | ||
output += `${header}: ${value}\n` | ||
} | ||
} | ||
|
||
let body = response.body; | ||
let contentType = response.headers['content-type']; | ||
if (contentType) { | ||
let type = MimeUtility.parse(contentType).type; | ||
if (type === 'application/json') { | ||
body = JSON.stringify(JSON.parse(body), null, 4); | ||
} | ||
} | ||
|
||
output += `\n${body}`; | ||
this._outputChannel.appendLine(`${output}\n`); | ||
this._outputChannel.show(true); | ||
|
||
this._statusBarItem.text = ` $(clock) ${response.elapsedMillionSeconds}ms`; | ||
this._statusBarItem.tooltip = 'duration'; | ||
|
||
// persist to history json file | ||
PersistUtility.save(httpRequest); | ||
}) | ||
.catch(error => { | ||
this._statusBarItem.text = ''; | ||
this._outputChannel.appendLine(`${error}\n`); | ||
this._outputChannel.show(true); | ||
}); | ||
} | ||
|
||
dispose() { | ||
this._outputChannel.dispose(); | ||
this._statusBarItem.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
|
||
import { QuickPickItem } from 'vscode'; | ||
import { HttpRequest } from '../models/httpRequest' | ||
|
||
export class HistoryQuickPickItem implements QuickPickItem { | ||
label: string; | ||
description: string; | ||
detail: string; | ||
rawRequest: HttpRequest; | ||
} |
Oops, something went wrong.