mirror of
https://github.com/opf/openproject.git
synced 2026-06-14 03:30:14 +00:00
Remove HALAPIResource
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
// -- copyright
|
||||
// OpenProject is a project management system.
|
||||
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License version 3.
|
||||
//
|
||||
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||||
// Copyright (C) 2006-2013 Jean-Philippe Lang
|
||||
// Copyright (C) 2010-2013 the ChiliProject Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
// See doc/COPYRIGHT.rdoc for more details.
|
||||
// ++
|
||||
|
||||
/* globals Hyperagent */
|
||||
require('hyperagent');
|
||||
|
||||
angular.module('openproject.api')
|
||||
.run(run)
|
||||
.factory('HALAPIResource', HALAPIResource);
|
||||
|
||||
function run($http, $q) {
|
||||
Hyperagent.configure('ajax', function(settings) {
|
||||
settings.transformResponse = function (data) { return data; };
|
||||
|
||||
return $http(settings).then(
|
||||
function (response) { settings.success(response.data); },
|
||||
settings.error
|
||||
);
|
||||
});
|
||||
Hyperagent.configure('defer', $q.defer);
|
||||
Hyperagent.configure('_', _);
|
||||
}
|
||||
|
||||
function HALAPIResource () {
|
||||
return {
|
||||
setup: function(uri, params) {
|
||||
params = params || {};
|
||||
var link = new Hyperagent.Resource(_.extend({ url: uri }, params));
|
||||
|
||||
if (params.method) {
|
||||
link.props.href = uri;
|
||||
link.props.method = params.method;
|
||||
}
|
||||
|
||||
return link;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
// -- copyright
|
||||
// OpenProject is a project management system.
|
||||
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License version 3.
|
||||
//
|
||||
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||||
// Copyright (C) 2006-2013 Jean-Philippe Lang
|
||||
// Copyright (C) 2010-2013 the ChiliProject Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
// See doc/COPYRIGHT.rdoc for more details.
|
||||
// ++
|
||||
|
||||
describe('HALAPIResource', function() {
|
||||
|
||||
var HALAPIResource;
|
||||
beforeEach(angular.mock.module('openproject.api'));
|
||||
beforeEach(angular.mock.module('openproject.services'));
|
||||
|
||||
beforeEach(inject(function(_HALAPIResource_) {
|
||||
HALAPIResource = _HALAPIResource_;
|
||||
}));
|
||||
|
||||
describe('setup', function() {
|
||||
var apiResource, resourceFunction;
|
||||
var workPackageUri = 'api/v3/work_packages/1';
|
||||
|
||||
beforeEach(inject(function($q) {
|
||||
apiResource = {
|
||||
fetch: $q.when(function() { return { id: workPackageId }; })
|
||||
};
|
||||
}));
|
||||
|
||||
beforeEach(inject(function(HALAPIResource) {
|
||||
resourceFunction = sinon.stub(Hyperagent, 'Resource').returns(apiResource);
|
||||
HALAPIResource.setup(workPackageUri);
|
||||
}));
|
||||
|
||||
afterEach(function() {
|
||||
resourceFunction.restore();
|
||||
});
|
||||
|
||||
it('makes an api setup call', function() {
|
||||
expect(resourceFunction).to.have.been.calledWith({
|
||||
url: workPackageUri
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -28,7 +28,6 @@
|
||||
/* globals URI */
|
||||
|
||||
module.exports = function(
|
||||
HALAPIResource,
|
||||
$http,
|
||||
I18n,
|
||||
NotificationsService
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
|
||||
angular.module('openproject.services')
|
||||
.service('ActivityService', [
|
||||
'HALAPIResource',
|
||||
'$http',
|
||||
'I18n',
|
||||
'NotificationsService',
|
||||
@@ -60,7 +59,6 @@ angular.module('openproject.services')
|
||||
'./timezone-service')])
|
||||
.service('TypeService', ['$http', 'PathHelper', require('./type-service')])
|
||||
.service('UserService', [
|
||||
'HALAPIResource',
|
||||
'$http',
|
||||
'PathHelper',
|
||||
'CacheService',
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
//++
|
||||
|
||||
module.exports = function(
|
||||
HALAPIResource,
|
||||
$http,
|
||||
PathHelper,
|
||||
CacheService) {
|
||||
|
||||
@@ -66,26 +66,3 @@ ProjectsService.getProject('project_identifier').then(function(project) {
|
||||
|
||||
This is in principle a very good concept to delegate responsibility of inference to the client and absolves the client of having to know each path in the application in advance.
|
||||
|
||||
## Using hyperagent.js
|
||||
|
||||
For all practical purposes, the OpenProject frontend uses a fork of [`hyperagent.js`](https://github.com/weluse/hyperagent) (actually [this one is used](https://github.com/manwithtwowatches/hyperagent)).
|
||||
|
||||
`hyperagent.js` aims to provide an interface to a structed JSON response, providing a resource object automatically. While this is a nice goal, the current implementation used is not complete.
|
||||
|
||||
The library has been wrapped as a service in `./frontend/app/api/hal-api-resource.js` and can be injected when needed.
|
||||
|
||||
What the service actually does is making resource objects out out of certain API responses (`v3` only) and providing `LazyResource`s to attached links. This is also the difference to using `_links` and `links` as a property sometimes:
|
||||
|
||||
```javascript
|
||||
//@see ./frontend/app/work_packages/services/work-package-attachments-service.js
|
||||
|
||||
// `workPackage` Hyperagent resource
|
||||
var addAttachmentPath = workPackage.links.addAttachment.url();
|
||||
|
||||
// `workPackage` is an API response
|
||||
var addAttachmentPath = workPackage._links.addAttachment.href;
|
||||
```
|
||||
|
||||
One of the minor drawbacks of `hyperagent.js` is that it (_read_: the fork used) only supports `GET` requests at the moment and one has to awkwardly inject the `method` desired into the `options` of the AJAX call made (see also the `setup` method of `hal-api-resource.js`, as well as an example in `loadWorkPackageForm` in `./frontend/app/services/work-package-service.js`).
|
||||
|
||||
__Note__: The long term goal should be to leverage `angular.$http` and make the calls accordingly. One of the short term goals should be to remove duplication introduced when building requests via the `HALAPIResource`.
|
||||
|
||||
Reference in New Issue
Block a user