This is indeed a smart way to do it, thanks for the answer !

On a sidenote, I actually used URL() to do the following to retrieve more 
geocoding information:

> google_geo_url = URL(scheme='http', host='maps.googleapis.com', a='maps', 
> c='api', f='geocode', args=['json'], vars={'sensor':'false', 
> 'address':addr})
>

Which, I find, is a very elegant way to make a call to an external API.

As Massimo mentioned : this only works if a/c/f are ALL defined, which can 
be confusing. But then again : URL wasn't designed for external urls.

I think Jonathan's idea to make a XURL() helper would be a nice improvement 
: it is equivalent to using encodeURL but definitely more web2py-esque.
URL(..., external=True) could also do the trick.

Like villas, I believed that specifying an explicit scheme and host would 
necessarily make it an external URL...

On Thursday, March 27, 2014 5:25:15 PM UTC+1, Jonathan Lundell wrote:
>
> On 27 Mar 2014, at 6:49 AM, Louis Amon <[email protected] <javascript:>> 
> wrote: 
> > I'm trying to use the URL() function to connect to an external website's 
> API, thus benefitting from web2py's HTML entity encoding feature. 
> > 
> > I wrote something like this : 
> > 
> > URL(scheme='http', domain='www.example.org', a='API', 
> vars={'locality':'some, city'}) 
> > 
> > and found that the encoded URL turns out like this : 
> > http://www.example.org/APP_NAME/API?locality=some%2C+city 
> > 
> > With APP_NAME being my current application in web2py... 
> > 
> > 
> > I found a monkeypatch solution like this : 
> > URL(scheme='http', domain='www.example.org', a='API', c=' ', f=' ', 
> vars={'locality':'some, city'}) 
> >   
> > Which gives me an ugly but functional URL : 
> > http://www.example.org/API/ / ?locality=some%2C+city 
> > 
> > 
> > 
> > Is there a more elegant solution to this ? 
> > 
> > 
>
> URL() doesn't anticipate being used to generate external URLs, only web2py 
> URLs. You'd be better off, I think, writing your own utility routine to do 
> what you're after. urllib.urlencode does the magic for your query string 
> (vars). 
>
> So for your example, something like: 
>
> encodeURL("http://www.example.org";, path=['API'], 
> query={'locality':'some, city'}) 
>
> ...would be pretty trivial to write (path is a list of URL path elements 
> to be suitable encoded and joined with '/').

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to