mero909

Api Factory

Feb 25th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Copyright 2016 Thomson Reuters/ONESOURCE. All rights reserved.
  3.  */
  4.  
  5. (function () {
  6.     'use strict';
  7.  
  8.     angular
  9.         .module('indirect.determination')
  10.         .factory('apiFactory', ApiFactory);
  11.  
  12.     ApiFactory.$inject = ['$http', '$log', 'lonestarUtilityFactory', 'indirectEnumeration'];
  13.  
  14.     /**
  15.      * A list of service methods that connect to the RESTful api. <p/>
  16.      *
  17.      * @param $http The angular $http service.
  18.      * @param $log The angular $log service.
  19.      * @param lonestarUtilityFactory A reference to the utilityFactory service in determination.
  20.      * @param indirectEnumeration A reference to the indirectEnumeration service (constants).
  21.      *
  22.      * @returns object A single object of service methods for connecting to a RESTful api.
  23.      */
  24.     function ApiFactory($http, $log, lonestarUtilityFactory, indirectEnumeration) {
  25.         var service = {};
  26.  
  27.         service.config = {
  28.             'host': '',
  29.             '$lonestarUser': null,
  30.             '$lonestarConfig': null
  31.         };
  32.  
  33.         /**
  34.          * Gets the base Url for the RESTful API for both LS2 and standalone. <p/>
  35.          *
  36.          * @returns string The base Url host for the API to both LS2 and standalone.
  37.          */
  38.         service.getHost = function getHost() {
  39.             if (service.config.host !== '') {
  40.                 return service.config.host;
  41.             }
  42.  
  43.             try {
  44.                 if (lonestarUtilityFactory.isInLoneStar()) {
  45.                     service.config.$lonestarUser = lonestarUtilityFactory.getUserInfo();
  46.                     service.config.$lonestarConfig = lonestarUtilityFactory.getLoneStarConfig();
  47.  
  48.                     if (lonestarUtilityFactory.hasLoneStarConfig(service.config.$lonestarConfig)) {
  49.                         var serviceSuffix = indirectEnumeration.ZuulServiceId + '/' + indirectEnumeration.ApiVersion;
  50.                         service.config.host = service.config.$lonestarConfig.getBaseUrl() + serviceSuffix;
  51.                     }
  52.                 }
  53.             } catch (err) {
  54.                 $log.debug(err);
  55.             }
  56.  
  57.             return service.config.host;
  58.         };
  59.  
  60.         /**
  61.          * This method fetches a list of all companies from the backend. <p/>
  62.          *
  63.          * @return {*} Returns a promise with the list of all companies.
  64.          */
  65.         service.getListOfCompanies = function getListOfCompanies() {
  66.             return $http.get(service.getHost() + '/companies');
  67.         };
  68.  
  69.         /**
  70.          * Gets the registration info for a user by userId. <p/>
  71.          *
  72.          * @param userId The user identifier.
  73.          *
  74.          * @returns {*} Returns a promise of registration information.
  75.          */
  76.         service.getRegistrationByUserId = function getRegistrationByUserId(userId) {
  77.             return $http({
  78.                         method: 'GET',
  79.                         url: service.getHost() + '/getRegistrationByUserId',
  80.                         data : {
  81.                             merchantId: userId
  82.                         }
  83.                    });
  84.         };
  85.  
  86.         return service;
  87.     }
  88. }());
Add Comment
Please, Sign In to add comment