I think I may be overlooking something simple. I have a class with a constructor defined. In one of the functions I make a call to an api an store the response in a variable. I'd like to return that variable. kind of like, info = MyClass(), data = info.somefunctuon(). In my case data always returns None.
here is a snippet of what I have.
class SomeAPI(object):
def __init__(self, br, cj, username, password, logged_in='', api_id='', data=''):
self.br = br
self.cj = cj
self.logged_in = False
self.username = username
self.password = password
self.api_id = api_id
br.set_cookiejar(self.cj)
br.set_handle_robots(False)
def _login_check(f):
#wraps(f)
def wrapper(self):
if self.log_in():
f(self)
else:
raise MyError('Not Logged In')
return wrapper
def log_in(self):
auth_data = {'cmd': 'auth', 'params': {'username': self.username,
'password': self.password}}
if not self.logged_in:
self.br.open('https://someurl.com/auth', dumps(auth_data))
data = loads(self.br.response().read())
status = data['response']['status']
if status == 'OK':
api_id = data['response']['api_id']
self.logged_in = True
self.api_id = api_id
else:
raise MyError(status)
return self.logged_in
#_login_check
def campaigns_list(self):
campaigns_list_data = {'cmd': 'ab_campaigns'}
raw = self.br.open('https://someurl.com/{}'.format(self.api_id),
dumps(campaigns_list_data))
json_resp = raw.read()
resp = loads(json_resp)
status = resp['response']['status']
if status == 'OK':
data = resp['response']['Ad Campaigns']
return data
when I do:
info = SomeAPI(mechanize.Browser(), cookielib.LWPCookieJar(), 'MyName', '12345')
data = info.campaigns_list()
print data
I always get None. I know that I get the data because when i replace return with print I get the expected response. I thought maybe I'd need to define data on the constructor, but then each function of my class would need its own variable defined. I think it would get messy.
Any suggestions please let me know. Thank you!
Your decorator _login_check calls the function it wraps, but it doesn't return whatever the function returns.
Change:
def _login_check(f):
#wraps(f)
def wrapper(self):
if self.log_in():
f(self)
else:
raise MyError('Not Logged In')
return wrapper
to:
def _login_check(f):
#wraps(f)
def wrapper(self):
if self.log_in():
return f(self)
else:
raise MyError('Not Logged In')
return wrapper
Related
So I have two class that are called:
a.py
import requests
class A:
def __init__(self, parent):
self.parent = parent
self.accessToken = "VeryImportant" <---
def productFeed(self, Country, UserInput):
while True:
try:
else:
params = {
'country': UserInput,
'locale': 'en-{}_{}'.format(UserInput, UserInput)
}
headers = {
'importante': 'Yessir {}'.format(self.accessToken),
}
r = requests.get("URL", params=params, headers=headers, timeout=5)
# -------------------------------------------------------------------------
if r.status_code == 200:
return r.json()
b.py
import lib.vendors.testing.a as testing
class B:
def __init__(self):
self.getClassA = testing.A
def main(self):
country = "GB"
UserInput = "Hello"
print(self.getClassA.productFeed(country, UserInput))
However it seems like I am getting issue on class A: saying TypeError: ProductFeed() missing 1 required positional argument: 'UserInput'
The issue I believe is related to the self - I assume I need to add it into the params inside the self.getClassA.productFeed(country, UserInput) however the issue will be that if I send over the self, it doesn't want to use the self.accessToken from class A in that case and then I am not able to the the correct request.
What I want to do is that if I call self.getClassA.productFeed(country, UserInput)- I want to use the self.accessToken that is in class A instead of needing to send it over
What should I do to be able to do that?
You can choose to use something as static.
If "accessToken" isn't different for every instance of the class A you can define it before the init function. In that way you can read it by using A.accessToken and your program wont crash anymore.
However if accessToken is static, this means that different instances of the class A cannot have different values for accessToken.
You can make the productFeed function static and change the self with anything else. When you call it you have to pass an instance of A as argument
In A class
#staticmethod
def productFeed(instance, Country, UserInput):
while True:
try:
else:
params = {
'country': UserInput,
'locale': 'en-{}_{}'.format(UserInput, UserInput)
}
headers = {
'importante': 'Yessir {}'.format(instance.accessToken),
}
r = requests.get("URL", params=params, headers=headers, timeout=5)
# -------------------------------------------------------------------------
if r.status_code == 200:
return r.json()
in main
def main(self):
country = "GB"
UserInput = "Hello"
print(self.getClassA.productFeed(A(None),country, UserInput))
You are not creating an instance of the A class. You use a direct reference:
testing.A
where you should use
testing.A( ... )
Your code is interpreting the country variable as the self argument because self is not a keyword
I want to build a python client on top of a REST API that uses authentication with a api_token. Hence all api calls require the api_token. As it is pretty ugly to add a field
'token=...'
e.g.
a = f1(5, token='token')
b = f2(6, 12, token='token')
c = f3(2, 'a', token='token')
where internally f1 and f2 delegate to the REST api
to each function call. What I would like to have is something like:
auth = authenticate('token')
a = f1(5)
b = f2(6, 12,)
c = f3(2, 'a')
What I can do is to create a class and make all functions member functions. Hence, we would have:
auth = calculator('token')
a = auth.f1(5)
b = auth.f2(6, 12,)
c = auth.f3(2, 'a')
but that would also be somewhat ugly. I am trying to get this to work with decorators, but to no avail so far.
class authenticate:
def __init__(self, token):
self.token = token
def __call__(self, func):
def functor(*args, **kwargs):
return func(*args, **kwargs, key=self.authentication)
return functor
#authenticate
def f1(a, key):
data = a
result = requests.get(1, data, key)
return result
However, this seems to be going nowhere. I am also wondering whether this might work at all as decorators are executed at import time and the token is added at runtime.
Any suggestions on how to make this work or anyone know if there is another standard pattern for this?
So after some hacking around we came up with the following:
class authenticate:
# start empty key
key = None
#classmethod
""" add the token """
def set_key(cls, token):
cls.token = token
def __init__(self, func=None):
if func is not None:
self.func = func
else:
print('no function')
def __call__(self, *arg):
"""
add authentication to function func
"""
ret = self.func(*arg, auth_key=self.key)
return ret
#authenticate
def f1(a, key):
data = a
result = requests.get(1, data, key)
return result
Then you can run code like:
authentication_key = 'token'
print('Initiate class')
authenticate().set_key(key=authentication_key)
print('Run f1(5)')
a1 = f1(5) # no token needed!
a2 = f2(6, 12) # again no token needed as it is in the decorator
print(a1)
This works more or less as I hoped and I find it cleaner than the class methods. If anyone has a better suggestion or improvements let me know.
I'm trying to create a mock response and want to set the response.text to some unicode chars.
Here's the code:
class TestClass(TestCase):
#mock.patch(self, mocked_get)
def test_func(self):
mocked_get.side_effect = self.mocked_get
expected_result = [u'ssemsth']
result = self.api.query(query_params)
self.assertEqual(result, expected_result)
def mocked_get(self, *args, **kwargs):
class MockResponse():
def test_result():
res = Response()
res.message = u'semsmth'
return res
# some testcondition
mock_response = MockResponse()
if self.api.counter == 1:
return mock_response.test_result()
In the result for the response, I'm getting [u''], a list with empty unicode string. I want to get [u'semsmth']
Any thoughts? I'm thinking I can't set the res.message = u'semsth' and just return that. How do I set this attribute for the Response() object i'm sending to the calling test_function
I am writing a generic wrapper around a REST API. I have several functions like the one below, responsible for retrieving a user from its email address. The part of interest is how the response is processed, based on a list of expected status codes (besides HTTP 200) and callbacks associated to each expected status code:
import requests
def get_user_from_email(email):
response = requests.get('http://example.com/api/v1/users/email:%s' % email)
# define callbacks
def return_as_json(response):
print('Found user with email [%s].' % email)
return response.json()
def user_with_email_does_not_exist(response):
print('Could not find any user with email [%s]. Returning `None`.' % email),
return None
expected_status_codes_and_callbacks = {
requests.codes.ok: return_as_json, # HTTP 200 == success
404: user_with_email_does_not_exist,
}
if response.status_code in expected_status_codes_and_callbacks:
callback = expected_status_codes_and_callbacks[response.status_code]
return callback(response)
else:
response.raise_for_status()
john_doe = get_user_from_email('john.doe#company.com')
print(john_doe is not None) # True
unregistered_user = get_user_from_email('unregistered.user#company.com')
print(unregistered_user is None) # True
The code above works well so I want to refactor and generalize the response processing part. I would love to end up with the following code:
#process_response({requests.codes.ok: return_as_json, 404: user_with_email_does_not_exist})
def get_user_from_email(email):
# define callbacks
def return_as_json(response):
print('Found user with email [%s].' % email)
return response.json()
def user_with_email_does_not_exist(response):
print('Could not find any user with email [%s]. Returning `None`.' % email),
return None
return requests.get('https://example.com/api/v1/users/email:%s' % email)
with the process_response decorator defined as:
import functools
def process_response(extra_response_codes_and_callbacks=None):
def actual_decorator(f):
#functools.wraps(f)
def wrapper(*args, **kwargs):
response = f(*args, **kwargs)
if response.status_code in expected_status_codes_and_callbacks:
action_to_perform = expected_status_codes_and_callbacks[response.status_code]
return action_to_perform(response)
else:
response.raise_for_status() # raise exception on unexpected status code
return wrapper
return actual_decorator
My problem is the decorator complains about not having access to return_as_json and user_with_email_does_not_exist because these callbacks are defined inside the wrapped function.
If I decide to move the callbacks outside of the wrapped function, for example at the same level as the decorator itself, then the callbacks have no access to the response and email variables inside the wrapped function.
# does not work either, as response and email are not visible from the callbacks
def return_as_json(response):
print('Found user with email [%s].' % email)
return response.json()
def user_with_email_does_not_exist(response):
print('Could not find any user with email [%s]. Returning `None`.' % email),
return None
#process_response({requests.codes.ok: return_as_json, 404: user_with_email_does_not_exist})
def get_user_from_email(email):
return requests.get('https://example.com/api/v1/users/email:%s' % email)
What is the right approach here? I find the decorator syntax very clean but I cannot figure out how to pass the required parts to it (either the callbacks themselves or their input arguments like response and email).
You could convert the decorator keys into strings, and then pull the inner functions from the outer function passed to the decorator via f.func_code.co_consts. Don't do it this way.
import functools, new
from types import CodeType
def decorator(callback_dict=None):
def actual_decorator(f):
code_dict = {c.co_name: c for c in f.func_code.co_consts if type(c) is CodeType}
#functools.wraps(f)
def wrapper(*args, **kwargs):
main_return = f(*args, **kwargs)
if main_return['callback'] in callback_dict:
callback_string = callback_dict[main_return['callback']]
callback = new.function(code_dict[callback_string], {})
return callback(main_return)
return wrapper
return actual_decorator
#decorator({'key_a': 'function_a'})
def main_function(callback):
def function_a(callback_object):
for k, v in callback_object.items():
if k != 'callback':
print '{}: {}'.format(k, v)
return {'callback': callback, 'key_1': 'value_1', 'key_2': 'value_2'}
main_function('key_a')
# key_1: value_1
# key_2: value_2
Can you use classes? The solution is immediate if you can use a class.
As mentioned in the comments for my other answer, here is an answer that uses classes and decorators. It's a bit counter-intuitive because get_user_from_email is declared as a class, but ends up as a function after decorating. It does have the desired syntax however, so that's a plus. Maybe this could be a starting point for a cleaner solution.
# dummy response object
from collections import namedtuple
Response = namedtuple('Response', 'data status_code error')
def callback_mapper(callback_map):
def actual_function(cls):
def wrapper(*args, **kwargs):
request = getattr(cls, 'request')
response = request(*args, **kwargs)
callback_name = callback_map.get(response.status_code)
if callback_name is not None:
callback_function = getattr(cls, callback_name)
return callback_function(response)
else:
return response.error
return wrapper
return actual_function
#callback_mapper({'200': 'json', '404': 'does_not_exist'})
class get_user_from_email:
#staticmethod
def json(response):
return 'json response: {}'.format(response.data)
#staticmethod
def does_not_exist(response):
return 'does not exist'
#staticmethod
def request(email):
response = Response('response data', '200', 'exception')
return response
print get_user_from_email('blah')
# json response: response data
Here's an approach that uses function member data on class methods in order to map the response function to the appropriate callback. This seems like the cleanest syntax to me, but still has a class turning into a function (which could be easily avoided if desired).
# dummy response object
from collections import namedtuple
Response = namedtuple('Response', 'data status_code error')
def callback(status_code):
def method(f):
f.status_code = status_code
return staticmethod(f)
return method
def request(f):
f.request = True
return staticmethod(f)
def callback_redirect(cls):
__callback_map = {}
for attribute_name in dir(cls):
attribute = getattr(cls, attribute_name)
status_code = getattr(attribute, 'status_code', '')
if status_code:
__callback_map[status_code] = attribute
if getattr(attribute, 'request', False):
__request = attribute
def call_wrapper(*args, **kwargs):
response = __request(*args, **kwargs)
callback = __callback_map.get(response.status_code)
if callback is not None:
return callback(response)
else:
return response.error
return call_wrapper
#callback_redirect
class get_user_from_email:
#callback('200')
def json(response):
return 'json response: {}'.format(response.data)
#callback('404')
def does_not_exist(response):
return 'does not exist'
#request
def request(email):
response = Response(email, '200', 'exception')
return response
print get_user_from_email('generic#email.com')
# json response: generic#email.com
You could pass the function parameters of the outer function to the handlers:
def return_as_json(response, email=None): # email param
print('Found user with email [%s].' % email)
return response.json()
#process_response({requests.codes.ok: return_as_json, 404: ...})
def get_user_from_email(email):
return requests.get('...: %s' % email)
# in decorator
# email param will be passed to return_as_json
return action_to_perform(response, *args, **kwargs)
I have the following code:
def verify_pseudo_streaming(self, publishedName, path, start):
cname = self.get_cname(publishedName)
params = {'start': start}
url = 'http://{}{}'.format(cname, path)
origin_size = int(requests.head(url).headers['Content-Length'])
start_headers = requests.head(url, params=params).headers
start_size = int(start_headers['Content-Length'])
msg = "Start size is not lower than origin size"
assert start_size < origin_size, msg
In my test I have mocked the requests.head in my unit test, how do I get the value of headers the first and second time when running requests.head without really running it ?
I finally ended up doing the one below which worked ...
class MockHeaders(object):
def __init__(self):
pass
def streaming_headers(self, *args, **kwargs):
start = kwargs.get('params', {})
self.headers['Content-Length'] = start.get('start', 10)
stuff = Mock()
stuff.headers = self.headers
return stuff
<snip> ... </snip>
#patch("FrontEnd.requests.head")
#patch("FrontEnd.FrontEnd.get_cname")
def test_verify_pseudo_streaming(self, mock_get_cname,mock_head):
mock_get_cname.return_value = 'hulahoop'
mock_header = MockHeaders()
mock_head.side_effect = mock_header.streaming_headers
mock_head.return_value = mock_header
try:
self.fe.verify_pseudo_streaming('publishedName', 'path', 5)
except AssertionError:
self.fail("Unexpected Assertion Error")
I am just going to keep this open to see if others got other more elegant ideas.
you can mock \ monkeypatch only this method
requests.sessions.Session.send
this is what requests use to send the request, so if you change that to do nothing
it will not send the request
def x(*args, **kwarg):
pass
requests.sessions.Session.send = x
I would mock out requests like this:
class FakeHeaders(object):
def __init__(self):
self.headers = {'Content-Length': 1}
def inc_headers():
self.headers['Content-Length'] += 1
def testVerifyPsuedoStreaming(self):
fake_header = FakeHeader()
with mock.patch.object(
request, 'head', side_effect=fake_header.inc_headers,
return_value=fake_header) as mock_head:
...