Sunday, November 24, 2013

I'll go build my own JSONIP! Or tiny web service for user geo location

Maybe it will be the most tiny web service I have ever made but not less important than others.

Goals

I often create experimental webapps and need to know approximately the position of the user even if Geolocation.getCurrentPosition() doesn't work.

Why brand new feature of html5 Geolocation.getCurrentPosition() is not enough? When my webapp starts I want to show to the user most relevant information. But at the moment user doesn't know anything about my webapp so with high probability he will say - No to Geolocation.getCurrentPosition(). So why don't we do the 1st step and show the most relevant information without asking for Geolocation.getCurrentPosition()? And only after the user will really want more relevant information and will be ready for a request of Geolocation.getCurrentPosition() - then we will use it.

So this is my point.

What we have

  • jsonip.com
    returns: {"ip":"*.*.*.*","about":"/about","Pro!":"http://getjsonip.com"} So it returns position only for pro version

  • jsonip.appspot.com
    returns: {"ip": "*.*.*.*", "address":""}", "address":"*.*.*.*"}

It returned position earlier but now it returns only IP. And even then it is overquored sometimes.

So why don't we create our own web services?

Lets try

I'll go build my own JSONIP, with blackjack and hookers. Wait a minute ... - to be more correct it will be with ip and geo location! :)

I decide to create my own jsonip web service on the same GAE platform.

So here are the questions that I have got on my way to do it:

get a user ip

docs

request.remote_addr

get user Geo location

We are very happy with GAE - because it already provides the Geo location information inside the request: docs

request.headers.get("X-AppEngine-CityLatLong")

But we can get more - Country, Region, City. It's really cool!

response json

It is the easiest question but anyway http://stackoverflow.com/questions/12664696/how-to-properly-output-json-with-app-engine-python-webapp2 docs

test local and global

When you run your web service locally you won't get any IP and Geo information - So test it on GAE.

JSONP vs CORS

JSONP

Emulate jsonp article

CORS

And support brand new CORS

self.response.headers.add_header('Access-Control-Allow-Origin', '*')

Warning

If you are too lazy to create your own Geo location web services on GAE and want to use the one I created, just ask. Otherwise if you are going to do it without my consent I'll just lock Access-Control-Allow-Origin to support only my domains.

Example

All sources are there

No comments :

Post a Comment