當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript common.request函數代碼示例

本文整理匯總了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.`
          );
        });
      }
    });
  }
開發者ID:theintern,項目名稱:digdug,代碼行數:26,代碼來源:BrowserStackTunnel.ts

示例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();
  });
}
開發者ID:edhager,項目名稱:intern,代碼行數:11,代碼來源:util.ts

示例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.`
          );
        }
      });
    });
  }
開發者ID:theintern,項目名稱:digdug,代碼行數:51,代碼來源:TestingBotTunnel.ts

示例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;
  }
開發者ID:edhager,項目名稱:intern,代碼行數:55,代碼來源:Http.ts

示例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.`
          );
        }
      });
    });
  }
開發者ID:theintern,項目名稱:digdug,代碼行數:44,代碼來源:SauceLabsTunnel.ts

示例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.`
            );
          }
        });
      }
    });
  }
開發者ID:theintern,項目名稱:digdug,代碼行數:37,代碼來源:CrossBrowserTestingTunnel.ts


注:本文中的@theintern/common.request函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。