Advertisement
Guest User

Untitled

a guest
Mar 12th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { AsyncStorage } from 'react-native';
  2. import jsrsasign from 'jsrsasign';
  3. import HttpError from './HttpError';
  4. import { Alert } from 'react-native';
  5.  
  6. export default async function request(url, body = {}, params = {}, type = 'POST') {
  7.   const pubKey = await AsyncStorage.getItem('@EncryptStore:publicKey');
  8.   const privateKey = await AsyncStorage.getItem('@EncryptStore:privateKey');
  9.   let token = await AsyncStorage.getItem('@TopUpStore:Token');
  10.   token = token ? token : '';
  11.  
  12.   if (pubKey === null || privateKey === null) {
  13.     return;
  14.   }
  15.  
  16.   params.token = token;
  17.   url += (url.indexOf('?') === -1 ? '?' : '&') + queryParams(params);
  18.   const bodyString = type === 'POST' || type === 'DELETE' ? JSON.stringify(body) : '';
  19.  
  20.   const signature = jsrsasign.KJUR.crypto.Util.md5(pubKey);
  21.   const sig = new jsrsasign.KJUR.crypto.Signature({ alg: 'SHA256withRSA' });
  22.   sig.init(privateKey);
  23.   sig.updateString(bodyString);
  24.   const sigValueHex = sig.sign();
  25.   const base64Sign = jsrsasign.hextob64(sigValueHex);
  26.  
  27.   const post = {
  28.     method: type,
  29.     headers: {
  30.       Accept: 'application/json',
  31.       'Content-Type': 'application/json',
  32.       Sign: base64Sign,
  33.       Signature: signature
  34.     },
  35.   };
  36.  
  37.   if (type === 'POST' || type === 'DELETE') post.body = bodyString;
  38.  
  39.   LOG('\n===== START HTTP REQUEST =====');
  40.   LOG(`HTTP send url: ${url}`);
  41.   LOG(`HTTP send body: ${bodyString}`);
  42.  
  43.   const response = await fetch(url, post);
  44.   const responseStatus = await response.status;
  45.   LOG(`HTTP response code: ${responseStatus}`);
  46.  
  47.   if (responseStatus !== 200) {
  48.     const responseBody = await response.json();
  49.     let errorText = '';
  50.  
  51.     if (responseBody.Message !== undefined) {
  52.       errorText = responseBody.Message;
  53.     }
  54.  
  55.     if (responseBody.ModelState !== undefined) {
  56.       if (Object.keys(responseBody.ModelState)[0]) {
  57.         errorText =
  58.           responseBody.ModelState[Object.keys(responseBody.ModelState)[0]][0];
  59.       }
  60.     }
  61.  
  62.     LOG(`HTTP error body: ${JSON.stringify(responseBody)}`);
  63.     LOG('===== END HTTP REQUEST =====\n');
  64.  
  65.     Alert.alert('Error', errorText);
  66.     throw new HttpError(errorText);
  67.   }
  68.  
  69.   LOG(`HTTP response code: ${responseStatus}`);
  70.   LOG('===== END HTTP REQUEST =====\n');
  71.  
  72.   return response;
  73. }
  74.  
  75. function queryParams(params) {
  76.   return Object.keys(params)
  77.     .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
  78.     .join('&');
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement