https://github.com/pvlbzn/gobject
Pythonic Google Geocode API wrapper
https://github.com/pvlbzn/gobject
geocoding google-api google-geolocation json wrapper
Last synced: about 1 month ago
JSON representation
Pythonic Google Geocode API wrapper
- Host: GitHub
- URL: https://github.com/pvlbzn/gobject
- Owner: pvlbzn
- Created: 2017-02-10T13:07:30.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-12T18:48:53.000Z (over 9 years ago)
- Last Synced: 2025-09-19T00:38:28.928Z (9 months ago)
- Topics: geocoding, google-api, google-geolocation, json, wrapper
- Language: Python
- Size: 18.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://codeclimate.com/github/pvlbzn/gobject/coverage)
# Gobject
Google Geocode API wrapper supercharged with Python Data Model,
which makes Gobject behave like a first class citizen.
## Install
```
pip install gobject
```
## Example
_Assume we are working in Python REPL environement_
Import modules: requests to make a request, json as a utility, gobject. Note that `gobject` dependency free library.
It does not care about a source of JSON containing response from Google Geocode API.
```
import gobject, requests, json
```
Get JSON response from Google API
```
resp = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=Jakarta&key=YOUR_API_KEY')
resp
>
```
We got the response from API, now lets initialize `gobject`. It can be done in two ways, first one is preffered.
```
gobj = gobject.load(resp.text)
# Or another way using the class Gobject
gobj_alt = gobject.Gobject(resp.text)
```
Beeing a Pythonic, `gobject` leverages Python Data Model to be first-class citizen in your code.
```
gobj == gobj_alt
> True
```
Important feature: `gobject.load` has an inverse function `gobject.serialize`. There exists a _bijection_ relation.
Wrap the data into an object, serialize it and compare to original data.
```
(gobject.load(resp.text)).serialize() == json.loads(resp.text)
> True
```
`gobject` API follows Google Geocode API, just read Geocode docs and you will find it in Gobject too
```
gobj.bounds
> , southwest: >
gobj.bounds.northeast
>
gobj.bounds.northeast == gobj.bounds.southwest
> False
gobj.bounds.northeast = gobj.bounds.southwest
gobj.bounds.northeast == gobj.bounds.southwest
> True
len(gobj.address_components)
> 2
```
#### Errors
Gobject handles all (5 kinds) errors which may occur in 'status' field of a JSON
response from Google Geocode API.
Lets make a request with non-existent place, such as _Cappleble_.
```
resp = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=Cappleble&key=API_KEY')
resp
>
```
And load its data into the `gobject`
```
wobj = gobject.load(resp.text)
> Traceback (most recent call last):
> ...
> gobject.exception.ZeroResultsError: The geocode was successful but returned no results.
This may occur if the geocoder was passed a non-existent address.
```
Exception message says exactly what we did, the cause of it is a non-existent place.
`gobject` supports all exceptions from Google API with their explanation messages.
## Code Quality
Code quality is measured by codeclimate, you can find badge on a first line.
To test the code run:
```
py.test
```