Added ability to cancel requests

This commit is contained in:
Andrew Bastin
2020-05-30 18:28:46 -04:00
parent 0fccd3d5cb
commit 4a2f698ff8
2 changed files with 81 additions and 48 deletions

View File

@@ -187,6 +187,7 @@
"json_prettify_invalid_body": "Couldn't prettify an invalid body, solve json syntax errors and try again", "json_prettify_invalid_body": "Couldn't prettify an invalid body, solve json syntax errors and try again",
"prettify_body": "Prettify body", "prettify_body": "Prettify body",
"cancel": "Cancel", "cancel": "Cancel",
"cancelled": "Cancelled",
"save": "Save", "save": "Save",
"dismiss": "Dismiss", "dismiss": "Dismiss",
"are_you_sure": "Are you sure?", "are_you_sure": "Are you sure?",

View File

@@ -189,12 +189,25 @@
</li> </li>
<li class="shrink"> <li class="shrink">
<label class="hide-on-small-screen" for="send">&nbsp;</label> <label class="hide-on-small-screen" for="send">&nbsp;</label>
<button :disabled="!isValidURL" @click="sendRequest" id="send" ref="sendButton"> <button
v-if="!runningRequest"
:disabled="!isValidURL"
@click="sendRequest"
id="send"
ref="sendButton"
>
{{ $t("send") }} {{ $t("send") }}
<span> <span>
<i class="material-icons">send</i> <i class="material-icons">send</i>
</span> </span>
</button> </button>
<button v-else @click="cancelRequest" id="send" ref="sendButton">
{{ $t("cancel") }}
<span>
<i class="material-icons">clear</i>
</span>
</button>
</li> </li>
</ul> </ul>
<div class="blue"> <div class="blue">
@@ -1332,7 +1345,7 @@ import runTestScriptWithVariables from "../functions/postwomanTesting"
import parseTemplateString from "../functions/templating" import parseTemplateString from "../functions/templating"
import AceEditor from "../components/ui/ace-editor" import AceEditor from "../components/ui/ace-editor"
import { tokenRequest, oauthRedirect } from "../assets/js/oauth" import { tokenRequest, oauthRedirect } from "../assets/js/oauth"
import { sendNetworkRequest } from "../functions/network" import { cancelRunningRequest, sendNetworkRequest } from "../functions/network"
import { fb } from "../functions/fb" import { fb } from "../functions/fb"
import { getEditorLangForMimeType } from "~/functions/editorutils" import { getEditorLangForMimeType } from "~/functions/editorutils"
import { import {
@@ -1450,6 +1463,7 @@ export default {
files: [], files: [],
filenames: "", filenames: "",
navigatorShare: navigator.share, navigatorShare: navigator.share,
runningRequest: false,
settings: { settings: {
SCROLL_INTO_ENABLED: SCROLL_INTO_ENABLED:
@@ -2085,6 +2099,9 @@ export default {
} }
return await sendNetworkRequest(requestOptions, this.$store) return await sendNetworkRequest(requestOptions, this.$store)
}, },
cancelRequest() {
cancelRunningRequest(this.$store)
},
async sendRequest() { async sendRequest() {
this.$toast.clear() this.$toast.clear()
if (this.settings.SCROLL_INTO_ENABLED) this.scrollInto("response") if (this.settings.SCROLL_INTO_ENABLED) this.scrollInto("response")
@@ -2154,12 +2171,16 @@ export default {
headers = headersObject headers = headersObject
try { try {
const startTime = Date.now() const startTime = Date.now()
this.runningRequest = true
const payload = await this.makeRequest( const payload = await this.makeRequest(
auth, auth,
headers, headers,
requestBody, requestBody,
this.showPreRequestScript && this.preRequestScript this.showPreRequestScript && this.preRequestScript
) )
this.runningRequest = false
const duration = Date.now() - startTime const duration = Date.now() - startTime
this.$toast.info(this.$t("finished_in", { duration }), { this.$toast.info(this.$t("finished_in", { duration }), {
icon: "done", icon: "done",
@@ -2202,6 +2223,13 @@ export default {
} }
})() })()
} catch (error) { } catch (error) {
this.runningRequest = false
// If the error is caused by cancellation, do nothing
if (error === "cancellation") {
this.response.status = this.$t("cancelled")
this.response.body = this.$t("cancelled")
} else {
console.log(error) console.log(error)
if (error.response) { if (error.response) {
this.response.headers = error.response.headers this.response.headers = error.response.headers
@@ -2220,7 +2248,10 @@ export default {
preRequestScript: this.preRequestScript, preRequestScript: this.preRequestScript,
} }
if ((this.preRequestScript && this.showPreRequestScript) || hasPathParams(this.params)) { if (
(this.preRequestScript && this.showPreRequestScript) ||
hasPathParams(this.params)
) {
let environmentVariables = getEnvironmentVariablesFromScript(this.preRequestScript) let environmentVariables = getEnvironmentVariablesFromScript(this.preRequestScript)
environmentVariables = addPathParamsToVariables(this.params, environmentVariables) environmentVariables = addPathParamsToVariables(this.params, environmentVariables)
entry.path = parseTemplateString(entry.path, environmentVariables) entry.path = parseTemplateString(entry.path, environmentVariables)
@@ -2254,6 +2285,7 @@ export default {
} }
} }
} }
}
// tests // tests
const syntheticResponse = { const syntheticResponse = {
status: this.response.status, status: this.response.status,