How to use the xhr.fetch function in xhr

To help you get started, we’ve selected a few xhr 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 ajb413 / crowdsalable-eth-token / app / pubnub-functions / sms-handler.js View on Github external
let messages = [];

  // Create an object for each number that will be SMSed.
  // Object array will be the POST body to ClickSend.
  for (let number of crowdPhoneNumbers) {
    messages.push({
      source: 'pubnub-blocks',
      from: 'cstoken',
      body: body,
      to: number,
      custom_string: `TOK-${crowdsaleName}`
    });
  }

  // POST to ClickSend API
  return xhr.fetch(uri, {
    'method'  : 'POST',
    'headers' : {
      'Authorization' : authorization,
      'Content-Type': 'application/json'
    },
    'body': JSON.stringify({
      'messages': messages
    }),
    'timeout' : 5000
  })
  .then((res) => {
    return request.ok();
  })
  .catch((err) => {
    console.error(err);
    return request.abort();
github stephenlb / twitch-tv-obs-subtitles / functions / giphy.js View on Github external
return getVaultKey('giphy-key').then( apikey => {
            // Fetch URI and Cache Key
            const fetchKey = `${giphyURI}?` + [
                'q='       + search
            ,   'api_key=' + apikey
            ,   'rating='  + rating
            ,   'lang='    + 'en'
            ,   'limit='   + '10'
            ].join('&');

            // Return local Giphy result if cached
            if (fetchKey in giphyCache) return resolve(giphyCache[fetchKey]);

            // We don't have cached result so we can search
            // and then save to cache
            return xhr.fetch(fetchKey).then( giphyResponse => {
                // Save Local Cache
                giphyCache[fetchKey] = giphyResponse;
                // Return newly fetched Giphy Response
                return resolve(giphyResponse);
            } );
        } );
    } );
github initialstate / pubnub-live-twitter-dashboard / IBMWatsonUpdated.js View on Github external
return db.get('sentiment_db').then(function (val) {
         const sentimentDb = val || {};
         const sessionSentiment = sentimentDb[sessionId] || {
            overall: 0,
            positive: {
                count: 0,
                avg: 0
               },
            negative: {
                count: 0,
                avg: 0
            }
        };

        return XHR.fetch(apiUrl + '?' + querystring).then(function (r) {
            console.log(r);
            const body = JSON.parse(r.body);
            const type = body.sentiment.document.label;
            const score = body.sentiment.document.score;
            const cur = sessionSentiment[type] || { count: 0, avg: 0 };
            const curSum = cur.avg * cur.count;
            const newtotal = ++(cur.count);
            const newAvg = ((curSum) + Number(score)) / newtotal;
            console.log(newAvg);
            sessionSentiment[type] = {
                count: newtotal,
                avg: newAvg
            };

            sessionSentiment.overall =
                (sessionSentiment.positive.count
github ajb413 / chat-engine-vue / pubnub-functions / lex-text-on-request.js View on Github external
headers: {},
                    host: 'runtime.lex.us-east-1.amazonaws.com',
                    body: inputText,
                };
                
                signAWS(opts, { accessKeyId: AWS_access_key, secretAccessKey: AWS_secret_key });

                const http_options = {
                  'method': 'POST',
                  'body': opts.body,
                  'headers': opts.headers
                };

                opts.headers['Content-Type'] = contentType;

                return xhr.fetch('https://' + opts.host + opts.path, http_options).then((res) => {
                    let payload = {};
                    
                    if (res.body) {
                        payload['lex_text'] = res.headers['x-amz-lex-message'];
                        payload['lex_sound'] = ByteArrayToBase64(res['$buffer'].data);
                    } else {
                        const body = JSON.parse(res.body);
                        payload['error'] = body['message'];
                        response.status = 400;
                        return response.send('Bad Request');
                    }
                    
                    response.status = 200;
                    return response.send(payload);
                }).catch((error) => {
                    console.error(error);