Skip to content

Commit

Permalink
Add basic auth support for client calls to api
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehwang committed Dec 8, 2016
1 parent dbb4a93 commit 87e4ff1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
20 changes: 10 additions & 10 deletions jarvis_cli/client/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def replace_links(k, v):


def _get_jarvis_resource_unconverted(endpoint, conn, resource_id):
url = _build_url(conn, endpoint, urllib.parse.quote(resource_id))
r = requests.get(url)
url = _build_url(conn.url, endpoint, urllib.parse.quote(resource_id))
r = requests.get(url, auth=(conn.user, conn.password))

if r.status_code == 200:
return r.json()
Expand All @@ -50,8 +50,8 @@ def _get_jarvis_resource(endpoint, conn, resource_id):


def _put_jarvis_resource_unconverted(endpoint, conn, resource_id, resource_updated):
url = _build_url(conn, endpoint, urllib.parse.quote(resource_id))
r = requests.put(url, json=resource_updated)
url = _build_url(conn.url, endpoint, urllib.parse.quote(resource_id))
r = requests.put(url, auth=(conn.user, conn.password), json=resource_updated)

if r.status_code == 200:
return r.json()
Expand All @@ -71,12 +71,12 @@ def _put_jarvis_resource(endpoint, conn, resource_id, resource_updated):

def _post_jarvis_resource_unconverted(endpoint, conn, resource_request, quiet,
skip_tags_check):
url = _build_url(conn, endpoint)
url = _build_url(conn.url, endpoint)

if skip_tags_check:
url = "{0}?skipTagsCheck=true".format(url)

r = requests.post(url, json=resource_request)
r = requests.post(url, auth=(conn.user, conn.password), json=resource_request)

if r.status_code == 200 or r.status_code == 201:
return r.json()
Expand Down Expand Up @@ -104,7 +104,7 @@ def post_event(conn, event_request, quiet=False):

def query_generator(endpoint, conn, query_params):
def query_jarvis_resources(url):
r = requests.get(url)
r = requests.get(url, auth=(conn.user, conn.password))

if r.status_code == 200:
result = r.json()
Expand All @@ -121,7 +121,7 @@ def query_jarvis_resources(url):

# REVIEW: This whole query url construction needs to be revisited

next_link = _build_url(conn, endpoint)
next_link = _build_url(conn.url, endpoint)

def query_param(field, value):
return "{0}={1}".format(field, urllib.parse.quote(value)) \
Expand Down Expand Up @@ -149,7 +149,7 @@ def query(endpoint, conn, query_params):


def get_data_summary(resource_type, conn):
url = _build_url(conn, "datasummary", resource_type)
r = requests.get(url)
url = _build_url(conn.url, "datasummary", resource_type)
r = requests.get(url, auth=(conn.user, conn.password))
r.raise_for_status()
return r.json()
13 changes: 10 additions & 3 deletions jarvis_cli/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from collections import namedtuple
from functools import partial
import configparser
from contextlib import contextmanager
Expand Down Expand Up @@ -31,12 +32,17 @@ def _get_config_param(key, config_map, expected_type=str):
return expected_type(config_map[key])

get_api_url = partial(_get_config_param, "api_url")
get_api_user = partial(_get_config_param, "api_user")
get_api_password = partial(_get_config_param, "api_password")
get_jarvis_data_directory = partial(_get_config_param, "data_directory")
get_jarvis_snapshots_directory = partial(_get_config_param, "snapshots_directory")
get_author = partial(_get_config_param, "author")

ClientConnection = namedtuple("ClientConnection", ["url", "user", "password"])

def get_client_connection(config_map):
return get_api_url(config_map).strip("/")
return ClientConnection(get_api_url(config_map).strip("/"),
get_api_user(config_map), get_api_password(config_map))

def _set_config_param(key, config_map, value, expected_type=str):
if expected_type != type(value):
Expand All @@ -45,8 +51,9 @@ def _set_config_param(key, config_map, value, expected_type=str):
# as a value validation.
config_map[key] = str(value)

set_host = partial(_set_config_param, "host")
set_port = partial(_set_config_param, "port", expected_type=int)
set_api_url = partial(_set_config_param, "api_url")
set_api_user = partial(_set_config_param, "api_user")
set_api_password = partial(_set_config_param, "api_password")
set_jarvis_data_directory = partial(_set_config_param, "data_directory")
set_jarvis_snapshots_directory = partial(_set_config_param, "snapshots_directory")
set_author = partial(_set_config_param, "author")
7 changes: 4 additions & 3 deletions jarvis_cli/interactive/for_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ def prompt_init_config(environment, config_path):
ConfigParam = namedtuple("ConfigParam", ["title", "set_func", "default"])
config_params = [
ConfigParam("Your name", config.set_author, os.getlogin()),
ConfigParam("Jarvis API hostname", config.set_host, "localhost"),
ConfigParam("Jarvis API port", config.set_port, 3000),
ConfigParam("Jarvis API data directory",
ConfigParam("Jarvis api url", config.set_api_url, "http://localhost"),
ConfigParam("Jarvis api username", config.set_api_user, ""),
ConfigParam("Jarvis api password", config.set_api_password, ""),
ConfigParam("Jarvis api data directory",
config.set_jarvis_data_directory, "/opt/jarvis"),
ConfigParam("Jarvis API snapshots directory",
config.set_jarvis_snapshots_directory,
Expand Down

0 comments on commit 87e4ff1

Please sign in to comment.