ironing out some issues with making queries against objects with no .d

master
Marek Isalski 7 years ago
parent 4524068245
commit ece9a3e86a

@ -50,13 +50,15 @@ class APIv2( object ):
new_query.update( query )
if 'expand' in new_query:
new_query[ 'expand' ] = [ ",".join( new_query[ 'expand'] ) ]
if new_query[ 'expand' ]:
new_query[ 'expand' ] = [ ",".join( new_query[ 'expand' ] ) ]
else:
del new_query[ 'expand' ]
return urlqueryparse.set_query_fields( url, new_query )
new_url = urlqueryparse.set_query_fields( url, new_query )
return new_url
def make_request( self, url, data, method, _countdown = 5 ):
url = self.normalize_url( url )
method = { 'POST': requests.post,
'PATCH': requests.patch,
'GET': requests.get,
@ -82,13 +84,17 @@ class APIv2( object ):
else:
raise ValueError( request.text )
def get_one( self, url, no_d = False ):
def get_one( self, url, no_d = False, query = None ):
url = self.normalize_url( url, no_d = no_d, query = query )
return self.make_request( url, None, 'GET' )
def get_many( self, url, count = 10, no_d = False ):
next = None
url = self.normalize_url( url, no_d = no_d, query = { 'count': [ count ] } )
def get_many( self, url, count = 10, no_d = False, query = None ):
if query is None:
query = {}
query[ 'count' ] = [ count ]
url = self.normalize_url( url, no_d = no_d, query = query )
next = None
while True:
result = self.make_request( url, None, 'GET' )
if 'results' in result:

@ -16,13 +16,19 @@ def get_query_fields( url ):
def set_query_fields( url, querydict ):
components = urlparse( url )
parseddict = dict( parse_qsl( urlparse( url ).query ) )
parseddict.update( querydict )
old_query = parse_qsl( urlparse( url ).query )
new_query = []
for ( k, vs ) in querydict.items():
for v in vs:
new_query.append( ( k, v ) )
for ( k, v ) in old_query:
if k not in querydict:
new_query.append( ( k, v ) )
components = ( components.scheme,
components.netloc,
components.path,
components.params,
urlencode( list( parseddict.items() ) ),
urlencode( new_query ),
components.fragment
)
return urlunparse( components )

Loading…
Cancel
Save