Skip to content

Commit

Permalink
Fixed some issues with Canadapost & updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Mar 1, 2015
1 parent 3899b68 commit 06ceb4b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 71 deletions.
35 changes: 13 additions & 22 deletions docs/providers/CanadaPost.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,30 @@ searching to improve data accuracy and relevancy. Simply start typing a business
name, address or Postal Code and AddressComplete will suggest results as you go.
Using Geocoder you can retrieve CanadaPost's geocoded data from Addres Complete API.

## Python Example
## Examples

**Getting Postal Code**

```python
>>> import geocoder
>>> g = geocoder.canadapost('<address>')
>>> g = geocoder.canadapost('453 Booth Street, Ottawa')
>>> g.postal
'K1R 7K9'
>>> g.json
...
```

## Geocoder Attributes

* address
* country
* key
* locality
* location
* ok
* postal
* provider
* quality
* route
* state
* status
* street_number
**Command Line Interface**

```bash
$ geocode '453 Booth Street, Ottawa' --provider canadapost
```

## Parameters

* :param ``location``: Your search location you want geocoded.
* :param ``key``: (optional) use your own API Key from CanadaPost Address Complete.
- `location`: Your search location you want geocoded.
- `key`: (optional) API Key from CanadaPost Address Complete.

## References

* [GitHub Repo](https://github.com/DenisCarriere/geocoder)
* [GitHub Wiki](https://github.com/DenisCarriere/geocoder/wiki)
* [Addres Complete API](https://www.canadapost.ca/pca/)
- [Addres Complete API](https://www.canadapost.ca/pca/)
8 changes: 5 additions & 3 deletions docs/providers/W3W.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ $ geocode '45.15, -75.14' --provider w3w --method reverse

## Parameters

- **location**: Your search location you want geocoded.
- **key**: W3W API key.
- **method**: Chose a method (geocode, method)
- `location`: Your search location you want geocoded.
- `key`: W3W API key.
- `method`: Chose one of the following methods:
* geocode
* reverse

## References

Expand Down
3 changes: 1 addition & 2 deletions geocoder/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ def canadapost(location, **kwargs):
"""CanadaPost Provider
:param ``location``: Your search location you want geocoded.
:param ``key``: (optional) use your own API Key from
CanadaPost Address Complete.
:param ``key``: (optional) API Key from CanadaPost Address Complete.
"""
return get(location, provider='canadapost', **kwargs)

Expand Down
73 changes: 29 additions & 44 deletions geocoder/canadapost.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,25 @@
import requests
from .base import Base
from .keys import canadapost_key
from .location import Location


class Canadapost(Base):
"""
Addres Complete API
=======================
The next generation of address finders, AddressComplete uses intelligent, fast
searching to improve data accuracy and relevancy. Simply start typing a business
name, address or Postal Code and AddressComplete will suggest results as you go.
The next generation of address finders, AddressComplete uses
intelligent, fast searching to improve data accuracy and relevancy.
Simply start typing a business name, address or Postal Code
and AddressComplete will suggest results as you go.
Params
------
:param ``location``: Your search location you want geocoded.
:param ``key``: (optional) API Key from CanadaPost Address Complete.
API Reference
-------------
https://www.canadapost.ca/pca/
OSM Quality (6/6)
-----------------
[x] addr:housenumber
[x] addr:street
[x] addr:city
[x] addr:state
[x] addr:country
[x] addr:postal
Attributes (13/17)
------------------
[x] accuracy
[x] address
[ ] bbox
[x] city
[ ] confidence
[x] country
[x] housenumber
[ ] lat
[ ] lng
[x] location
[x] ok
[x] postal
[x] provider
[x] quality
[x] state
[x] status
[x] street
"""
provider = 'canadapost'
method = 'geocode'
Expand All @@ -59,24 +35,31 @@ def __init__(self, location, **kwargs):
self.key = kwargs.get('key', canadapost_key)
self.timeout = kwargs.get('timeout', 5.0)
self.proxies = kwargs.get('proxies', '')
self.id = ''
self.key = ''

# Connect to CanadaPost to retrieve API key if none are provided
if not self.key:
self._retrieve_key()
self._retrieve_id()

# Define parameters
self.params = {
'Key': self.key,
'Id': self.id,
'Source': '',
}

if bool(self.key and self.id):
self.params = {
'Key': self.key,
'Id': self.id,
'Source': '',
}
self._initialize(**kwargs)
else:
self.json = dict()
self.parse = self.tree()
self._json()

def __repr__(self):
return "<[{0}] {1} [{2} - {3}]>".format(self.status, self.provider, self.postal, self.address)
return "<[{0}] {1} [{2} - {3}]>".format(self.status,
self.provider,
self.postal,
self.address)

def _retrieve_key(self):
url = 'http://www.canadapost.ca/cpo/mc/personal/postalcode/fpc.jsf'
Expand Down Expand Up @@ -105,10 +88,12 @@ def _retrieve_id(self, last_id=''):
'SearchTerm': self.location,
}

url = 'https://ws1.postescanada-canadapost.ca/AddressComplete'
url += '/Interactive/Find/v2.00/json3ex.ws'
url = 'https://ws1.postescanada-canadapost.ca/AddressComplete' \
'/Interactive/Find/v2.00/json3ex.ws'
try:
r = requests.get(url, params=params, timeout=self.timeout, proxies=self.proxies)
r = requests.get(url, params=params,
timeout=self.timeout,
proxies=self.proxies)
items = r.json().get('Items')
self.status_code = 200
except:
Expand Down

0 comments on commit 06ceb4b

Please sign in to comment.