Skip to content

Commit 18d0b46

Browse files
committed
Define utils.py
- Utility functions to assist with connecting to a LabKey instance. - create_server_context, build_url, and handle_response
1 parent 357b8af commit 18d0b46

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

labkey/utils.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#
2+
# Copyright (c) 2015 LabKey Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
from __future__ import unicode_literals
17+
18+
import requests
19+
import ssl
20+
21+
from requests.adapters import HTTPAdapter
22+
from requests.packages.urllib3.poolmanager import PoolManager
23+
24+
25+
# _ssl.c:504: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure
26+
# http://lukasa.co.uk/2013/01/Choosing_SSL_Version_In_Requests/
27+
class SafeTLSAdapter(HTTPAdapter):
28+
def init_poolmanager(self, connections, maxsize, block=False):
29+
self.poolmanager = PoolManager(num_pools=connections,
30+
maxsize=maxsize,
31+
block=block,
32+
ssl_version=ssl.PROTOCOL_TLSv1)
33+
34+
35+
def create_server_context(domain, container_path, context_path=None, use_ssl=True):
36+
# TODO: Document
37+
server_context = dict(domain=domain, container_path=container_path, context_path=context_path)
38+
39+
if use_ssl:
40+
scheme = 'https'
41+
else:
42+
scheme = 'http'
43+
scheme += '://'
44+
45+
if use_ssl:
46+
session = requests.Session()
47+
session.mount(scheme, SafeTLSAdapter())
48+
else:
49+
# TODO: Is there a better way? Can we have session.mount('http')?
50+
session = requests
51+
52+
server_context['scheme'] = scheme
53+
server_context['session'] = session
54+
55+
return server_context
56+
57+
58+
def build_url(controller, action, server_context, container_path=None):
59+
"""
60+
Builds a URL from a controller and an action. Users the server context to determine domain,
61+
context path, container, etc.
62+
:param controller: The controller to use in building the URL
63+
:param action: The action to use in building the URL
64+
:param server_context:
65+
:param container_path:
66+
:return:
67+
"""
68+
sep = '/'
69+
70+
url = server_context['scheme']
71+
url += server_context['domain']
72+
73+
if server_context['context_path'] is not None:
74+
url += sep + server_context['context_path']
75+
76+
url += sep + controller
77+
78+
if container_path is not None:
79+
url += sep + container_path
80+
else:
81+
url += sep + server_context['container_path']
82+
83+
url += sep + action
84+
85+
return url
86+
87+
88+
def handle_response(response):
89+
sc = response.status_code
90+
91+
if sc == 200 or sc == 207:
92+
return response.json()
93+
elif sc == 401:
94+
print(str(sc) + ": Authorization failed.")
95+
elif sc == 404:
96+
msg = str(sc) + ": "
97+
decoded = response.json()
98+
if 'exception' in decoded:
99+
msg += decoded['exception']
100+
else:
101+
msg += 'Not found.'
102+
print(msg)
103+
elif sc == 500:
104+
msg = str(sc) + ": "
105+
decoded = response.json()
106+
if 'exception' in decoded:
107+
msg += decoded['exception']
108+
else:
109+
msg += 'Internal Server Error.'
110+
print(msg)
111+
else:
112+
print(str(sc))
113+
print(response.json())
114+
115+
return None

0 commit comments

Comments
 (0)