How to use gaxios - 9 common examples

To help you get started, we’ve selected a few gaxios examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github JustinBeckwith / linkinator / src / index.ts View on Github external
// Perform a HEAD or GET request based on the need to crawl
    let status = 0;
    let state = LinkState.BROKEN;
    let data = '';
    let shouldRecurse = false;
    try {
      let res = await gaxios.request({
        method: opts.crawl ? 'GET' : 'HEAD',
        url: opts.url.href,
        responseType: opts.crawl ? 'text' : 'stream',
        validateStatus: () => true,
      });

      // If we got an HTTP 405, the server may not like HEAD. GET instead!
      if (res.status === 405) {
        res = await gaxios.request({
          method: 'GET',
          url: opts.url.href,
          responseType: 'stream',
          validateStatus: () => true,
        });
      }

      // Assume any 2xx status is 👌
      status = res.status;
      if (res.status >= 200 && res.status < 300) {
        state = LinkState.OK;
      }
      data = res.data;
      shouldRecurse = isHtml(res);
    } catch (err) {
      // request failure: invalid domain name, etc.
github googleapis / repo-automation-bots / packages / gcf-utils / src / gcf-utils.ts View on Github external
? JSON.parse(req.body.toString('utf8'))
      : req.body) as Scheduled;
    // PubSub messages have their payload encoded in body.message.data
    // as a base64 blob.
    if (body.message && body.message.data) {
      body = JSON.parse(Buffer.from(body.message.data, 'base64').toString());
    }

    if (body.repo) {
      // Job was scheduled for a single repository:
      this.receivePromise(body.repo, id, body, eventName);
    } else {
      // Job should be run on all managed repositories:
      const url =
        'https://raw.githubusercontent.com/googleapis/sloth/master/repos.json';
      const res = await request({ url });
      const { repos } = res.data;
      // We process WORK_SIZE repos in parallel:
      const WORK_SIZE = 3;
      while (repos.length) {
        await Promise.all(
          repos.splice(0, WORK_SIZE).map(repo => {
            return this.receivePromise(repo.repo, id, body, eventName);
          })
        );
      }
    }
  }
github googleapis / gcp-metadata / src / index.ts View on Github external
// reasons for this:
  //
  // 1. the DNS is slow in some GCP environments; by checking both, we might
  //    detect the runtime environment signficantly faster.
  // 2. we can't just check the IP, which is tarpitted and slow to respond
  //    on a user's local machine.
  //
  // Additional logic has been added to make sure that we don't create an
  // unhandled rejection in scenarios where a failure happens sometime
  // after a success.
  //
  // Note, however, if a failure happens prior to a success, a rejection should
  // occur, this is for folks running locally.
  //
  let responded = false;
  const r1: Promise = request(options)
    .then(res => {
      responded = true;
      return res;
    })
    .catch(err => {
      if (responded) {
        return r2;
      } else {
        responded = true;
        throw err;
      }
    });
  const r2: Promise = request(secondaryOptions)
    .then(res => {
      responded = true;
      return res;
github googleapis / sloth / src / issue.ts View on Github external
async function getRepoIssues(repo: Repo, flags?: Flags): Promise {
  const [owner, name] = repo.repo.split('/');
  const result = {issues: new Array(), repo};
  let res!: GaxiosResponse;
  const rootUrl = 'https://drghs.endpoints.devrel-prod.cloud.goog/api/v1';
  const url = `${rootUrl}/${repo.repo}/issues?key=${apiKey}&closed=false`;
  try {
    res = await request({url});
  } catch (e) {
    console.warn(`Error fetching issues for ${repo.repo}.`);
    //console.warn(e);
    return result;
  }

  if (!res.data || !res.data.Issues) {
    return result;
  }

  res!.data.Issues.forEach(r => {
    const api = getApi(r, repo);
    const issue: Issue = {
      owner,
      name,
      language: repo.language,
github googleapis / gaxios / samples / quickstart.js View on Github external
async function quickstart() {
  const url = 'https://www.googleapis.com/discovery/v1/apis/';
  const res = await request({url});
  console.log(`status: ${res.status}`);
  console.log(`data:`);
  console.log(res.data);
}
quickstart();
github googleapis / gcp-metadata / src / index.ts View on Github external
//
  let responded = false;
  const r1: Promise = request(options)
    .then(res => {
      responded = true;
      return res;
    })
    .catch(err => {
      if (responded) {
        return r2;
      } else {
        responded = true;
        throw err;
      }
    });
  const r2: Promise = request(secondaryOptions)
    .then(res => {
      responded = true;
      return res;
    })
    .catch(err => {
      if (responded) {
        return r1;
      } else {
        responded = true;
        throw err;
      }
    });
  return Promise.race([r1, r2]);
}
github google / js-green-licenses / src / github.ts View on Github external
private async apiPost(path: string, body?: {}): Promise {
    const url = urlParse('https://api.github.com');
    url.pathname = posixPath.join(this.pathPrefix, path);
    const resp = await request({
      method: 'POST',
      url: urlFormat(url),
      data: body,
      ...this.getAxiosConfig(),
    });
    return resp.data;
  }
github google / js-green-licenses / src / github.ts View on Github external
private async apiGet(
    path: string,
    params?: QueryParams
  ): Promise {
    const url = urlParse('https://api.github.com', true);
    url.pathname = posixPath.join(this.pathPrefix, path);
    if (params) {
      url.query = params;
    }
    const resp = await request({
      method: 'GET',
      url: urlFormat(url),
      ...this.getAxiosConfig(),
    });
    return resp.data;
  }
github faastjs / faast.js / src / google / google-faast.ts View on Github external
import { Mutable } from "../types";
import {
    FunctionCallSerialized,
    FunctionReturnSerialized,
    WrapperOptions
} from "../wrapper";
import { publishPubSub, receiveMessages } from "./google-queue";
import { shouldRetryRequest } from "./google-shared";
import * as googleTrampolineHttps from "./google-trampoline-https";
import * as googleTrampolineQueue from "./google-trampoline-queue";

import CloudFunctions = cloudfunctions_v1;
import PubSubApi = pubsub_v1;
import CloudBilling = cloudbilling_v1;

const gaxios = new Gaxios({
    retryConfig: {
        retry: 3,
        noResponseRetries: 3,
        shouldRetry: shouldRetryRequest(log.retry)
    }
});

/**
 * Valid Google Cloud
 * {@link https://cloud.google.com/compute/docs/regions-zones/ | regions}.
 * Only some of these [regions have Cloud Functions](https://cloud.google.com/functions/docs/locations).
 * @public
 */
export type GoogleRegion =
    | "asia-east1"
    | "asia-east2"

gaxios

A simple common HTTP client specifically for Google APIs and services.

Apache-2.0
Latest version published 4 months ago

Package Health Score

89 / 100
Full package analysis

Popular gaxios functions