blob: 04b9fe02cb395c9581b344346ec0f33585b193b1 [file] [log] [blame]
Simon Hunte6720442015-01-15 12:21:06 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Remote Communications Module -- REST Service
Simon Hunte6720442015-01-15 12:21:06 -080019 */
20(function () {
21 'use strict';
22
23 var $log;
24
25 angular.module('onosRemote')
Simon Huntbc76fb12015-02-26 18:09:54 -080026 .factory('RestService',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070027 ['$log', '$http', 'FnService', 'UrlFnService',
Simon Huntbc76fb12015-02-26 18:09:54 -080028
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070029 function (_$log_, $http, fs, ufs) {
Simon Hunte6720442015-01-15 12:21:06 -080030 $log = _$log_;
31
Simon Huntbc76fb12015-02-26 18:09:54 -080032 function get(url, callback, errorCb) {
Simon Hunt1e4a0012015-01-21 11:36:08 -080033 var fullUrl = ufs.rsUrl(url);
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080034
35 $http.get(fullUrl).then(function (response) {
Simon Hunte6720442015-01-15 12:21:06 -080036 // success
37 callback(response.data);
38 }, function (response) {
39 // error
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070040 var emsg = 'Failed to retrieve JSON data: ' + fullUrl;
Simon Huntbc76fb12015-02-26 18:09:54 -080041 $log.warn(emsg, response.status, response.data);
42 if (errorCb) {
43 errorCb(emsg);
44 }
Simon Hunte6720442015-01-15 12:21:06 -080045 });
46 }
47
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070048 // TODO: test this
49 function post(url, data, callbacks) {
50 var fullUrl = ufs.rsUrl(url);
51 $http.post(fullUrl, data).then(function (response) {
52 // success
53 if (callbacks && fs.isF(callbacks.success)) {
54 callbacks.success(response.data);
55 }
56 }, function (response) {
57 // error
58 var msg = 'Problem with $http post request: ' + fullUrl;
59 $log.warn(msg, response.status, response.data);
60
61 if (callbacks && fs.isF(callbacks.error)) {
62 callbacks.error(msg);
63 }
64 });
65 }
66
Simon Hunte6720442015-01-15 12:21:06 -080067 return {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070068 get: get,
69 post: post
Simon Hunte6720442015-01-15 12:21:06 -080070 };
71 }]);
Simon Hunte6720442015-01-15 12:21:06 -080072}());