本文整理汇总了TypeScript中@theintern/common.request函数的典型用法代码示例。如果您正苦于以下问题:TypeScript request函数的具体用法?TypeScript request怎么用?TypeScript request使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了request函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: sendJobState
sendJobState(jobId: string, data: JobState): CancellablePromise<void> {
const payload = JSON.stringify({
status: data.status || data.success ? 'completed' : 'error'
});
const url = `https://www.browserstack.com/automate/sessions/${jobId}.json`;
return request(url, {
method: 'put',
data: payload,
headers: {
'Content-Length': String(Buffer.byteLength(payload, 'utf8')),
'Content-Type': 'application/json'
},
password: this.accessKey,
username: this.username,
proxy: this.proxy
}).then<void>(response => {
if (response.status < 200 || response.status >= 300) {
return response.text().then(text => {
throw new Error(
text || `Server reported ${response.status} with no other data.`
);
});
}
});
}
示例2: loadText
/**
* Load a text resource
*/
function loadText(path: string): CancellablePromise<any> {
return request(path).then(response => {
if (!response.ok) {
throw new Error('Request failed: ' + response.status);
}
return response.text();
});
}
示例3: sendJobState
sendJobState(jobId: string, data: JobState): CancellablePromise<void> {
const params: { [key: string]: string | number } = {};
if (data.success != null) {
params['test[success]'] = String(data.success ? 1 : 0);
}
if (data.status) {
params['test[status_message]'] = data.status;
}
if (data.name) {
params['test[name]'] = data.name;
}
if (data.extra) {
params['test[extra]'] = JSON.stringify(data.extra);
}
if (data.tags && data.tags.length) {
params['groups'] = data.tags.join(',');
}
const url = `https://api.testingbot.com/v1/tests/${jobId}`;
const payload = stringify(params);
return request(url, {
method: 'put',
data: payload,
headers: {
'Content-Length': String(Buffer.byteLength(payload, 'utf8')),
'Content-Type': 'application/x-www-form-urlencoded'
},
password: this.accessKey,
username: this.username,
proxy: this.proxy
}).then(function(response) {
return response.text().then(text => {
if (text) {
const data = JSON.parse(text);
if (data.error) {
throw new Error(data.error);
} else if (!data.success) {
throw new Error('Job data failed to save.');
} else if (response.status !== 200) {
throw new Error(`Server reported ${response.status} with: ${text}`);
}
} else {
throw new Error(
`Server reported ${response.status} with no other data.`
);
}
});
});
}
示例4: _sendMessages
/**
* Some testing services have problems handling large message POSTs, so
* limit the maximum size of each POST body to maxPostSize bytes. Always
* send at least one message, even if it's more than maxPostSize bytes.
*/
protected _sendMessages(): CancellablePromise<any> | undefined {
const messages = this._messageBuffer;
if (messages.length === 0) {
return;
}
const block = [messages.shift()!];
let size = block[0].message.length;
while (
messages.length > 0 &&
size + messages[0].message.length < this._maxPostSize
) {
size += messages[0].message.length;
block.push(messages.shift()!);
}
this._activeRequest = request(this.url, {
method: 'post',
headers: { 'Content-Type': 'application/json' },
data: block.map(entry => entry.message)
})
.then(response => {
if (response.status === 200) {
return response.json<any[]>();
} else if (response.status === 204) {
return [];
} else {
throw new Error(response.statusText);
}
})
.then((results: any[]) => {
block.forEach((entry, index) => {
entry.resolve(results[index]);
});
})
.catch(error => {
block.forEach(entry => {
entry.reject(error);
});
})
.finally(() => {
this._activeRequest = undefined;
if (messages.length > 0) {
return this._sendMessages();
}
});
return this._activeRequest;
}
示例5: sendJobState
sendJobState(jobId: string, data: JobState): CancellablePromise<void> {
let url = parseUrl(this.restUrl || 'https://saucelabs.com/rest/v1/');
url.auth = this.username + ':' + this.accessKey;
url.pathname += this.username + '/jobs/' + jobId;
const payload = JSON.stringify({
build: data.buildId,
'custom-data': data.extra,
name: data.name,
passed: data.success,
public: data.visibility,
tags: data.tags
});
return request(formatUrl(url), {
method: 'put',
data: payload,
headers: {
'Content-Length': String(Buffer.byteLength(payload, 'utf8')),
'Content-Type': 'application/x-www-form-urlencoded'
},
password: this.accessKey,
username: this.username,
proxy: this.proxy
}).then(function(response) {
return response.text().then(text => {
if (text) {
const data = JSON.parse(text);
if (data.error) {
throw new Error(data.error);
}
if (response.status !== 200) {
throw new Error(`Server reported ${response.status} with: ${text}`);
}
} else {
throw new Error(
`Server reported ${response.status} with no other data.`
);
}
});
});
}
示例6: sendJobState
sendJobState(jobId: string, data: JobState): CancellablePromise<void> {
const payload = JSON.stringify({
action: 'set_score',
score: data.status || data.success ? 'pass' : 'fail'
});
const url = `https://crossbrowsertesting.com/api/v3/selenium/${jobId}`;
return request(url, {
method: 'put',
data: payload,
headers: {
'Content-Length': String(Buffer.byteLength(payload, 'utf8')),
'Content-Type': 'application/json'
},
username: this.username,
password: this.accessKey,
proxy: this.proxy
}).then<void>(response => {
if (response.status !== 200) {
return response.text().then(text => {
if (text) {
const data = JSON.parse(text);
if (data.status) {
throw new Error(`Could not save test status (${data.message})`);
}
throw new Error(`Server reported ${response.status} with: ${text}`);
} else {
throw new Error(
`Server reported ${response.status} with no other data.`
);
}
});
}
});
}