Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // a library to wrap and simplify api calls
- import apisauce from 'apisauce'
- import fixtureApi from './FixtureApi'
- // our "constructor"
- const create = (baseURL = 'http://192.168.1.222:8000/') => {
- // ------
- // STEP 1
- // ------
- //
- // Create and configure an apisauce-based api object.
- //
- const api = apisauce.create({
- // base URL is read from the "constructor"
- baseURL,
- // here are some default headers
- headers: {
- },
- // 10 second timeout...
- timeout: 10000
- })
- // ------
- // STEP 2
- // ------
- //
- // Define some functions that call the api. The goal is to provide
- // a thin wrapper of the api layer providing nicer feeling functions
- // rather than "get", "post" and friends.
- //
- // I generally don't like wrapping the output at this level because
- // sometimes specific actions need to be take on `403` or `401`, etc.
- //
- // Since we can't hide from that, we embrace it by getting out of the
- // way at this level.
- //
- const getRoot = () => api.get('')
- const getRate = () => api.get('rate_limit')
- // const getUser = (username) => api.get('search/users', {q: username})
- const getConfig = () => api.get('api/get_config/?format=json')
- const getUser = () => api.get('rest-auth/user/')
- const updateUser = () => console.log("updateUser")
- const postSignUp = () => console.log("postSignUp")
- const postLogin = () => console.log("postLogin")
- const postFacebookLogin = body => api.post('rest-auth/facebook/', {
- access_token: body.accessToken,
- })
- const postLogout = () => console.log("postLogout")
- const getLessons = accessToken => {
- api.setHeaders({
- 'Authorization': 'Token ' + accessToken,
- })
- return api.get('api/user/lessons/')
- }
- const getLesson = (accessToken, lessonId) => {
- delete api.headers['Authorization']
- api.setHeaders({
- 'Authorization': 'Token ' + accessToken,
- })
- return api.get('api/lessons/' + lessonId)
- }
- const postLessonComplete = () => console.log("postLessonComplete")
- const getMessages = (accessToken, lastSyncId, lastReadId) => {
- console.log("getMessages")
- return fixtureApi.getMessages(accessToken, lastSyncId, lastReadId)
- }
- const postMessages = () => console.log("postMessages")
- // ------
- // STEP 3
- // ------
- //
- // Return back a collection of functions that we would consider our
- // interface. Most of the time it'll be just the list of all the
- // methods in step 2.
- //
- // Notice we're not returning back the `api` created in step 1? That's
- // because it is scoped privately. This is one way to create truly
- // private scoped goodies in JavaScript.
- //
- return {
- // a list of the API functions from step 2
- getRoot,
- getRate,
- getUser,
- getConfig,
- updateUser,
- postSignUp,
- postLogin,
- postFacebookLogin,
- postLogout,
- getLessons,
- getLesson,
- postLessonComplete,
- getMessages,
- postMessages,
- }
- }
- // let's return back our create method as the default.
- export default {
- create
- }
Add Comment
Please, Sign In to add comment