Skip to content

Commit 951e676

Browse files
committed
Code Refactor 2
1 parent e9b456a commit 951e676

13 files changed

+1607
-1600
lines changed

examples/work_unit_hello_world.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22

3-
from pyhpcc.auth import Auth
4-
from pyhpcc.models import HPCC
5-
from pyhpcc.models import WorkunitSubmit as ws
3+
from pyhpcc.models.auth import Auth
4+
from pyhpcc.models.hpcc import HPCC
5+
from pyhpcc.models.workunit_submit import WorkunitSubmit as ws
66

77
# Configurations
88
environment = "<Your environment url>" # Eg: myuniversity.hpccsystems.io

src/pyhpcc/handlers/__init__.py

Whitespace-only changes.

src/pyhpcc/handlers/roxie_handler.py

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import logging
2+
3+
import pyhpcc.config as conf
4+
from pyhpcc.errors import HPCCAuthenticationError, TypeError
5+
from pyhpcc.utils import convert_arg_to_utf8_str
6+
7+
log = logging.getLogger(__name__)
8+
9+
10+
def roxie_handler(**config):
11+
"""
12+
Decorator for HPCC Roxie class methods.
13+
14+
Parameters
15+
----------
16+
config : dict
17+
A dictionary of configuration options for HPCC Roxie class methods.
18+
19+
Returns
20+
-------
21+
function
22+
The decorated function
23+
24+
Raises
25+
------
26+
TypeError
27+
If the config parameter is not a dictionary
28+
"""
29+
30+
class APIMethod(object):
31+
api = config["api"]
32+
response_type = api.response_type
33+
method = config.get("method", "POST")
34+
require_auth = config.get("require_auth", False)
35+
use_cache = config.get("use_cache", True)
36+
37+
def __init__(self, args, kwargs):
38+
"""
39+
Constructor for the HPCC Roxie APIMethod class.
40+
41+
Parameters
42+
----------
43+
args : list
44+
The positional arguments
45+
46+
kwargs : dict
47+
The keyword arguments
48+
49+
Returns
50+
-------
51+
None
52+
"""
53+
api = self.api
54+
self.session = api.auth.session
55+
if self.require_auth and not api.auth:
56+
raise HPCCAuthenticationError("Authentication required for this method")
57+
self.post_data = kwargs.pop("data", None)
58+
self.session.headers = kwargs.pop("headers", {})
59+
self.build_parameters(args, kwargs)
60+
61+
def build_parameters(self, args, kwargs):
62+
"""
63+
Builds the parameters for the API call
64+
65+
Parameters:
66+
----------
67+
args:
68+
The positional arguments
69+
kwargs:
70+
The keyword arguments
71+
72+
Returns:
73+
-------
74+
A dictionary of parameters
75+
76+
Raises:
77+
------
78+
TypeError:
79+
If the parameter is not allowed or if duplicate parameters are passed
80+
81+
"""
82+
self.session.params = {}
83+
for idx, arg in enumerate(args):
84+
if arg is None:
85+
continue
86+
try:
87+
self.session.params[self.allowed_param[idx]] = (
88+
convert_arg_to_utf8_str(arg)
89+
)
90+
except Exception:
91+
raise TypeError("Too many arguments")
92+
for k, arg in list(kwargs.items()):
93+
# for k, arg in kwargs.items():
94+
if arg is None:
95+
continue
96+
if k in self.session.params:
97+
raise TypeError("Duplicate argument: %s" % k)
98+
99+
self.session.params[k] = convert_arg_to_utf8_str(arg)
100+
101+
log.info("PARAMS: %r", self.session.params)
102+
103+
def execute(self):
104+
"""
105+
Executes the API call
106+
107+
Parameters:
108+
----------
109+
None
110+
111+
Returns:
112+
-------
113+
The response from the API call
114+
115+
Raises:
116+
------
117+
requests.HTTPError:
118+
If the response is not OK
119+
"""
120+
self.api.cached_result = False
121+
122+
# Build the request URL
123+
full_url = (
124+
self.api.auth.get_url()
125+
+ self.api.auth.path_delimiter
126+
+ self.api.definition
127+
+ self.api.auth.path_delimiter
128+
+ self.api.roxie_port
129+
+ self.api.auth.path_delimiter
130+
+ self.api.search_service
131+
+ self.api.auth.path_delimiter
132+
+ "."
133+
+ self.response_type
134+
)
135+
136+
# Debugging
137+
if conf.DEBUG:
138+
print("full_url: ", full_url)
139+
print("self.session.params: ", self.session.params)
140+
print("self.session.headers: ", self.session.headers)
141+
print("self.post_data: ", self.post_data)
142+
143+
# If auth is required, add auth to the session
144+
if self.api.auth:
145+
auth = self.api.auth.oauth
146+
147+
# Execute request
148+
resp = self.session.request(
149+
self.method,
150+
full_url,
151+
data=self.post_data,
152+
timeout=self.api.timeout,
153+
auth=auth,
154+
)
155+
156+
# Check for errors
157+
self.api.last_response = resp
158+
resp.raise_for_status()
159+
160+
result = resp
161+
162+
# Cache the result
163+
# if self.use_cache and self.api.cache:
164+
# self.api.cache.set(self.session.params, result)
165+
166+
return result
167+
168+
def _call(*args, **kwargs):
169+
"""
170+
Calls the API method
171+
172+
Parameters
173+
----------
174+
args : list
175+
The positional arguments
176+
177+
kwargs : dict
178+
The keyword arguments
179+
180+
Returns
181+
-------
182+
object
183+
The result of the API call
184+
185+
"""
186+
method = APIMethod(args, kwargs)
187+
return method.execute()
188+
189+
return _call

