shubhamgoyal

Untitled

Dec 21st, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // a library to wrap and simplify api calls
  2. import apisauce from 'apisauce'
  3. import fixtureApi from './FixtureApi'
  4.  
  5. // our "constructor"
  6. const create = (baseURL = 'http://192.168.1.222:8000/') => {
  7.   // ------
  8.   // STEP 1
  9.   // ------
  10.   //
  11.   // Create and configure an apisauce-based api object.
  12.   //
  13.   const api = apisauce.create({
  14.     // base URL is read from the "constructor"
  15.     baseURL,
  16.     // here are some default headers
  17.     headers: {
  18.     },
  19.     // 10 second timeout...
  20.     timeout: 10000
  21.   })
  22.  
  23.   // ------
  24.   // STEP 2
  25.   // ------
  26.   //
  27.   // Define some functions that call the api.  The goal is to provide
  28.   // a thin wrapper of the api layer providing nicer feeling functions
  29.   // rather than "get", "post" and friends.
  30.   //
  31.   // I generally don't like wrapping the output at this level because
  32.   // sometimes specific actions need to be take on `403` or `401`, etc.
  33.   //
  34.   // Since we can't hide from that, we embrace it by getting out of the
  35.   // way at this level.
  36.   //
  37.   const getRoot = () => api.get('')
  38.   const getRate = () => api.get('rate_limit')
  39.   // const getUser = (username) => api.get('search/users', {q: username})
  40.   const getConfig = () => api.get('api/get_config/?format=json')
  41.   const getUser = () => api.get('rest-auth/user/')
  42.   const updateUser = () => console.log("updateUser")
  43.   const postSignUp = () => console.log("postSignUp")
  44.   const postLogin = () => console.log("postLogin")
  45.   const postFacebookLogin = body => api.post('rest-auth/facebook/', {
  46.     access_token: body.accessToken,
  47.   })
  48.   const postLogout = () => console.log("postLogout")
  49.   const getLessons = accessToken => {
  50.     api.setHeaders({
  51.       'Authorization': 'Token ' + accessToken,
  52.     })
  53.     return api.get('api/user/lessons/')
  54.   }
  55.   const getLesson = (accessToken, lessonId) => {
  56.     delete api.headers['Authorization']
  57.     api.setHeaders({
  58.       'Authorization': 'Token ' + accessToken,
  59.     })
  60.     return api.get('api/lessons/' + lessonId)
  61.   }
  62.   const postLessonComplete = () => console.log("postLessonComplete")
  63.   const getMessages = (accessToken, lastSyncId, lastReadId) => {
  64.     console.log("getMessages")
  65.     return fixtureApi.getMessages(accessToken, lastSyncId, lastReadId)
  66.   }
  67.   const postMessages = () => console.log("postMessages")
  68.  
  69.   // ------
  70.   // STEP 3
  71.   // ------
  72.   //
  73.   // Return back a collection of functions that we would consider our
  74.   // interface.  Most of the time it'll be just the list of all the
  75.   // methods in step 2.
  76.   //
  77.   // Notice we're not returning back the `api` created in step 1?  That's
  78.   // because it is scoped privately.  This is one way to create truly
  79.   // private scoped goodies in JavaScript.
  80.   //
  81.   return {
  82.     // a list of the API functions from step 2
  83.     getRoot,
  84.     getRate,
  85.     getUser,
  86.     getConfig,
  87.     updateUser,
  88.     postSignUp,
  89.     postLogin,
  90.     postFacebookLogin,
  91.     postLogout,
  92.     getLessons,
  93.     getLesson,
  94.     postLessonComplete,
  95.     getMessages,
  96.     postMessages,
  97.   }
  98. }
  99.  
  100. // let's return back our create method as the default.
  101. export default {
  102.   create
  103. }
Add Comment
Please, Sign In to add comment