|
1 | | -import * as Https from 'nativescript-https'; |
2 | | -import * as Observable from 'tns-core-modules/data/observable'; |
3 | | -import * as fs from 'tns-core-modules/file-system'; |
4 | | -import * as dialogs from 'tns-core-modules/ui/dialogs'; |
5 | | -import * as Page from 'tns-core-modules/ui/page'; |
| 1 | +import * as Https from "nativescript-https"; |
| 2 | +import * as Observable from "tns-core-modules/data/observable"; |
| 3 | +import * as fs from "tns-core-modules/file-system"; |
| 4 | +import * as dialogs from "tns-core-modules/ui/dialogs"; |
| 5 | +import * as Page from "tns-core-modules/ui/page"; |
6 | 6 |
|
7 | | -export function onNavigatingTo(args: Page.NavigatedData) { |
8 | | - let page = args.object as Page.Page; |
9 | | - page.bindingContext = Observable.fromObject({enabled: false}); |
| 7 | +let page; |
| 8 | +let viewModel; |
| 9 | +export function pageLoaded(args: Page.NavigatedData) { |
| 10 | + page = args.object as Page.Page; |
| 11 | + viewModel = Observable.fromObject({ |
| 12 | + enabled: false, |
| 13 | + progress: 0, |
| 14 | + currentRequest: null, |
| 15 | + }); |
| 16 | + page.bindingContext = viewModel; |
10 | 17 | } |
11 | 18 |
|
12 | | -function getRequest(url: string, allowLargeResponse = false) { |
13 | | - Https.request( |
14 | | - { |
| 19 | +function createRequest( |
| 20 | + url: string, |
| 21 | + options?: Partial<Https.HttpsRequestOptions> |
| 22 | +) { |
| 23 | + return Https.createRequest({ |
| 24 | + useLegacy: true, |
15 | 25 | url, |
16 | | - method: 'GET', |
| 26 | + method: "GET", |
17 | 27 | timeout: 1, |
18 | | - allowLargeResponse |
19 | | - }) |
20 | | - .then(response => console.log('Https.request response', response)) |
21 | | - .catch(error => { |
22 | | - console.error('Https.request error', error); |
23 | | - dialogs.alert(error); |
24 | | - }); |
| 28 | + ...options, |
| 29 | + }); |
25 | 30 | } |
26 | 31 |
|
27 | | -function postRequest(url: string, body: any) { |
28 | | - Https.request( |
29 | | - { |
| 32 | +function onError(error) { |
| 33 | + console.error("Https.request error", error, error.stack); |
| 34 | + dialogs.alert(error.toString()); |
| 35 | + page.bindingContext.currentRequest = null; |
| 36 | + page.bindingContext.progress = 0; |
| 37 | + return Promise.reject(error); |
| 38 | +} |
| 39 | +function getRequest(url: string, options?: Partial<Https.HttpsRequestOptions>) { |
| 40 | + return Https.request({ |
| 41 | + useLegacy: true, |
30 | 42 | url, |
31 | | - method: 'POST', |
32 | | - body |
33 | | - }) |
34 | | - .then(response => console.log('Https.request response', response)) |
35 | | - .catch(error => { |
36 | | - console.error('Https.request error', error); |
37 | | - dialogs.alert(error); |
38 | | - }); |
| 43 | + method: "GET", |
| 44 | + timeout: 1, |
| 45 | + ...options, |
| 46 | + }) |
| 47 | + .then((response) => { |
| 48 | + page.bindingContext.currentRequest = null; |
| 49 | + page.bindingContext.progress = 0; |
| 50 | + console.log("Https.request response", response); |
| 51 | + return response; |
| 52 | + }) |
| 53 | + .catch(onError); |
| 54 | +} |
| 55 | + |
| 56 | +function postRequest( |
| 57 | + url: string, |
| 58 | + options?: Partial<Https.HttpsRequestOptions> |
| 59 | +) { |
| 60 | + return Https.request({ |
| 61 | + useLegacy: true, |
| 62 | + url, |
| 63 | + method: "POST", |
| 64 | + ...options, |
| 65 | + }) |
| 66 | + .then((response) => console.log("Https.request response", response)) |
| 67 | + .catch((error) => { |
| 68 | + console.error("Https.request error", error); |
| 69 | + dialogs.alert(error); |
| 70 | + }); |
39 | 71 | } |
40 | 72 |
|
41 | 73 | export function postHttpbin() { |
42 | | - postRequest('https://httpbin.org/post', {"foo": "bar", "baz": undefined, "plaz": null}); |
| 74 | + postRequest("https://httpbin.org/post", { |
| 75 | + body: { foo: "bar", baz: undefined, plaz: null }, |
| 76 | + }); |
43 | 77 | } |
44 | 78 |
|
45 | 79 | export function postHttpbinWithUTF8() { |
46 | | - Https.request( |
47 | | - { |
48 | | - url: 'https://httpbin.org/post', |
49 | | - method: 'POST', |
50 | | - body: {"foo": "bar", "baz": undefined, "plaz": null}, |
| 80 | + Https.request({ |
| 81 | + url: "https://httpbin.org/post", |
| 82 | + method: "POST", |
| 83 | + body: { foo: "bar", baz: undefined, plaz: null }, |
51 | 84 | headers: { |
52 | | - 'Content-Type': "application/json; charset=utf-8", |
53 | | - 'X-testing': "ok" |
54 | | - } |
55 | | - }) |
56 | | - .then(response => console.log('Https.request response', response)) |
57 | | - .catch(error => { |
58 | | - console.error('Https.request error', error); |
| 85 | + "Content-Type": "application/json; charset=utf-8", |
| 86 | + "X-testing": "ok", |
| 87 | + }, |
| 88 | + }).catch((error) => { |
| 89 | + console.error("Https.request error", error); |
59 | 90 | dialogs.alert(error); |
60 | | - }); |
| 91 | + }); |
61 | 92 | } |
62 | 93 |
|
63 | 94 | export function getHttpbin() { |
64 | | - getRequest('https://httpbin.org/get'); |
| 95 | + getRequest("https://httpbin.org/get"); |
| 96 | +} |
| 97 | + |
| 98 | +export function cancelCurrentRequest() { |
| 99 | + if (page.bindingContext.currentRequest) { |
| 100 | + page.bindingContext.currentRequest.cancel(); |
| 101 | + } |
| 102 | +} |
| 103 | +export function getBigFile() { |
| 104 | + const request = createRequest( |
| 105 | + "http://ipv4.download.thinkbroadband.com/200MB.zip", |
| 106 | + { |
| 107 | + onProgress: (current, total) => { |
| 108 | + page.bindingContext.progress = (current / total) * 100; |
| 109 | + }, |
| 110 | + } |
| 111 | + ); |
| 112 | + page.bindingContext.currentRequest = request; |
| 113 | + |
| 114 | + return new Promise<any>((resolve, reject) => { |
| 115 | + request.run(resolve, reject); |
| 116 | + }) |
| 117 | + .then((response) => { |
| 118 | + console.log("did get response"); |
| 119 | + let dir = fs.knownFolders.temp().getFile("200MB.zip"); |
| 120 | + return (response.content as Https.HttpsResponseLegacy).toFile( |
| 121 | + dir.path |
| 122 | + ); |
| 123 | + // console.log("did get response done"); |
| 124 | + }) |
| 125 | + .then(() => { |
| 126 | + page.bindingContext.currentPromise = null; |
| 127 | + console.log("did get response done"); |
| 128 | + }) |
| 129 | + .catch(onError); |
65 | 130 | } |
66 | 131 |
|
67 | 132 | export function getHttpbinLargeResponse() { |
68 | | - getRequest('https://httpbin.org/bytes/100000', true); |
| 133 | + getRequest("https://httpbin.org/bytes/100000"); |
69 | 134 | } |
70 | 135 |
|
71 | 136 | export function getMockbin() { |
72 | | - getRequest('https://mockbin.com/request'); |
| 137 | + getRequest("https://mockbin.com/request"); |
| 138 | +} |
| 139 | + |
| 140 | +export function get404() { |
| 141 | + getRequest("https://mockbin.com/reque2st"); |
73 | 142 | } |
74 | 143 |
|
75 | 144 | export function enableSSLPinning(args: Observable.EventData) { |
76 | | - let dir = fs.knownFolders.currentApp().getFolder('assets'); |
77 | | - let certificate = dir.getFile('httpbin.org.cer').path; |
78 | | - Https.enableSSLPinning({host: 'httpbin.org', commonName: "httpbin.org", certificate}); |
79 | | - console.log('enabled'); |
| 145 | + let dir = fs.knownFolders.currentApp().getFolder("assets"); |
| 146 | + let certificate = dir.getFile("httpbin.org.cer").path; |
| 147 | + Https.enableSSLPinning({ |
| 148 | + host: "httpbin.org", |
| 149 | + commonName: "httpbin.org", |
| 150 | + certificate, |
| 151 | + }); |
| 152 | + console.log("enabled"); |
80 | 153 | } |
81 | 154 |
|
82 | 155 | export function enableSSLPinningExpired(args: Observable.EventData) { |
83 | | - let dir = fs.knownFolders.currentApp().getFolder('assets'); |
84 | | - let certificate = dir.getFile('httpbin.org.expired.cer').path; |
85 | | - Https.enableSSLPinning({host: 'httpbin.org', certificate}); |
86 | | - console.log('enabled'); |
| 156 | + let dir = fs.knownFolders.currentApp().getFolder("assets"); |
| 157 | + let certificate = dir.getFile("httpbin.org.expired.cer").path; |
| 158 | + Https.enableSSLPinning({ host: "httpbin.org", certificate }); |
| 159 | + console.log("enabled"); |
87 | 160 | } |
88 | 161 |
|
89 | 162 | export function disableSSLPinning(args: Observable.EventData) { |
90 | | - Https.disableSSLPinning(); |
91 | | - console.log('disabled'); |
| 163 | + Https.disableSSLPinning(); |
| 164 | + console.log("disabled"); |
92 | 165 | } |
93 | | - |
94 | | - |
|
0 commit comments