src/pyhpcc/wrappers.py renamed to src/pyhpcc/handlers/thor_handler.py

+1-183
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
log = logging.getLogger(__name__)
88

99

10-
def thor_wrapper(**config):
10+
def thor_handler(**config):
1111
"""Decorator for HPCC THOR class methods.
1212
1313
Parameters
@@ -193,185 +193,3 @@ def _call(*args, **kwargs):
193193
return method.execute()
194194

195195
return _call
196-
197-
198-
def roxie_wrapper(**config):
199-
"""
200-
Decorator for HPCC Roxie class methods.
201-
202-
Parameters
203-
----------
204-
config : dict
205-
A dictionary of configuration options for HPCC Roxie class methods.
206-
207-
Returns
208-
-------
209-
function
210-
The decorated function
211-
212-
Raises
213-
------
214-
TypeError
215-
If the config parameter is not a dictionary
216-
"""
217-
218-
class APIMethod(object):
219-
api = config["api"]
220-
response_type = api.response_type
221-
method = config.get("method", "POST")
222-
require_auth = config.get("require_auth", False)
223-
use_cache = config.get("use_cache", True)
224-
225-
def __init__(self, args, kwargs):
226-
"""
227-
Constructor for the HPCC Roxie APIMethod class.
228-
229-
Parameters
230-
----------
231-
args : list
232-
The positional arguments
233-
234-
kwargs : dict
235-
The keyword arguments
236-
237-
Returns
238-
-------
239-
None
240-
"""
241-
api = self.api
242-
self.session = api.auth.session
243-
if self.require_auth and not api.auth:
244-
raise HPCCAuthenticationError("Authentication required for this method")
245-
self.post_data = kwargs.pop("data", None)
246-
self.session.headers = kwargs.pop("headers", {})
247-
self.build_parameters(args, kwargs)
248-
249-
def build_parameters(self, args, kwargs):
250-
"""
251-
Builds the parameters for the API call
252-
253-
Parameters:
254-
----------
255-
args:
256-
The positional arguments
257-
kwargs:
258-
The keyword arguments
259-
260-
Returns:
261-
-------
262-
A dictionary of parameters
263-
264-
Raises:
265-
------
266-
TypeError:
267-
If the parameter is not allowed or if duplicate parameters are passed
268-
269-
"""
270-
self.session.params = {}
271-
for idx, arg in enumerate(args):
272-
if arg is None:
273-
continue
274-
try:
275-
self.session.params[self.allowed_param[idx]] = (
276-
convert_arg_to_utf8_str(arg)
277-
)
278-
except Exception:
279-
raise TypeError("Too many arguments")
280-
for k, arg in list(kwargs.items()):
281-
# for k, arg in kwargs.items():
282-
if arg is None:
283-
continue
284-
if k in self.session.params:
285-
raise TypeError("Duplicate argument: %s" % k)
286-
287-
self.session.params[k] = convert_arg_to_utf8_str(arg)
288-
289-
log.info("PARAMS: %r", self.session.params)
290-
291-
def execute(self):
292-
"""
293-
Executes the API call
294-
295-
Parameters:
296-
----------
297-
None
298-
299-
Returns:
300-
-------
301-
The response from the API call
302-
303-
Raises:
304-
------
305-
requests.HTTPError:
306-
If the response is not OK
307-
"""
308-
self.api.cached_result = False
309-
310-
# Build the request URL
311-
full_url = (
312-
self.api.auth.get_url()
313-
+ self.api.auth.path_delimiter
314-
+ self.api.definition
315-
+ self.api.auth.path_delimiter
316-
+ self.api.roxie_port
317-
+ self.api.auth.path_delimiter
318-
+ self.api.search_service
319-
+ self.api.auth.path_delimiter
320-
+ "."
321-
+ self.response_type
322-
)
323-
324-
# Debugging
325-
if conf.DEBUG:
326-
print("full_url: ", full_url)
327-
print("self.session.params: ", self.session.params)
328-
print("self.session.headers: ", self.session.headers)
329-
print("self.post_data: ", self.post_data)
330-
331-
# If auth is required, add auth to the session
332-
if self.api.auth:
333-
auth = self.api.auth.oauth
334-
335-
# Execute request
336-
resp = self.session.request(
337-
self.method,
338-
full_url,
339-
data=self.post_data,
340-
timeout=self.api.timeout,
341-
auth=auth,
342-
)
343-
344-
# Check for errors
345-
self.api.last_response = resp
346-
resp.raise_for_status()
347-
348-
result = resp
349-
350-
# Cache the result
351-
# if self.use_cache and self.api.cache:
352-
# self.api.cache.set(self.session.params, result)
353-
354-
return result
355-
356-
def _call(*args, **kwargs):
357-
"""
358-
Calls the API method
359-
360-
Parameters
361-
----------
362-
args : list
363-
The positional arguments
364-
365-
kwargs : dict
366-
The keyword arguments
367-
368-
Returns
369-
-------
370-
object
371-
The result of the API call
372-
373-
"""
374-
method = APIMethod(args, kwargs)
375-
return method.execute()
376-
377-
return _call

0 commit comments

Comments
 (0)