5
5
import com .annimon .ownlang .lib .*;
6
6
import java .io .IOException ;
7
7
import java .io .UnsupportedEncodingException ;
8
- import java .nio .charset .UnsupportedCharsetException ;
9
- import java .util .ArrayList ;
10
8
import java .util .List ;
11
9
import java .util .Map ;
12
- import org .apache .http .HttpEntity ;
13
- import org .apache .http .HttpResponse ;
14
- import org .apache .http .NameValuePair ;
15
- import org .apache .http .client .entity .UrlEncodedFormEntity ;
16
- import org .apache .http .client .methods .*;
17
- import org .apache .http .entity .StringEntity ;
18
- import org .apache .http .impl .client .BasicResponseHandler ;
19
- import org .apache .http .impl .client .CloseableHttpClient ;
20
- import org .apache .http .impl .client .HttpClientBuilder ;
21
- import org .apache .http .message .BasicNameValuePair ;
10
+ import okhttp3 .*;
11
+ import okhttp3 .internal .http .HttpMethod ;
22
12
23
13
public final class http_http implements Function {
24
14
25
15
private static final Value
26
16
HEADER_KEY = new StringValue ("header" ),
27
- CHARSET_KEY = new StringValue ("charset" );
17
+ CHARSET_KEY = new StringValue ("charset" ),
18
+ ENCODED_KEY = new StringValue ("encoded" ),
19
+ CONTENT_TYPE = new StringValue ("content_type" ),
20
+ EXTENDED_RESULT = new StringValue ("extended_result" );
21
+
22
+ private static final MediaType URLENCODED_MEDIA_TYPE = MediaType .parse ("application/x-www-form-urlencoded" );
23
+
24
+ private final OkHttpClient client = new OkHttpClient ();
28
25
29
26
@ Override
30
27
public Value execute (Value ... args ) {
@@ -85,91 +82,90 @@ private Value process(String url, String method, Value params, FunctionValue fun
85
82
return process (url , method , params , MapValue .EMPTY , function );
86
83
}
87
84
88
- private Value process (String url , String method , Value requestParams , MapValue options , FunctionValue function ) {
85
+ private Value process (String url , String methodStr , Value requestParams , MapValue options , FunctionValue function ) {
86
+ final String method = methodStr .toUpperCase ();
89
87
final Function callback = function .getValue ();
90
- try (CloseableHttpClient httpClient = HttpClientBuilder .create ().build ()) {
91
- HttpRequestBase httpMethod ;
92
- switch (method .toUpperCase ()) {
93
- case "POST" :
94
- httpMethod = new HttpPost (url );
95
- break ;
96
- case "PUT" :
97
- httpMethod = new HttpPut (url );
98
- break ;
99
- case "DELETE" :
100
- httpMethod = new HttpDelete (url );
101
- break ;
102
- case "PATCH" :
103
- httpMethod = new HttpPatch (url );
104
- break ;
105
- case "HEAD" :
106
- httpMethod = new HttpHead (url );
107
- break ;
108
- case "OPTIONS" :
109
- httpMethod = new HttpOptions (url );
110
- break ;
111
- case "TRACE" :
112
- httpMethod = new HttpTrace (url );
113
- break ;
114
- case "GET" :
115
- default :
116
- httpMethod = new HttpGet (url );
117
- break ;
118
- }
119
-
88
+ try {
89
+ final Request .Builder builder = new Request .Builder ()
90
+ .url (url )
91
+ .method (method , getRequestBody (method , requestParams , options ));
120
92
if (options .containsKey (HEADER_KEY )) {
121
- applyHeaderParams ((MapValue ) options .get (HEADER_KEY ), httpMethod );
93
+ applyHeaderParams ((MapValue ) options .get (HEADER_KEY ), builder );
122
94
}
123
95
124
- if (httpMethod instanceof HttpEntityEnclosingRequestBase ) {
125
- final HttpEntityEnclosingRequestBase heerb = (HttpEntityEnclosingRequestBase ) httpMethod ;
126
- if (requestParams .type () == Types .MAP ) {
127
- applyMapRequestParams (heerb , (MapValue ) requestParams , options );
128
- } else {
129
- applyStringRequestParams (heerb , requestParams , options );
130
- }
131
- }
132
-
133
- final HttpResponse httpResponse = httpClient .execute (httpMethod );
134
- final String response = new BasicResponseHandler ().handleResponse (httpResponse );
135
- callback .execute (new StringValue (response ));
136
- return NumberValue .fromBoolean (true );
96
+ final Response response = client .newCall (builder .build ()).execute ();
97
+ callback .execute (getResult (response , options ));
98
+ return NumberValue .fromBoolean (response .isSuccessful ());
137
99
} catch (IOException ex ) {
138
100
return NumberValue .fromBoolean (false );
139
101
}
140
102
}
103
+
104
+ private Value getResult (Response response , MapValue options ) throws IOException {
105
+ if (options .containsKey (EXTENDED_RESULT )) {
106
+ final MapValue map = new MapValue (10 );
107
+ map .set (new StringValue ("text" ), new StringValue (response .body ().string ()));
108
+ map .set (new StringValue ("message" ), new StringValue (response .message ()));
109
+ map .set (new StringValue ("code" ), new NumberValue (response .code ()));
110
+ final MapValue headers = new MapValue (response .headers ().size ());
111
+ for (Map .Entry <String , List <String >> entry : response .headers ().toMultimap ().entrySet ()) {
112
+ final int valuesSize = entry .getValue ().size ();
113
+ final ArrayValue values = new ArrayValue (valuesSize );
114
+ for (int i = 0 ; i < valuesSize ; i ++) {
115
+ values .set (i , new StringValue (entry .getValue ().get (i )));
116
+ }
117
+ headers .set (new StringValue (entry .getKey ()), values );
118
+ }
119
+ map .set (new StringValue ("headers" ), headers );
120
+ map .set (new StringValue ("content_length" ), new NumberValue (response .body ().contentLength ()));
121
+ map .set (CONTENT_TYPE , new StringValue (response .body ().contentType ().toString ()));
122
+ return map ;
123
+ }
124
+ return new StringValue (response .body ().string ());
125
+ }
141
126
142
- private void applyHeaderParams (MapValue headerParams , HttpRequestBase httpMethod ) {
127
+ private void applyHeaderParams (MapValue headerParams , Request . Builder builder ) {
143
128
for (Map .Entry <Value , Value > p : headerParams ) {
144
- httpMethod .addHeader (p .getKey ().asString (), p .getValue ().asString ());
129
+ builder .header (p .getKey ().asString (), p .getValue ().asString ());
130
+ }
131
+ }
132
+
133
+ private RequestBody getRequestBody (String method , Value params , MapValue options ) throws UnsupportedEncodingException {
134
+ if (!HttpMethod .permitsRequestBody (method )) return null ;
135
+
136
+ if (params .type () == Types .MAP ) {
137
+ return getMapRequestBody ((MapValue ) params , options );
145
138
}
139
+ return getStringRequestBody (params , options );
146
140
}
147
141
148
- private void applyMapRequestParams ( HttpEntityEnclosingRequestBase h , MapValue params , MapValue options )
149
- throws UnsupportedEncodingException {
150
- final List < NameValuePair > entityParams = new ArrayList <>( params . size () );
142
+ private RequestBody getMapRequestBody ( MapValue params , MapValue options ) throws UnsupportedEncodingException {
143
+ final FormBody . Builder form = new FormBody . Builder ();
144
+ final boolean alreadyEncoded = ( options . containsKey ( ENCODED_KEY ) && options . get ( ENCODED_KEY ). asNumber () != 0 );
151
145
for (Map .Entry <Value , Value > param : params ) {
152
146
final String name = param .getKey ().asString ();
153
147
final String value = param .getValue ().asString ();
154
- entityParams .add (new BasicNameValuePair (name , value ));
148
+ if (alreadyEncoded )
149
+ form .addEncoded (name , value );
150
+ else
151
+ form .add (name , value );
155
152
}
156
- HttpEntity entity ;
157
- if (options .containsKey (CHARSET_KEY )) {
158
- entity = new UrlEncodedFormEntity (entityParams , options .get (CHARSET_KEY ).asString ());
159
- } else {
160
- entity = new UrlEncodedFormEntity (entityParams );
161
- }
162
- h .setEntity (entity );
153
+ return form .build ();
163
154
}
164
155
165
- private void applyStringRequestParams ( final HttpEntityEnclosingRequestBase heerb , Value requestParams , MapValue options ) throws UnsupportedEncodingException , UnsupportedCharsetException {
166
- HttpEntity entity ;
167
- if (options .containsKey (CHARSET_KEY )) {
168
- entity = new StringEntity ( requestParams . asString (), options .get (CHARSET_KEY ).asString ());
156
+ private RequestBody getStringRequestBody ( Value params , MapValue options ) throws UnsupportedEncodingException {
157
+ final MediaType type ;
158
+ if (options .containsKey (CONTENT_TYPE )) {
159
+ type = MediaType . parse ( options .get (CONTENT_TYPE ).asString ());
169
160
} else {
170
- entity = new StringEntity ( requestParams . asString ()) ;
161
+ type = URLENCODED_MEDIA_TYPE ;
171
162
}
172
- heerb .setEntity (entity );
163
+
164
+ if (options .containsKey (CHARSET_KEY )) {
165
+ final String charset = options .get (CHARSET_KEY ).asString ();
166
+ return RequestBody .create (type , params .asString ().getBytes (charset ));
167
+ }
168
+
169
+ return RequestBody .create (type , params .asString ());
173
170
}
174
-
175
171
}
0 commit comments