API Docs for: 1.5.0
Show:

File: src/CAPI.js

  1. /* global define */
  2. define(['authAgents/SessionAuthAgent', 'authAgents/HttpBasicAuthAgent', 'ConnectionManager',
  3. 'ConnectionFeatureFactory', 'connections/XmlHttpRequestConnection', 'connections/MicrosoftXmlHttpRequestConnection',
  4. 'services/DiscoveryService', 'services/ContentService', 'services/ContentTypeService',
  5. 'services/UserService', 'utils/extend'],
  6. function (SessionAuthAgent, HttpBasicAuthAgent, ConnectionManager,
  7. ConnectionFeatureFactory, XmlHttpRequestConnection, MicrosoftXmlHttpRequestConnection,
  8. DiscoveryService, ContentService, ContentTypeService,
  9. UserService, extend) {
  10. "use strict";
  11.  
  12. /**
  13. * Creates an instance of CAPI - main object which handles the API initialization and gives ability to retrieve various services.
  14. * Could be created only in one instance. Handles connections, authorization and REST paths discovery automatically.
  15. *
  16. * @class CAPI
  17. * @constructor
  18. * @param endPointUrl {String} url pointing to REST root
  19. * @param authenticationAgent {Object} Instance of one of the AuthAgents (e.g. SessionAuthAgent, HttpBasicAuthAgent)
  20. * @param [options] {Object} Object containing different options for the CAPI
  21. * @param [options.rootPath='/api/ezp/v2/'] {String} the API root path
  22. * @param [options.logRequests=false] {Boolean} whether to log requests
  23. * @param [options.siteAccess=null] {String|null} siteaccess in which requests should be executed
  24. * @example
  25. * var authAgent = new SessionAuthAgent({
  26. login: "admin",
  27. password: "admin"
  28. }),
  29. jsCAPI = new CAPI(
  30. 'http://ez.git.local', authAgent, {
  31. logRequests: true, // Whether we should log each request to the js console or not
  32. rootPath: '/api/ezp/v2/', // Path to the REST root
  33. connectionStack: [ // Array of connections, should be filled-in in preferred order
  34. {connection: XmlHttpRequestConnection},
  35. {connection: MicrosoftXmlHttpRequestConnection}
  36. ]
  37. });
  38. */
  39. var CAPI = function (endPointUrl, authenticationAgent, options) {
  40. var defaultOptions,
  41. mergedOptions,
  42. connectionFactory,
  43. connectionManager,
  44. discoveryService,
  45. contentService,
  46. contentTypeService,
  47. userService;
  48.  
  49. // Options used if not overwritten from the outside
  50. defaultOptions = {
  51. logRequests: false, // Whether we should log each request to the js console or not
  52. rootPath: '/api/ezp/v2/', // Path to the REST root
  53. connectionStack: [ // Array of connections, should be filled-in in preferred order
  54. {connection: XmlHttpRequestConnection},
  55. {connection: MicrosoftXmlHttpRequestConnection}
  56. ],
  57. siteAccess: null
  58. };
  59.  
  60. authenticationAgent.setCAPI(this);
  61.  
  62. // Merging provided options (if any) with defaults
  63. mergedOptions = extend({}, defaultOptions, options);
  64.  
  65. connectionFactory = new ConnectionFeatureFactory(mergedOptions.connectionStack);
  66. connectionManager = new ConnectionManager(endPointUrl, authenticationAgent, connectionFactory, mergedOptions.siteAccess);
  67. connectionManager.logRequests = mergedOptions.logRequests;
  68. discoveryService = new DiscoveryService(mergedOptions.rootPath, connectionManager);
  69.  
  70. /**
  71. * Checks that the CAPI instance is logged in
  72. *
  73. * @method isLoggedIn
  74. * @param {Function} callback
  75. */
  76. this.isLoggedIn = function (callback) {
  77. authenticationAgent.isLoggedIn(callback);
  78. };
  79.  
  80. /**
  81. * Logs in the user
  82. *
  83. * @method logIn
  84. * @param {Object} [credentials]
  85. * @param {String} credentials.login
  86. * @param {String} credentials.password
  87. * @param {Function} callback
  88. */
  89. this.logIn = function (credentials, callback) {
  90. if ( callback ) {
  91. authenticationAgent.setCredentials(credentials);
  92. } else {
  93. callback = credentials;
  94. }
  95. authenticationAgent.logIn(callback);
  96. };
  97.  
  98. /**
  99. * Logs out the current user.
  100. *
  101. * @method logOut
  102. * @param {Function} callback
  103. */
  104. this.logOut = function (callback) {
  105. authenticationAgent.logOut(callback);
  106. };
  107.  
  108. /**
  109. * Get instance of Content Service. Use ContentService to retrieve information and execute operations related to Content.
  110. *
  111. * @method getContentService
  112. * @return {ContentService}
  113. * @example
  114. * var contentService = jsCAPI.getContentService();
  115. * contentService.loadRoot(
  116. * callback
  117. * );
  118. */
  119. this.getContentService = function () {
  120. if (!contentService) {
  121. contentService = new ContentService(
  122. connectionManager,
  123. discoveryService,
  124. mergedOptions.rootPath
  125. );
  126. }
  127. return contentService;
  128. };
  129.  
  130. /**
  131. * Get instance of Content Type Service. Use ContentTypeService to retrieve information and execute operations related to ContentTypes.
  132. *
  133. * @method getContentTypeService
  134. * @return {ContentTypeService}
  135. * @example
  136. * var contentTypeService = jsCAPI.getContentTypeService();
  137. * contentTypeService.loadContentType(
  138. * '/api/ezp/v2/content/types/18',
  139. * callback
  140. * );
  141. */
  142. this.getContentTypeService = function () {
  143. if (!contentTypeService) {
  144. contentTypeService = new ContentTypeService(
  145. connectionManager,
  146. discoveryService
  147. );
  148. }
  149. return contentTypeService;
  150. };
  151.  
  152. /**
  153. * Get instance of User Service. Use UserService to retrieve information and execute operations related to Users.
  154. *
  155. * @method getUserService
  156. * @return {UserService}
  157. * @example
  158. * var userService = jsCAPI.getUserService();
  159. * userService.loadRootUserGroup(
  160. * callback
  161. * );
  162. */
  163. this.getUserService = function () {
  164. if (!userService) {
  165. userService = new UserService(
  166. connectionManager,
  167. discoveryService,
  168. mergedOptions.rootPath
  169. );
  170. }
  171. return userService;
  172. };
  173.  
  174. /**
  175. * Get instance of Discovery Service. Use DiscoveryService to internally to discover
  176. * resources URI and media type provided in the root resource.
  177. *
  178. * @method getDiscoveryService
  179. * @return {DiscoveryService}
  180. * @example
  181. * var discoveryService = jsCAPI.getDiscoveryService();
  182. * discoveryService.getInfoObject(
  183. * "Trash",
  184. * callback
  185. * );
  186. */
  187. this.getDiscoveryService = function () {
  188. return discoveryService;
  189. };
  190.  
  191. /**
  192. * Gets the connection manager
  193. *
  194. * @method getConnectionManager
  195. * @return {ConnectionManager}
  196. * @example
  197. * var connectionManager = jsCAPI.getConnectionManager();
  198. * connectionManager.request(
  199. * "GET",
  200. * "/endpoint",
  201. * "",
  202. * {"Accept": "application/json"},
  203. * callback
  204. * );
  205. */
  206. this.getConnectionManager = function () {
  207. return connectionManager;
  208. };
  209. };
  210.  
  211. return CAPI;
  212. });
  213.