1
1
package com .annimon .ownlang .modules .http ;
2
2
3
- import com .annimon .ownlang .exceptions .ArgumentsMismatchException ;
4
3
import com .annimon .ownlang .lib .*;
5
4
import java .io .IOException ;
6
5
import java .io .UnsupportedEncodingException ;
14
13
import okhttp3 .Response ;
15
14
import okhttp3 .internal .http .HttpMethod ;
16
15
17
- public final class http_http implements Function {
16
+ public final class HttpFunctions {
18
17
19
18
private static final Value
20
19
HEADER_KEY = new StringValue ("header" ),
@@ -27,78 +26,94 @@ public final class http_http implements Function {
27
26
28
27
private final OkHttpClient client = new OkHttpClient ();
29
28
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
+
34
62
switch (args .length ) {
35
63
case 1 : // http(url)
36
- url = args [0 ].asString ();
37
- return process (url , "GET" );
64
+ break ;
38
65
39
66
case 2 : // http(url, method) || http(url, callback)
40
- url = args [0 ].asString ();
41
67
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 ();
43
71
}
44
- return process ( url , args [ 1 ]. asString ()) ;
72
+ break ;
45
73
46
74
case 3 : // http(url, method, params) || http(url, method, callback)
47
- url = args [0 ].asString ();
48
75
method = args [1 ].asString ();
49
76
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 ];
51
80
}
52
- return process ( url , method , args [ 2 ], FunctionValue . EMPTY . getValue ()) ;
81
+ break ;
53
82
54
83
case 4 : // http(url, method, params, callback)
55
- url = args [0 ].asString ();
56
84
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 ;
59
88
60
89
case 5 : // http(url, method, params, headerParams, callback)
61
- url = args [0 ].asString ();
62
90
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 ;
69
95
}
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
- }
79
96
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 ();
86
97
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 );
95
99
callback .execute (getResult (response , options ));
96
100
return NumberValue .fromBoolean (response .isSuccessful ());
97
101
} catch (IOException ex ) {
98
102
return NumberValue .fromBoolean (false );
99
103
}
100
104
}
101
105
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
+
102
117
private Value getResult (Response response , MapValue options ) throws IOException {
103
118
if (options .containsKey (EXTENDED_RESULT )) {
104
119
final MapValue map = new MapValue (10 );
0 commit comments