Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow hostnames to be specified via environment variables #102

Merged
merged 6 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__
*.egg-info
.eggs
.cache
.pytest_cache
.tox
build
dist
Expand Down
2 changes: 1 addition & 1 deletion datadotworld/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
)
from datadotworld.datadotworld import DataDotWorld, UriParam # noqa: F401

__version__ = '1.6.0'
__version__ = '1.6.1'

# Convenience top-level functions

Expand Down
10 changes: 4 additions & 6 deletions datadotworld/client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
ContentNegotiatingApiClient
)
from datadotworld.util import parse_dataset_key, _user_agent

from datadotworld.hosts import API_HOST, DOWNLOAD_HOST

class RestApiClient(object):
"""REST API client
Expand All @@ -48,10 +48,8 @@ class RestApiClient(object):
def __init__(self, config):
self._config = config
self._protocol = 'https'
laconc marked this conversation as resolved.
Show resolved Hide resolved
self._download_host = 'download.data.world'

api_host = 'api.data.world'
self._host = "{}://{}/v0".format(self._protocol, api_host)
self._host = "{}/v0".format(API_HOST)
swagger_client = _swagger.ApiClient(
host=self._host,
header_name='Authorization',
Expand Down Expand Up @@ -457,8 +455,8 @@ def download_datapackage(self, dataset_key, dest_dir):
'but {} already exists'.format(dest_dir))

owner_id, dataset_id = parse_dataset_key(dataset_key)
url = "{0}://{1}/datapackage/{2}/{3}".format(
self._protocol, self._download_host, owner_id, dataset_id)
url = "{0}/datapackage/{1}/{2}".format(
DOWNLOAD_HOST, owner_id, dataset_id)
headers = {
'User-Agent': _user_agent(),
'Authorization': 'Bearer {0}'.format(self._config.auth_token)
Expand Down
8 changes: 3 additions & 5 deletions datadotworld/datadotworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from datadotworld.models.query import QueryResults
from datadotworld.util import _user_agent, parse_dataset_key
from datadotworld.files import RemoteFile
from datadotworld.hosts import QUERY_HOST


class DataDotWorld(object):
Expand All @@ -52,9 +53,6 @@ class DataDotWorld(object):
"""

def __init__(self, config=None):
self._protocol = 'https'
self._query_host = 'query.data.world'
self._download_host = 'download.data.world'
self._config = config or ChainedConfig()
self.api_client = RestApiClient(self._config)

Expand Down Expand Up @@ -100,8 +98,8 @@ def query(self, dataset_key, query, query_type="sql", parameters=None):
params["parameters"] = ",".join(["{}={}".format(
k, convert_to_sparql_literal(parameters[k]))
for k in parameters.keys()])
url = "{0}://{1}/{2}/{3}/{4}".format(self._protocol, self._query_host,
query_type, owner_id, dataset_id)
url = "{0}/{1}/{2}/{3}".format(QUERY_HOST,
query_type, owner_id, dataset_id)
headers = {
'User-Agent': _user_agent(),
'Accept': 'application/sparql-results+json',
Expand Down
6 changes: 3 additions & 3 deletions datadotworld/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import requests
from datadotworld.util import parse_dataset_key, _user_agent
from datadotworld.client.api import RestApiError

from datadotworld.hosts import API_HOST, QUERY_HOST

class RemoteFile:
""" """
Expand Down Expand Up @@ -68,8 +68,8 @@ def __init__(self, config, dataset_key, file_name,
whether to decode textual responses as unicode when returning
streamed lines in 'r' mode
"""
self._api_host = "https://api.data.world/v0"
self._query_host = "https://query.data.world"
self._api_host = "{}/v0".format(API_HOST)
self._query_host = QUERY_HOST
self._config = config
self._dataset_key = dataset_key
self._file_name = file_name
Expand Down
28 changes: 28 additions & 0 deletions datadotworld/hosts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# data.world-py
# Copyright 2017 data.world, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the
# License.
#
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# This product includes software developed at
# data.world, Inc.(http://data.world/).

from os import environ

# define constants for the various hosts the SDK may need to connect to
# for connecting to the public data.world server, the defaults are fine -
# to connect to a separate data.world environment, the environment variables
# can be specified.
API_HOST = environ.get('DW_API_HOST', 'https://api.data.world')
DOWNLOAD_HOST = environ.get('DW_DOWNLOAD_HOST', 'https://download.data.world')
QUERY_HOST = environ.get('DW_QUERY_HOST', 'https://query.data.world')