normalize query strings which contain API URLs
This commit is contained in:
parent
b469be7dbb
commit
007909891c
@ -2,6 +2,9 @@ import requests
|
||||
import requests.exceptions
|
||||
import json
|
||||
import time
|
||||
import re
|
||||
|
||||
path_to_pk = re.compile( r".*/([0-9]+)/(\?.*)?(#.*)?" )
|
||||
|
||||
from . import urlqueryparse
|
||||
|
||||
@ -33,6 +36,34 @@ class APIv2( object ):
|
||||
path = path + "/"
|
||||
return path
|
||||
|
||||
def path_to_pk( self, path ):
|
||||
if not isinstance( path, str ):
|
||||
return path
|
||||
if path.startswith( self.base ) or path.startswith( self.api_path ):
|
||||
try:
|
||||
match = path_to_pk.split( path )
|
||||
return match[ 1 ]
|
||||
except:
|
||||
return path
|
||||
return path
|
||||
|
||||
def normalize_query( self, query ):
|
||||
for ( k, v ) in query.items():
|
||||
if isinstance( v, tuple ):
|
||||
query[ k ] = list( v )
|
||||
elif isinstance( v, list ):
|
||||
pass
|
||||
else:
|
||||
query[ k ] = [ v ]
|
||||
query[ k ] = [ self.path_to_pk( x ) for x in v ]
|
||||
|
||||
if 'expand' in query:
|
||||
if query[ 'expand' ]:
|
||||
query[ 'expand' ] = [ ",".join( query[ 'expand' ] ) ]
|
||||
else:
|
||||
del query[ 'expand' ]
|
||||
return query
|
||||
|
||||
def normalize_url( self, url, no_d = False, query = None ):
|
||||
url = urlqueryparse.set_url_components( url,
|
||||
scheme = 'https',
|
||||
@ -51,33 +82,29 @@ class APIv2( object ):
|
||||
if query:
|
||||
new_query.update( query )
|
||||
|
||||
if 'expand' in new_query:
|
||||
if new_query[ 'expand' ]:
|
||||
new_query[ 'expand' ] = [ ",".join( new_query[ 'expand' ] ) ]
|
||||
else:
|
||||
del new_query[ 'expand' ]
|
||||
new_query = self.normalize_query( new_query )
|
||||
|
||||
new_url = urlqueryparse.set_query_fields( url, new_query )
|
||||
return new_url
|
||||
|
||||
def get( self, url, query = None, _countdown = 5 ):
|
||||
url = self.normalize_url( url, query = query )
|
||||
def get( self, url, query = None, no_d = False, _countdown = 5 ):
|
||||
url = self.normalize_url( url, query = query, no_d = no_d )
|
||||
return self.make_request( url, data = None, method = 'GET', _countdown = _countdown )
|
||||
|
||||
def post( self, url, data, query = None, _countdown = 5 ):
|
||||
url = self.normalize_url( url, query = query )
|
||||
def post( self, url, data, query = None, no_d = False, _countdown = 5 ):
|
||||
url = self.normalize_url( url, query = query, no_d = no_d )
|
||||
return self.make_request( url, data = data, method = 'POST', _countdown = _countdown )
|
||||
|
||||
def patch( self, url, data, query = None, _countdown = 5 ):
|
||||
url = self.normalize_url( url, query = query )
|
||||
def patch( self, url, data, query = None, no_d = False, _countdown = 5 ):
|
||||
url = self.normalize_url( url, query = query, no_d = no_d )
|
||||
return self.make_request( url, data = data, method = 'PATCH', _countdown = _countdown )
|
||||
|
||||
def put( self, url, data, query = None, _countdown = 5 ):
|
||||
url = self.normalize_url( url, query = query )
|
||||
def put( self, url, data, query = None, no_d = False, _countdown = 5 ):
|
||||
url = self.normalize_url( url, query = query, no_d = no_d )
|
||||
return self.make_request( url, data = data, method = 'PUT', _countdown = _countdown )
|
||||
|
||||
def delete( self, url, query = None, _countdown = 5 ):
|
||||
url = self.normalize_url( url, query = query )
|
||||
def delete( self, url, query = None, no_d = False, _countdown = 5 ):
|
||||
url = self.normalize_url( url, query = query, no_d = no_d )
|
||||
return self.make_request( url, data = None, method = 'DELETE', _countdown = _countdown )
|
||||
|
||||
def make_request( self, url, data, method, _countdown = 5 ):
|
||||
|
Loading…
x
Reference in New Issue
Block a user