Commit 5eb301a0 authored by Leo Pound Singer's avatar Leo Pound Singer
Browse files

Refactor to respect env variables in REST API

Reorganize the initialization of the `GraceDB` and `GraceDBBasic` classes
so that the command-line tool and the REST API Python module both respect
environment variables like `GRACEDB_SERVICE_URL`. As a nice side effect,
this change also removes a lot of duplicated code.

The main use case is testing GraceDB client code by setting the environment
variable `GRACEDB_SERVICE_URL=https://gracedb-test.ligo.org/api/`.
This must be addressed before we can make progress on grinch#10.
parent 8cd13224
......@@ -22,10 +22,6 @@ import json
from ligo.gracedb.rest import GraceDb, GraceDbBasic
import six
DEFAULT_SERVICE_URL = "https://gracedb.ligo.org/api"
DEFAULT_BASIC_URL = "https://gracedb.ligo.org/apibasic/"
GIT_TAG = 'gracedb-1.27-1'
DEFAULT_COLUMNS = "graceid,labels,group,pipeline,search,far,gpstime,created,dataurl"
......@@ -86,11 +82,6 @@ validTypes = list(typeCodeMap.keys())
def derive_client(ClientBase=GraceDb):
class client(ClientBase):
def __init__(self, url=DEFAULT_SERVICE_URL, *args, **kwargs):
if (url[-1] != '/'):
url += '/'
self.url = url
super(client, self).__init__(url, *args, **kwargs)
def download(self, graceid, filename, destfile):
# Check that we *could* write the file before we
......@@ -280,21 +271,15 @@ Longer strings will be truncated.""" % {
pass
proxy = options.proxy or os.environ.get('HTTP_PROXY', None)
service = options.service or \
os.environ.get('GRACEDB_SERVICE_URL', None) or \
DEFAULT_SERVICE_URL
service = options.service or os.environ.get('GRACEDB_SERVICE_URL', None)
# If the user requested a specific service, but also wants basic auth,
# then the service had better be a basic auth endpoint. Otherwise die.
# On the other hand, if the user did not specify a service url, then we
# will use the default basic URL if basic auth was requested.
if options.use_basic_auth:
if options.service or os.environ.get('GRACEDB_SERVICE_URL', None):
if 'basic' not in service:
error("To use the basic auth client, specify a basic auth service URL or use the default.")
exit(1)
else:
service = DEFAULT_BASIC_URL
if options.use_basic_auth and service and 'basic' not in service:
error("To use the basic auth client, specify a basic auth service URL or use the default.")
exit(1)
# Client subclass according to preferred auth method.
global Client
......@@ -304,19 +289,7 @@ Longer strings will be truncated.""" % {
if options.alert is not None:
warning("alert option is deprecated. Alerts are now sent by default.")
proxyport = None
if proxy and proxy.find(':') > 0:
try:
proxy, proxyport = proxy.split(':')
proxyport = int(proxyport)
except:
op.error("Malformed proxy: '%s'" % proxy)
if proxyport:
client = Client(service,
proxy_host=proxy,
proxy_port=proxyport)
else:
client = Client(service, proxy_host=proxy)
client = Client(url=service, proxy=proxy)
if len(args) < 1:
op.error("not enough arguments")
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment