55import com .annimon .ownlang .lib .*;
66import java .io .IOException ;
77import java .io .UnsupportedEncodingException ;
8- import java .nio .charset .UnsupportedCharsetException ;
9- import java .util .ArrayList ;
108import java .util .List ;
119import 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 ;
2212
2313public final class http_http implements Function {
2414
2515 private static final Value
2616 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 ();
2825
2926 @ Override
3027 public Value execute (Value ... args ) {
@@ -85,91 +82,90 @@ private Value process(String url, String method, Value params, FunctionValue fun
8582 return process (url , method , params , MapValue .EMPTY , function );
8683 }
8784
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 ();
8987 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 ));
12092 if (options .containsKey (HEADER_KEY )) {
121- applyHeaderParams ((MapValue ) options .get (HEADER_KEY ), httpMethod );
93+ applyHeaderParams ((MapValue ) options .get (HEADER_KEY ), builder );
12294 }
12395
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 ());
13799 } catch (IOException ex ) {
138100 return NumberValue .fromBoolean (false );
139101 }
140102 }
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+ }
141126
142- private void applyHeaderParams (MapValue headerParams , HttpRequestBase httpMethod ) {
127+ private void applyHeaderParams (MapValue headerParams , Request . Builder builder ) {
143128 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 );
145138 }
139+ return getStringRequestBody (params , options );
146140 }
147141
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 );
151145 for (Map .Entry <Value , Value > param : params ) {
152146 final String name = param .getKey ().asString ();
153147 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 );
155152 }
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 ();
163154 }
164155
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 ());
169160 } else {
170- entity = new StringEntity ( requestParams . asString ()) ;
161+ type = URLENCODED_MEDIA_TYPE ;
171162 }
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 ());
173170 }
174-
175171}
0 commit comments