Commit 016bbc33677c58f58a98a50ff226a7a6fb0c0664
1 parent
90b17a3ef8
Exists in
master
interceptor
Showing
2 changed files
with
24 additions
and
19 deletions
Show diff stats
src/js/interceptor.js
1 | angular.module('focaConfiguracion') | 1 | angular.module('focaConfiguracion') |
2 | .factory("RequestHeadersInterceptor", [ '$injector', | 2 | .factory("interceptorRequest", [ '$injector', |
3 | function($injector) { | 3 | function($injector) { |
4 | 4 | ||
5 | function reintentarRequest(httpConfig) { | 5 | function reintentarRequest(httpConfig) { |
6 | 6 | ||
7 | var $timeout = $injector.get('$timeout'); | 7 | var $timeout = $injector.get('$timeout'); |
8 | 8 | ||
9 | $timeout(function() { | 9 | return $timeout(function() { |
10 | 10 | ||
11 | var $http = $injector.get('$http'); | 11 | var $http = $injector.get('$http'); |
12 | return $http(httpConfig.config); | 12 | return $http(httpConfig.config); |
13 | }, 5000); | 13 | }, 10000); |
14 | |||
15 | return getDataSqlite(httpConfig); | ||
16 | } | 14 | } |
17 | 15 | ||
18 | function updateDataSqlite(request) { | 16 | function updateDataSqlite(request) { |
19 | 17 | ||
20 | var sqliteService = $injector.get('focaSqliteService'); | 18 | var sqliteService = $injector.get('focaSqliteService'); |
21 | 19 | ||
22 | sqliteService.updateQuery( | 20 | sqliteService.updateQuery( |
23 | request.config.url, | 21 | request.config.url, |
24 | JSON.stringify(request.config.data), | 22 | JSON.stringify(request.config.data), |
25 | request.config.method, | 23 | request.config.method, |
26 | JSON.stringify(request.data)) | 24 | JSON.stringify(request.data)); |
27 | } | 25 | } |
28 | 26 | ||
29 | function getDataSqlite(request) { | 27 | function getDataSqlite(request) { |
28 | |||
30 | return new Promise(function(resolve) { | 29 | return new Promise(function(resolve) { |
31 | 30 | ||
32 | var sqliteService = $injector.get('focaSqliteService'); | 31 | var sqliteService = $injector.get('focaSqliteService'); |
33 | 32 | ||
34 | sqliteService.getQuery( | 33 | sqliteService.getQuery( |
35 | request.config.url, | 34 | request.config.url, |
36 | JSON.stringify(request.config.data), | 35 | JSON.stringify(request.config.data), |
37 | request.config.method, | 36 | request.config.method, |
38 | respuesta); | 37 | respuesta); |
39 | 38 | ||
40 | function respuesta(config) { | 39 | function respuesta(config) { |
40 | |||
41 | if (config) { | ||
41 | 42 | ||
42 | var request = { | 43 | var request = { |
43 | data: JSON.parse(config), | 44 | data: JSON.parse(config.response), |
44 | status: 200 | 45 | status: 200 |
45 | }; | 46 | }; |
46 | 47 | ||
47 | console.log(request); | 48 | resolve(request); |
48 | 49 | } else { | |
49 | if (config) { | 50 | resolve(false); |
50 | resolve(request); | ||
51 | } | ||
52 | } | 51 | } |
52 | } | ||
53 | }); | 53 | }); |
54 | } | 54 | } |
55 | 55 | ||
56 | var request = { | 56 | var request = { |
57 | responseError: async function(config) { | 57 | responseError: async function(config) { |
58 | 58 | ||
59 | if (config.status === -1) { | 59 | if (config.status === -1) { |
60 | 60 | ||
61 | var data = await reintentarRequest(config); | 61 | var data = await getDataSqlite(config); |
62 | |||
63 | config.data = data.data; | ||
64 | 62 | ||
65 | return config; | 63 | if (!data) { |
64 | return reintentarRequest(config); | ||
65 | } else { | ||
66 | config.data = data.data; | ||
67 | return config; | ||
68 | } | ||
66 | 69 | ||
67 | } else { | 70 | } else { |
68 | return config; | 71 | return config; |
69 | } | 72 | } |
70 | }, | 73 | }, |
71 | response: function(response) { | 74 | response: function(response) { |
72 | 75 | ||
73 | if (typeof response.data !== 'string') { | 76 | if (typeof response.data !== 'string') { |
74 | updateDataSqlite(response); | 77 | updateDataSqlite(response); |
75 | } | 78 | } |
76 | return response; | 79 | return response; |
77 | } | 80 | } |
78 | } | 81 | } |
79 | 82 |
src/js/service.js
1 | angular.module('appWrapperDemo') | 1 | angular.module('appWrapperDemo') |
2 | .factory('API_ENDPOINT', [ | 2 | .factory('API_ENDPOINT', [ |
3 | '$http', | 3 | '$http', |
4 | '$localStorage', | 4 | '$localStorage', |
5 | 'ENDPOINT_BASE', | 5 | 'ENDPOINT_BASE', |
6 | function($http, $localStorage, ENDPOINT_BASE) { | 6 | function($http, $localStorage, ENDPOINT_BASE) { |
7 | return { | 7 | return { |
8 | URL: 'http://debonline.dyndns.org:9900', | 8 | URL: 'http://10.231.45.189:9900', |
9 | ENDPOINT_BASE: ENDPOINT_BASE, | 9 | ENDPOINT_BASE: ENDPOINT_BASE, |
10 | setUrl: function(url) { | 10 | setUrl: function(url) { |
11 | this.URL = url; | 11 | this.URL = url; |
12 | $localStorage.urlEndPoint = url; | 12 | $localStorage.urlEndPoint = url; |
13 | }, | 13 | }, |
14 | getUrl: function() { | 14 | getUrl: function() { |
15 | return $http.get(this.ENDPOINT_BASE + '/terminal/consultar'); | 15 | return $http.get(this.ENDPOINT_BASE + '/terminal/consultar'); |
16 | } | 16 | } |
17 | }; | 17 | }; |
18 | }]); | 18 | }]); |
19 | 19 |