Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 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 Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
| 23 | var $log; |
| 24 | |
| 25 | angular.module('onosRemote') |
Simon Hunt | bc76fb1 | 2015-02-26 18:09:54 -0800 | [diff] [blame] | 26 | .factory('RestService', |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 27 | ['$log', '$http', 'FnService', 'UrlFnService', |
Simon Hunt | bc76fb1 | 2015-02-26 18:09:54 -0800 | [diff] [blame] | 28 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 29 | function (_$log_, $http, fs, ufs) { |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 30 | $log = _$log_; |
| 31 | |
Simon Hunt | bc76fb1 | 2015-02-26 18:09:54 -0800 | [diff] [blame] | 32 | function get(url, callback, errorCb) { |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 33 | var fullUrl = ufs.rsUrl(url); |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 34 | |
| 35 | $http.get(fullUrl).then(function (response) { |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 36 | // success |
| 37 | callback(response.data); |
| 38 | }, function (response) { |
| 39 | // error |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 40 | var emsg = 'Failed to retrieve JSON data: ' + fullUrl; |
Simon Hunt | bc76fb1 | 2015-02-26 18:09:54 -0800 | [diff] [blame] | 41 | $log.warn(emsg, response.status, response.data); |
| 42 | if (errorCb) { |
| 43 | errorCb(emsg); |
| 44 | } |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 45 | }); |
| 46 | } |
| 47 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 48 | // 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 Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 67 | return { |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 68 | get: get, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 69 | post: post, |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 70 | }; |
| 71 | }]); |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 72 | }()); |