|
1 | 1 | package com.annimon.ownlang.modules.http;
|
2 | 2 |
|
3 | 3 | import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
|
4 |
| -import com.annimon.ownlang.exceptions.TypeException; |
5 | 4 | import com.annimon.ownlang.lib.*;
|
6 | 5 | import java.io.IOException;
|
7 | 6 | import java.io.UnsupportedEncodingException;
|
@@ -31,65 +30,59 @@ public final class http_http implements Function {
|
31 | 30 | @Override
|
32 | 31 | public Value execute(Value[] args) {
|
33 | 32 | String url, method;
|
| 33 | + Function function; |
34 | 34 | switch (args.length) {
|
35 | 35 | case 1: // http(url)
|
36 | 36 | url = args[0].asString();
|
37 | 37 | return process(url, "GET");
|
38 |
| - |
| 38 | + |
39 | 39 | case 2: // http(url, method) || http(url, callback)
|
40 | 40 | url = args[0].asString();
|
41 | 41 | if (args[1].type() == Types.FUNCTION) {
|
42 |
| - return process(url, "GET", (FunctionValue) args[1]); |
| 42 | + return process(url, "GET", ValueUtils.consumeFunction(args[1], 1)); |
43 | 43 | }
|
44 | 44 | return process(url, args[1].asString());
|
45 |
| - |
| 45 | + |
46 | 46 | case 3: // http(url, method, params) || http(url, method, callback)
|
47 | 47 | url = args[0].asString();
|
48 | 48 | method = args[1].asString();
|
49 | 49 | if (args[2].type() == Types.FUNCTION) {
|
50 |
| - return process(url, method, (FunctionValue) args[2]); |
| 50 | + return process(url, method, ValueUtils.consumeFunction(args[2], 2)); |
51 | 51 | }
|
52 |
| - return process(url, method, args[2], FunctionValue.EMPTY); |
53 |
| - |
| 52 | + return process(url, method, args[2], FunctionValue.EMPTY.getValue()); |
| 53 | + |
54 | 54 | case 4: // http(url, method, params, callback)
|
55 |
| - if (args[3].type() != Types.FUNCTION) { |
56 |
| - throw new TypeException("Fourth arg must be a function callback"); |
57 |
| - } |
58 | 55 | url = args[0].asString();
|
59 | 56 | method = args[1].asString();
|
60 |
| - return process(url, method, args[2], (FunctionValue) args[3]); |
61 |
| - |
| 57 | + function = ValueUtils.consumeFunction(args[3], 3); |
| 58 | + return process(url, method, args[2], function); |
| 59 | + |
62 | 60 | case 5: // http(url, method, params, headerParams, callback)
|
63 |
| - if (args[3].type() != Types.MAP) { |
64 |
| - throw new TypeException("Third arg must be a map"); |
65 |
| - } |
66 |
| - if (args[4].type() != Types.FUNCTION) { |
67 |
| - throw new TypeException("Fifth arg must be a function callback"); |
68 |
| - } |
69 | 61 | url = args[0].asString();
|
70 | 62 | method = args[1].asString();
|
71 |
| - return process(url, method, args[2], (MapValue) args[3], (FunctionValue) args[4]); |
72 |
| - |
| 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 | + |
73 | 67 | default:
|
74 | 68 | throw new ArgumentsMismatchException("From 1 to 5 arguments expected, got " + args.length);
|
75 | 69 | }
|
76 | 70 | }
|
77 | 71 |
|
78 | 72 | private Value process(String url, String method) {
|
79 |
| - return process(url, method, FunctionValue.EMPTY); |
| 73 | + return process(url, method, FunctionValue.EMPTY.getValue()); |
80 | 74 | }
|
81 | 75 |
|
82 |
| - private Value process(String url, String method, FunctionValue function) { |
83 |
| - return process(url, method, MapValue.EMPTY, function); |
| 76 | + private Value process(String url, String method, Function callback) { |
| 77 | + return process(url, method, MapValue.EMPTY, callback); |
84 | 78 | }
|
85 | 79 |
|
86 |
| - private Value process(String url, String method, Value params, FunctionValue function) { |
87 |
| - return process(url, method, params, MapValue.EMPTY, function); |
| 80 | + private Value process(String url, String method, Value params, Function callback) { |
| 81 | + return process(url, method, params, MapValue.EMPTY, callback); |
88 | 82 | }
|
89 | 83 |
|
90 |
| - private Value process(String url, String methodStr, Value requestParams, MapValue options, FunctionValue function) { |
| 84 | + private Value process(String url, String methodStr, Value requestParams, MapValue options, Function callback) { |
91 | 85 | final String method = methodStr.toUpperCase();
|
92 |
| - final Function callback = function.getValue(); |
93 | 86 | try {
|
94 | 87 | final Request.Builder builder = new Request.Builder()
|
95 | 88 | .url(url)
|
|
0 commit comments