Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge 55d3127 into 34d46a4
Browse files Browse the repository at this point in the history
  • Loading branch information
dnomadb committed Jul 21, 2015
2 parents 34d46a4 + 55d3127 commit 0a63372
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 2 deletions.
57 changes: 57 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# OS X
.DS_Store
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: python
python:
- "2.7"
- "3.4"
install:
- "pip install coveralls"
- "pip install -e .[test]"
script:
- py.test
- coverage run --source=mapbox -m py.test
after_success:
- coveralls
sudo: false
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Mapbox

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

2 changes: 0 additions & 2 deletions README.md

This file was deleted.

28 changes: 28 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
mapbox.py
=========
.. image:: https://travis-ci.org/mapbox/mapbox-sdk-py.svg
:target: https://travis-ci.org/mapbox/mapbox-sdk-py
.. image:: https://coveralls.io/repos/mapbox/mapbox-sdk-py/badge.svg?branch=setup-module&service=github
:target: https://coveralls.io/github/mapbox/mapbox-sdk-py?branch=setup-module

usage - surface api
-----

::

import mapbox


access_token = '{your mapbox access token}'

with mapbox.Mapbox(access_token) as mbx:
surface_response = mbx.surface('{username.mapid}',
[
[lng, lat],
...
],
layer='{name of layer to query}',
fields=[])

surface_response.json()

21 changes: 21 additions & 0 deletions mapbox/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## mapbox.py
from __future__ import with_statement
import click
from mapbox.scripts import surface_query

class Mapbox:
def __init__(self, access_token):
self.access_token = access_token

def __enter__(self):
return self

def __exit__(self, ext_t, ext_v, trace):
if ext_t:
click.echo("in __exit__", err=True)

def exists(self):
return "Mapbox exists with access token %s" % (self.access_token,)

def surface(self, mapid, points, **kwargs):
return surface_query.surface(mapid, points, self.access_token, **kwargs)
Empty file added mapbox/scripts/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions mapbox/scripts/api_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def validator(params, kwargs, queryParams={}):
for k in params.keys():
if k in kwargs:
if 'type' in params[k] and type(kwargs[k]) != params[k]['type']:
raise TypeError("%s must be a %s" % (k, params[k]['type']))
else:
queryParams[k] = kwargs[k]
elif 'required' in params[k]:
raise ValueError("%s must be provided" % (k,))
return queryParams

def lat_lng_formatter(points):
try:
return ';'.join([
','.join([
str(ll) for ll in pt
]) for pt in points
])
except:
raise TypeError("points improperly formatted")

if __name__ == '__main__':
validator()
lat_lng_formatter()
2 changes: 2 additions & 0 deletions mapbox/scripts/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Don't think we need a CLI

34 changes: 34 additions & 0 deletions mapbox/scripts/surface_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import requests
from mapbox.scripts import api_utils


def surface(mapid, points, access_token, **kwargs):
queryParams = {
'access_token': access_token,
'points': api_utils.lat_lng_formatter(points)
}

params = {
'layer': {
'required': True,
'type': str
},
'fields': {
'required': True,
'type': list
},
'zoom': {
'type': int
}
}
queryParams = api_utils.validator(params, kwargs, queryParams)

queryUrl = 'https://api.mapbox.com/v4/surface/%s.json' % (mapid,)

surface_request = requests.get(queryUrl, params=queryParams)
surface_request.raise_for_status()

return surface_request.json()

if __name__ == '__main__':
surface()
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[egg_info]
tag_build = dev

[upload]
dry-run = 1
34 changes: 34 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from codecs import open as codecs_open
from setuptools import setup, find_packages


# Get the long description from the relevant file
with codecs_open('README.rst', encoding='utf-8') as f:
long_description = f.read()


setup(name='mapbox',
version='0.0.1',
description=u"Python SDK for Mapbox APIs",
long_description=long_description,
classifiers=[],
keywords='',
author=u"Damon Burgett",
author_email='damon@mapbox.com',
url='https://github.com/mapbox/mapbox-sdk-py',
license='MIT',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
include_package_data=True,
zip_safe=False,
install_requires=[
'click',
'requests'
],
extras_require={
'test': ['pytest'],
'request': ['security']
},
entry_points="""
[console_scripts]
"""
)
62 changes: 62 additions & 0 deletions tests/test_surface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import mapbox
import click
import pytest
import requests

def test_mapbox():
with mapbox.Mapbox('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123') as mbx:
assert mbx.exists() == 'Mapbox exists with access token abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123'


def test_surface_bad():
"""Test for raising bad errors"""
query_points = [
[-122.46477127075194, 37.77641361883315],
[-122.43558883666992, 37.76447260365713],
[-122.40606307983398, 37.75117238560617],
[-122.43009567260741, 37.745471560181194],
[-122.45859146118164, 37.73651223296987],
[-122.48674392700195, 37.73406859189756]
]

with mapbox.Mapbox('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123') as mbx:
with pytest.raises(requests.exceptions.HTTPError):
surface_response = mbx.surface('mapbox.mapbox-terrain-v2',
query_points,
layer='contour',
fields=['ele'])

with pytest.raises(TypeError):
surface_response = mbx.surface('mapbox.mapbox-terrain-v2',
query_points,
layer=100,
fields=['ele'])

with pytest.raises(TypeError):
surface_response = mbx.surface('mapbox.mapbox-terrain-v2',
query_points,
layer='contour',
fields='ele')

with pytest.raises(TypeError):
surface_response = mbx.surface('mapbox.mapbox-terrain-v2',
query_points,
layer='contour',
fields=['ele'],
zoom='one_bad_dude')

with pytest.raises(ValueError):
surface_response = mbx.surface('mapbox.mapbox-terrain-v2',
query_points,
fields=['ele'])

with pytest.raises(ValueError):
surface_response = mbx.surface('mapbox.mapbox-terrain-v2',
query_points,
layer='contour')

with pytest.raises(TypeError):
surface_response = mbx.surface('mapbox.mapbox-terrain-v2',
[1,2],
layer='contour',
fields=['ele'])

0 comments on commit 0a63372

Please sign in to comment.