Mercurial Hosting > luan
diff src/luan/modules/url/LuanUrl.java @ 1389:eb8b35dccd99
cleanup and stringify change
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 03 Sep 2019 22:54:31 -0600 |
parents | 45363886f256 |
children | 002152af497a |
line wrap: on
line diff
--- a/src/luan/modules/url/LuanUrl.java Tue Sep 03 22:12:53 2019 -0600 +++ b/src/luan/modules/url/LuanUrl.java Tue Sep 03 22:54:31 2019 -0600 @@ -44,7 +44,7 @@ public LuanUrl(URL url,LuanTable options) throws LuanException { if( options != null ) { Map map = options.asMap(); - String methodStr = getString(map,"method"); + String methodStr = Utils.removeString(map,"method"); if( methodStr != null ) { methodStr = methodStr.toUpperCase(); try { @@ -53,7 +53,7 @@ throw new LuanException( "invalid method: "+methodStr ); } } - Map headerMap = getMap(map,"headers"); + Map headerMap = removeMap(map,"headers"); if( headerMap != null ) { for( Object hack : headerMap.entrySet() ) { Map.Entry entry = (Map.Entry)hack; @@ -71,15 +71,15 @@ } } } - Map auth = getMap(map,"authorization"); + Map auth = removeMap(map,"authorization"); if( auth != null ) { if( headers!=null && headers.containsKey("authorization") ) throw new LuanException( "can't define authorization with header 'authorization' defined" ); - String username = getString(auth,"username"); + String username = Utils.removeString(auth,"username"); if( username==null ) username = ""; - String password = getString(auth,"password"); + String password = Utils.removeString(auth,"password"); if( password==null ) password = ""; - String type = getString(auth,"type"); + String type = Utils.removeString(auth,"type"); if( !auth.isEmpty() ) throw new LuanException( "unrecognized authorization options: "+auth ); if( type != null ) { @@ -92,8 +92,8 @@ authPassword = password; } } - Map params = getMap(map,"parameters"); - String enctype = getString(map,"enctype"); + Map params = removeMap(map,"parameters"); + String enctype = Utils.removeString(map,"enctype"); Object content = map.remove("content"); if( content != null ) { if( this.method != Method.POST ) @@ -159,11 +159,10 @@ } } } - Integer timeout = getInt(map,"time_out"); + Integer timeout = Utils.removeInt(map,"time_out"); if( timeout != null ) this.timeout = timeout; - if( !map.isEmpty() ) - throw new LuanException( "unrecognized options: "+map ); + Utils.checkEmpty(map); } this.url = url; } @@ -181,32 +180,8 @@ } } - private static String getString(Map map,String key) throws LuanException { - Object val = map.remove(key); - if( val!=null && !(val instanceof String) ) - throw new LuanException( "parameter '"+key+"' must be a string" ); - return (String)val; - } - - private static Integer getInt(Map map,String key) throws LuanException { - Object val = map.remove(key); - if( val==null ) - return null; - Integer i = Luan.asInteger(val); - if( i==null ) - throw new LuanException( "parameter '"+key+"' must be an integer" ); - return i; - } - - private static LuanTable getTable(Map map,String key) throws LuanException { - Object val = map.remove(key); - if( val!=null && !(val instanceof LuanTable) ) - throw new LuanException( "parameter '"+key+"' must be a table" ); - return (LuanTable)val; - } - - private static Map getMap(Map map,String key) throws LuanException { - LuanTable t = getTable(map,key); + private static Map removeMap(Map map,String key) throws LuanException { + LuanTable t = Utils.removeTable(map,key); return t==null ? null : t.asMap(); }