Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions cnmc_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
AVAILABLE_FILE_STATES = ["DISPONIBLE", "DESCARGADO"]
CUPS_CHUNK_SIZE = 10


class Client(object):
def __init__(self, key=None, secret=None, environment=None):
def __init__(self, key=None, secret=None, environment=None, timeout=None):

# Handle the key
self.key = key
Expand All @@ -29,18 +30,20 @@ def __init__(self, key=None, secret=None, environment=None):
self.environment = "prod"
if environment:
self.environment = environment

self.timeout = timeout
self.API = CNMC_API(key=self.key, secret=self.secret, environment=self.environment)


def test(self, message):
"""
Test do not follow the default method
"""
params = {
"m": message,
}
response = self.API.get(resource="/test/v1/echoseguro", params=params)
response = self.API.get(
resource="/test/v1/echoseguro", params=params,
timeout=self.timeout
)

# Validate and deserialize the response
schema = TestSchema()
Expand All @@ -51,7 +54,6 @@ def test(self, message):
else:
raise ValueError('Result deserialization is not performed properly for "{}"'.format(repr(result)))


def list(self, status=None, date_start=None, date_end=None):
"""
List downloaded files or files able to be downloaded, with the capacity of filter it by:
Expand Down Expand Up @@ -82,7 +84,10 @@ def list(self, status=None, date_start=None, date_end=None):
params['fechaHasta'] = date_start

# Ask the API
response = self.API.post(resource="/ficheros/v1/consultar", params=params)
response = self.API.post(
resource="/ficheros/v1/consultar", params=params,
timeout=self.timeout
)

# Validate and deserialize the response
schema = ListSchema()
Expand All @@ -93,14 +98,14 @@ def list(self, status=None, date_start=None, date_end=None):
else:
raise ValueError('Result deserialization is not performed properly for "{}"'.format(repr(result)))


def fetch_massive(self, cups, file_type, as_csv=False, wait=0):
"""
Fetch massively a list of CUPS, internally will chunk it to ask N fetch requests

:param cups: list of cups to fetch
:param file_type: desired files to download, see available SIPS files
:param as_csv: bool flag to return a CSV or a BytesIO instance
:param wait: number of seconds to wait before the next reaquest chunk
:return: List of CNMC_File models //{'code': 200, 'result': <csv.DictReader instance at 0x7f194e0f23f8>, 'error': False}
"""
results = []
Expand All @@ -115,7 +120,6 @@ def fetch_massive(self, cups, file_type, as_csv=False, wait=0):
cups_block = cups[start:start + CUPS_CHUNK_SIZE]
return results


def fetch(self, cups, file_type, as_csv=False):
"""
Fetch partial data for a list of CUPS
Expand Down Expand Up @@ -145,7 +149,11 @@ def fetch(self, cups, file_type, as_csv=False):
}

# Ask the API
response = self.API.download(resource="/verticales/v1/SIPS/consulta/v1/{}.csv".format(file_type), params=params)
response = self.API.download(
resource="/verticales/v1/SIPS/consulta/v1/{}.csv".format(file_type),
params=params,
timeout=self.timeout
)

# Return a csv reader if needed
if as_csv:
Expand All @@ -162,10 +170,6 @@ def fetch(self, cups, file_type, as_csv=False):
else:
raise ValueError('Fetch result deserialization is not performed properly for "{}"'.format(repr(result)))


return response


def download(self, filename):
"""
Download
Expand All @@ -178,5 +182,8 @@ def download(self, filename):
assert type(filename) == str

# Ask the API
response = self.API.get(resource="/ficheros/v1/descarga/{}".format(filename))
response = self.API.get(
resource="/ficheros/v1/descarga/{}".format(filename),
timeout=self.timeout
)
return response
26 changes: 8 additions & 18 deletions cnmc_client/cnmc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

import socket
import httplib
import oauth.oauth as oauth
import urllib
Expand All @@ -13,6 +14,7 @@
}
NULL_TOKEN = None


class CNMC_API(object):

def __init__(self, key=None, secret=None, environment=None, **kwargs):
Expand Down Expand Up @@ -42,10 +44,6 @@ def __init__(self, key=None, secret=None, environment=None, **kwargs):

self.url = CNCM_envs[self.environment]

self.NIF = self.get_NIF()



def get_NIF(self):
"""
Get NIF from test API method
Expand All @@ -55,17 +53,9 @@ def get_NIF(self):
response = self.get(resource="/test/v1/nif")
assert response['code'] == 200, "Connection is not established properly '{}'. Review oauth configuraion".format(str(response))

assert 'result' in response and 'empresa' in response['result'] and response['result']['empresa'][0]
assert 'result' in response and 'empresa' in response['result'] and response['result']['empresa'][0]
return response['result']['empresa'][0]


def set_request_token (self):
"""
Set the request token for current session
"""
self.request_token = self.session.fetch_request_token(self.url)


def method(self, method, resource, download=False, **kwargs):
"""
Main method handler
Expand All @@ -76,15 +66,18 @@ def method(self, method, resource, download=False, **kwargs):
from urlparse import urlparse
parsed = urlparse(url)
params = kwargs.get('params', None)
#response = self.session.request(method=method, url=url, **kwargs)
timeout = kwargs.get('timeout', socket._GLOBAL_DEFAULT_TIMEOUT)
consumer = oauth.OAuthConsumer(self.key, self.secret)
signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1()
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
consumer, token=NULL_TOKEN, http_method=method,http_url=url,
parameters=params
)
oauth_request.sign_request(signature_method_hmac_sha1, consumer, NULL_TOKEN)
connection = httplib.HTTPSConnection("%s:%d" % (parsed.hostname, parsed.port or 443))
connection = httplib.HTTPSConnection(
"%s:%d" % (parsed.hostname, parsed.port or 443),
timeout=timeout
)
if method == 'GET' and params:
resource +='?{}'.format(
urllib.urlencode(params)
Expand Down Expand Up @@ -113,21 +106,18 @@ def method(self, method, resource, download=False, **kwargs):
'error': False,
}


def get(self, resource, **kwargs):
"""
GET method, it dispatch a session.get method consuming the desired resource
"""
return self.method(method="GET", resource=resource, **kwargs)


def post(self, resource, **kwargs):
"""
POST method, it dispatch a session.get method consuming the desired resource
"""
return self.method(method="POST", resource=resource, **kwargs)


def download(self, resource, **kwargs):
"""
GET method, it dispatch a session.get method consuming the desired resource
Expand Down