SHARE
TWEET
Untitled
a guest
Apr 27th, 2018
53
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- import DI from "injector";
- import Task from "@/utils/task";
- export default function(DIContainer) {
- this.client = DIContainer.HttpClient;
- this.token = null;
- this.setToken = function(token) {
- this.client.defaults.headers["Authorization"] = "Bearer " + token;
- this.token = token;
- };
- this.getTokenParams = function() {
- return ["access_token", this.token];
- };
- this.get = (path, params = []) => {
- return this.client.get(
- DIContainer.ApiUrlBuilder(path) + DIContainer.ParamsUrlBuilder(params)
- );
- };
- this.getForEmbed = function(url) {
- return this.client.get(url);
- };
- this.externalGet = function(path, params = []) {
- let externalResource = DIContainer.root + "/" + path;
- return this.client.get(
- externalResource + DIContainer.ParamsUrlBuilder(params)
- );
- };
- this.getTask = function(path, params = []) {
- let url =
- DIContainer.ApiUrlBuilder(path) + DIContainer.ParamsUrlBuilder(params);
- return new Task(this.client.get, url);
- };
- this.post = (path, body, headers = {}, propagateErrors = false) => {
- // return this.client.post(DIContainer.ApiUrlBuilder(path), body);
- return new Promise((resolve, reject) => {
- this.client
- .post(DIContainer.ApiUrlBuilder(path), body, headers)
- .then(resolve)
- .catch(error => {
- if (propagateErrors) DIContainer.ErrorManager.propagateErrors(error);
- reject(error);
- });
- });
- };
- this.put = (path, body) => {
- return this.client.put(DIContainer.ApiUrlBuilder(path), body);
- };
- this.delete = (path, params = []) => {
- return this.client.delete(
- DIContainer.ApiUrlBuilder(path) + DIContainer.ParamsUrlBuilder(params)
- );
- };
- this.getAndMap = function(path, params = [], decoderModel) {
- return new Promise((resolve, reject) => {
- this.get(path, params)
- .then(response => {
- if (DIContainer.Service.Comp.isArray(response.data)) {
- resolve(
- response.data.map(o =>
- DIContainer.Service.Decoder.map(decoderModel, o)
- )
- );
- } else {
- resolve(
- DIContainer.Service.Decoder.map(decoderModel, response.data)
- );
- }
- })
- .catch(reason => reject(reason));
- });
- };
RAW Paste Data

