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 ) new_query.update( query )
if 'expand' in new_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 ): def make_request( self, url, data, method, _countdown = 5 ):
url = self.normalize_url( url )
method = { 'POST': requests.post, method = { 'POST': requests.post,
'PATCH': requests.patch, 'PATCH': requests.patch,
'GET': requests.get, 'GET': requests.get,
@ -82,13 +84,17 @@ class APIv2( object ):
else: else:
raise ValueError( request.text ) 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' ) return self.make_request( url, None, 'GET' )
def get_many( self, url, count = 10, no_d = False ): def get_many( self, url, count = 10, no_d = False, query = None ):
next = None if query is None:
url = self.normalize_url( url, no_d = no_d, query = { 'count': [ count ] } ) query = {}
query[ 'count' ] = [ count ]
url = self.normalize_url( url, no_d = no_d, query = query )
next = None
while True: while True:
result = self.make_request( url, None, 'GET' ) result = self.make_request( url, None, 'GET' )
if 'results' in result: if 'results' in result:

@ -16,13 +16,19 @@ def get_query_fields( url ):
def set_query_fields( url, querydict ): def set_query_fields( url, querydict ):
components = urlparse( url ) components = urlparse( url )
parseddict = dict( parse_qsl( urlparse( url ).query ) ) old_query = parse_qsl( urlparse( url ).query )
parseddict.update( querydict ) 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 = ( components.scheme,
components.netloc, components.netloc,
components.path, components.path,
components.params, components.params,
urlencode( list( parseddict.items() ) ), urlencode( new_query ),
components.fragment components.fragment
) )
return urlunparse( components ) return urlunparse( components )

Loading…
Cancel
Save