@@ -37,6 +37,24 @@ export function clearCache() {
3737
3838let _timeout = 10 ;
3939
40+ class Callback extends com . nativescript . https . OkhttpCallback {
41+ resolve
42+ reject
43+ onStringResponse ( content , statusCode , heads ) {
44+ let headers = { } ;
45+ // let heads: okhttp3.Headers = resp.headers();
46+ let i : number , len : number = heads . size ( ) ;
47+ for ( i = 0 ; i < len ; i ++ ) {
48+ let key = heads . name ( i ) ;
49+ headers [ key ] = heads . value ( i ) ;
50+ }
51+ this . resolve ( { content, statusCode, headers} ) ;
52+ }
53+ onFailure ( task , error ) {
54+ this . reject ( error ) ;
55+ }
56+ }
57+
4058export function enableSSLPinning ( options : Https . HttpsSSLPinningOptions ) {
4159 // console.log('options', options)
4260 if ( ! peer . host && ! peer . certificate ) {
@@ -185,12 +203,10 @@ function getClient(reload: boolean = false, timeout: number = 10): okhttp3.OkHtt
185203 Client = client . build ( ) ;
186204 return Client ;
187205}
188-
189206export function request ( opts : Https . HttpsRequestOptions ) : Promise < Https . HttpsResponse > {
190207 return new Promise ( ( resolve , reject ) => {
191208 try {
192209 let client = getClient ( false , opts . timeout ) ;
193-
194210 let request = new okhttp3 . Request . Builder ( ) ;
195211 request . url ( opts . url ) ;
196212
@@ -222,20 +238,25 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
222238 'PUT' : 'put' ,
223239 'PATCH' : 'patch'
224240 } ;
225-
226- if ( ( [ 'GET' , 'HEAD' ] . indexOf ( opts . method ) !== - 1 ) || ( opts . method === 'DELETE' && ! isDefined ( opts . body ) ) ) {
241+ let type
242+ if ( ( [ 'GET' , 'HEAD' ] . indexOf ( opts . method ) !== - 1 ) || ( opts . method === 'DELETE' && ! isDefined ( opts . body ) && ! isDefined ( opts . content ) ) ) {
227243 request [ methods [ opts . method ] ] ( ) ;
228244 } else {
229- let type = opts . headers && opts . headers [ 'Content-Type' ] ? < string > opts . headers [ 'Content-Type' ] : 'application/json' ;
230- let body = < any > opts . body || { } ;
231- try {
232- body = JSON . stringify ( body ) ;
233- } catch ( ignore ) {
245+ type = opts . headers && opts . headers [ 'Content-Type' ] ? < string > opts . headers [ 'Content-Type' ] : 'application/json' ;
246+
247+ let body ;
248+ if ( opts . body ) {
249+ try {
250+ body = JSON . stringify ( opts . body ) ;
251+ } catch ( ignore ) {
252+ }
253+ } else if ( opts . content ) {
254+ body = opts . content
234255 }
235256 request [ methods [ opts . method ] ] ( okhttp3 . RequestBody . create (
236- okhttp3 . MediaType . parse ( type ) ,
237- body
238- ) ) ;
257+ okhttp3 . MediaType . parse ( type ) ,
258+ body
259+ ) ) ;
239260 }
240261
241262 // We have to allow networking on the main thread because larger responses will crash the app with an NetworkOnMainThreadException.
@@ -244,53 +265,10 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
244265 if ( opts . allowLargeResponse ) {
245266 android . os . StrictMode . setThreadPolicy ( android . os . StrictMode . ThreadPolicy . LAX ) ;
246267 }
247-
248- client . newCall ( request . build ( ) ) . enqueue ( new okhttp3 . Callback ( {
249- onResponse : ( task , response ) => {
250- // console.log('onResponse')
251- // console.keys('response', response)
252- // console.log('onResponse > response.isSuccessful()', response.isSuccessful())
253-
254- // let body = response.body()//.bytes()
255- // console.keys('body', body)
256- // console.log('body.contentType()', body.contentType())
257- // console.log('body.contentType().toString()', body.contentType().toString())
258- // console.log('body.bytes()', body.bytes())
259- // console.dump('wtf', wtf)
260- // console.log('opts.url', opts.url)
261- // console.log('body.string()', body.string())
262-
263- // let content: any = response.body().string()
264- // console.log('content', content)
265- // try {
266- // content = JSON.parse(response.body().string())
267- // } catch (error) {
268- // return reject(error)
269- // }
270-
271- let content = response . body ( ) . string ( ) ;
272- try {
273- content = JSON . parse ( content ) ;
274- } catch ( e ) {
275- }
276-
277- let statusCode = response . code ( ) ;
278-
279- let headers = { } ;
280- let heads : okhttp3 . Headers = response . headers ( ) ;
281- let i : number , len : number = heads . size ( ) ;
282- for ( i = 0 ; i < len ; i ++ ) {
283- let key = heads . name ( i ) ;
284- headers [ key ] = heads . value ( i ) ;
285- }
286-
287- resolve ( { content, statusCode, headers} ) ;
288- } ,
289- onFailure : ( task , error ) => {
290- reject ( error ) ;
291- } ,
292- } ) ) ;
293-
268+ const callback = new Callback ( ) ;
269+ callback . resolve = resolve ;
270+ callback . reject = reject ;
271+ client . newCall ( request . build ( ) ) . enqueue ( callback ) ;
294272 } catch ( error ) {
295273 reject ( error ) ;
296274 }
0 commit comments