comparison src/luan/modules/url/LuanUrl.java @ 1420:225808b90cee

options handling
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 26 Oct 2019 22:21:09 -0600
parents 27efb1fcbcb5
children 8ab2f0fc3829
comparison
equal deleted inserted replaced
1419:59fd2e8b1b9d 1420:225808b90cee
41 private String authPassword = null; 41 private String authPassword = null;
42 public HttpURLConnection httpCon; 42 public HttpURLConnection httpCon;
43 43
44 public LuanUrl(URL url,LuanTable options) throws LuanException { 44 public LuanUrl(URL url,LuanTable options) throws LuanException {
45 if( options != null ) { 45 if( options != null ) {
46 Map map = options.asMap(); 46 options = new LuanTable(options);
47 String methodStr = Utils.removeString(map,"method"); 47 String methodStr = Utils.removeString(options,"method");
48 if( methodStr != null ) { 48 if( methodStr != null ) {
49 methodStr = methodStr.toUpperCase(); 49 methodStr = methodStr.toUpperCase();
50 try { 50 try {
51 this.method = Method.valueOf(methodStr); 51 this.method = Method.valueOf(methodStr);
52 } catch(IllegalArgumentException e) { 52 } catch(IllegalArgumentException e) {
53 throw new LuanException( "invalid method: "+methodStr ); 53 throw new LuanException( "invalid method: "+methodStr );
54 } 54 }
55 } 55 }
56 Map headerMap = removeMap(map,"headers"); 56 Map headerMap = removeMap(options,"headers");
57 if( headerMap != null ) { 57 if( headerMap != null ) {
58 for( Object hack : headerMap.entrySet() ) { 58 for( Object hack : headerMap.entrySet() ) {
59 Map.Entry entry = (Map.Entry)hack; 59 Map.Entry entry = (Map.Entry)hack;
60 String name = (String)entry.getKey(); 60 String name = (String)entry.getKey();
61 Object val = entry.getValue(); 61 Object val = entry.getValue();
69 throw new LuanException( "header '"+name+"' table must be list" ); 69 throw new LuanException( "header '"+name+"' table must be list" );
70 headers.put(name,t.asList()); 70 headers.put(name,t.asList());
71 } 71 }
72 } 72 }
73 } 73 }
74 Map auth = removeMap(map,"authorization"); 74 LuanTable auth = Utils.removeTable(options,"authorization");
75 if( auth != null ) { 75 if( auth != null ) {
76 if( headers!=null && headers.containsKey("authorization") ) 76 if( headers!=null && headers.containsKey("authorization") )
77 throw new LuanException( "can't define authorization with header 'authorization' defined" ); 77 throw new LuanException( "can't define authorization with header 'authorization' defined" );
78 String username = Utils.removeString(auth,"username"); 78 String username = Utils.removeString(auth,"username");
79 if( username==null ) username = ""; 79 if( username==null ) username = "";
90 } else { 90 } else {
91 authUsername = username; 91 authUsername = username;
92 authPassword = password; 92 authPassword = password;
93 } 93 }
94 } 94 }
95 Map params = removeMap(map,"parameters"); 95 Map params = removeMap(options,"parameters");
96 String enctype = Utils.removeString(map,"enctype"); 96 String enctype = Utils.removeString(options,"enctype");
97 Object content = map.remove("content"); 97 Object content = options.remove("content");
98 if( content != null ) { 98 if( content != null ) {
99 if( this.method != Method.POST ) 99 if( this.method != Method.POST )
100 throw new LuanException( "content can only be used with POST" ); 100 throw new LuanException( "content can only be used with POST" );
101 if( params != null ) 101 if( params != null )
102 throw new LuanException( "content cannot be used with parameters" ); 102 throw new LuanException( "content cannot be used with parameters" );
157 } catch(IOException e) { 157 } catch(IOException e) {
158 throw new RuntimeException(e); 158 throw new RuntimeException(e);
159 } 159 }
160 } 160 }
161 } 161 }
162 Integer timeout = Utils.removeInteger(map,"time_out"); 162 Integer timeout = Utils.removeInteger(options,"time_out");
163 if( timeout != null ) 163 if( timeout != null )
164 this.timeout = timeout; 164 this.timeout = timeout;
165 Utils.checkEmpty(map); 165 Utils.checkEmpty(options);
166 } 166 }
167 this.url = url; 167 this.url = url;
168 } 168 }
169 169
170 private static void and(StringBuilder sb) { 170 private static void and(StringBuilder sb) {
178 } catch(UnsupportedEncodingException e) { 178 } catch(UnsupportedEncodingException e) {
179 throw new RuntimeException(e); 179 throw new RuntimeException(e);
180 } 180 }
181 } 181 }
182 182
183 private static Map removeMap(Map map,String key) throws LuanException { 183 private static Map removeMap(LuanTable options,String key) throws LuanException {
184 LuanTable t = Utils.removeTable(map,key); 184 LuanTable t = Utils.removeTable(options,key);
185 return t==null ? null : t.asMap(); 185 return t==null ? null : t.asMap();
186 } 186 }
187 187
188 @Override public InputStream inputStream() throws IOException, LuanException { 188 @Override public InputStream inputStream() throws IOException, LuanException {
189 try { 189 try {