11package com .annimon .ownlang .modules .http ;
22
3- import com .annimon .ownlang .exceptions .ArgumentsMismatchException ;
43import com .annimon .ownlang .lib .*;
54import java .io .IOException ;
65import java .io .UnsupportedEncodingException ;
1413import okhttp3 .Response ;
1514import okhttp3 .internal .http .HttpMethod ;
1615
17- public final class http_http implements Function {
16+ public final class HttpFunctions {
1817
1918 private static final Value
2019 HEADER_KEY = new StringValue ("header" ),
@@ -27,78 +26,94 @@ public final class http_http implements Function {
2726
2827 private final OkHttpClient client = new OkHttpClient ();
2928
30- @ Override
31- public Value execute (Value [] args ) {
32- String url , method ;
33- Function function ;
29+ public Value httpSync (Value [] args ) {
30+ Arguments .checkRange (1 , 4 , args .length );
31+
32+ String url = args [0 ].asString ();
33+ String method = (args .length >= 2 ) ? args [1 ].asString () : "GET" ;
34+ Value requestParams = (args .length >= 3 ) ? args [2 ] : MapValue .EMPTY ;
35+ MapValue options = (args .length >= 4 ) ? ValueUtils .consumeMap (args [3 ], 3 ) : MapValue .EMPTY ;
36+
37+ boolean isSuccessful ;
38+ Value result = options .containsKey (EXTENDED_RESULT ) ? MapValue .EMPTY : StringValue .EMPTY ;
39+ try (Response response = executeRequest (url , method , requestParams , options )) {
40+ isSuccessful = response .isSuccessful ();
41+ if (isSuccessful ) {
42+ result = getResult (response , options );
43+ }
44+ } catch (IOException ioe ) {
45+ isSuccessful = false ;
46+ }
47+ return new ArrayValue (new Value [] {
48+ NumberValue .fromBoolean (isSuccessful ),
49+ result
50+ });
51+ }
52+
53+ public Value http (Value [] args ) {
54+ Arguments .checkRange (1 , 5 , args .length );
55+
56+ String url = args [0 ].asString ();
57+ String method = "GET" ;
58+ Value requestParams = MapValue .EMPTY ;
59+ MapValue options = MapValue .EMPTY ;
60+ Function callback = FunctionValue .EMPTY .getValue ();
61+
3462 switch (args .length ) {
3563 case 1 : // http(url)
36- url = args [0 ].asString ();
37- return process (url , "GET" );
64+ break ;
3865
3966 case 2 : // http(url, method) || http(url, callback)
40- url = args [0 ].asString ();
4167 if (args [1 ].type () == Types .FUNCTION ) {
42- return process (url , "GET" , ValueUtils .consumeFunction (args [1 ], 1 ));
68+ callback = ValueUtils .consumeFunction (args [1 ], 1 );
69+ } else {
70+ method = args [1 ].asString ();
4371 }
44- return process ( url , args [ 1 ]. asString ()) ;
72+ break ;
4573
4674 case 3 : // http(url, method, params) || http(url, method, callback)
47- url = args [0 ].asString ();
4875 method = args [1 ].asString ();
4976 if (args [2 ].type () == Types .FUNCTION ) {
50- return process (url , method , ValueUtils .consumeFunction (args [2 ], 2 ));
77+ callback = ValueUtils .consumeFunction (args [2 ], 2 );
78+ } else {
79+ requestParams = args [2 ];
5180 }
52- return process ( url , method , args [ 2 ], FunctionValue . EMPTY . getValue ()) ;
81+ break ;
5382
5483 case 4 : // http(url, method, params, callback)
55- url = args [0 ].asString ();
5684 method = args [1 ].asString ();
57- function = ValueUtils .consumeFunction (args [3 ], 3 );
58- return process (url , method , args [2 ], function );
85+ requestParams = args [2 ];
86+ callback = ValueUtils .consumeFunction (args [3 ], 3 );
87+ break ;
5988
6089 case 5 : // http(url, method, params, headerParams, callback)
61- url = args [0 ].asString ();
6290 method = args [1 ].asString ();
63- MapValue options = ValueUtils .consumeMap (args [3 ], 3 );
64- function = ValueUtils .consumeFunction (args [4 ], 4 );
65- return process (url , method , args [2 ], options , function );
66-
67- default :
68- throw new ArgumentsMismatchException ("From 1 to 5 arguments expected, got " + args .length );
91+ requestParams = args [2 ];
92+ options = ValueUtils .consumeMap (args [3 ], 3 );
93+ callback = ValueUtils .consumeFunction (args [4 ], 4 );
94+ break ;
6995 }
70- }
71-
72- private Value process (String url , String method ) {
73- return process (url , method , FunctionValue .EMPTY .getValue ());
74- }
75-
76- private Value process (String url , String method , Function callback ) {
77- return process (url , method , MapValue .EMPTY , callback );
78- }
7996
80- private Value process (String url , String method , Value params , Function callback ) {
81- return process (url , method , params , MapValue .EMPTY , callback );
82- }
83-
84- private Value process (String url , String methodStr , Value requestParams , MapValue options , Function callback ) {
85- final String method = methodStr .toUpperCase ();
8697 try {
87- final Request .Builder builder = new Request .Builder ()
88- .url (url )
89- .method (method , getRequestBody (method , requestParams , options ));
90- if (options .containsKey (HEADER_KEY )) {
91- applyHeaderParams ((MapValue ) options .get (HEADER_KEY ), builder );
92- }
93-
94- final Response response = client .newCall (builder .build ()).execute ();
98+ final Response response = executeRequest (url , method , requestParams , options );
9599 callback .execute (getResult (response , options ));
96100 return NumberValue .fromBoolean (response .isSuccessful ());
97101 } catch (IOException ex ) {
98102 return NumberValue .fromBoolean (false );
99103 }
100104 }
101105
106+ private Response executeRequest (String url , String methodStr , Value requestParams , MapValue options ) throws IOException {
107+ final String method = methodStr .toUpperCase ();
108+ final Request .Builder builder = new Request .Builder ()
109+ .url (url )
110+ .method (method , getRequestBody (method , requestParams , options ));
111+ if (options .containsKey (HEADER_KEY )) {
112+ applyHeaderParams ((MapValue ) options .get (HEADER_KEY ), builder );
113+ }
114+ return client .newCall (builder .build ()).execute ();
115+ }
116+
102117 private Value getResult (Response response , MapValue options ) throws IOException {
103118 if (options .containsKey (EXTENDED_RESULT )) {
104119 final MapValue map = new MapValue (10 );
0 commit comments