Here are the examples of the python api urllib.parse.urlencode taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
171 Examples
5
Example 1
Project: TrustRouter Source File: test_urllib.py
def test_nonstring_seq_values(self):
self.assertEqual("a=1&a=2", urllib.parse.urlencode({"a": [1, 2]}, True))
self.assertEqual("a=None&a=a",
urllib.parse.urlencode({"a": [None, "a"]}, True))
self.assertEqual("a=a&a=b",
urllib.parse.urlencode({"a": {"a": 1, "b": 1}}, True))
3
Example 2
def delete(self, url, data=None, headers=None, doseq=True):
if data is not None:
if isinstance(data, dict):
data = urlencode(data, doseq=doseq)
if '?' in url:
url += '&%s' % data
else:
url += '?%s' % data
return self._fetch(url, 'DELETE', headers=headers)
3
Example 3
def get_oauth_token(self):
"""Returns the oauth access token."""
if self._oauth_token is None:
resp, content = self._client.request(
TWITTER_ACCESS_TOKEN_URL + '?', 'POST',
body=urlencode({
'x_auth_username': self.username.encode('utf-8'),
'x_auth_password': self.password.encode('utf-8'),
'x_auth_mode': 'client_auth'
}),
headers={'Content-Type': 'application/x-www-form-urlencoded'}
)
if resp['status'] != '200':
raise RuntimeError('unable to login to Twitter')
data = dict(parse_qsl(content))
self._oauth_token = data['oauth_token']
self._oauth_token_secret = data['oauth_token_secret']
return self._oauth.Token(self._oauth_token,
self._oauth_token_secret)
3
Example 4
def __build_url(self, api_call, **kwargs):
"""Builds the api query"""
kwargs['key'] = self.api_key
if 'language' not in kwargs:
kwargs['language'] = self.language
if 'format' not in kwargs:
kwargs['format'] = self.__format
api_query = urlencode(kwargs)
return "{0}{1}?{2}".format(urls.BASE_URL,
api_call,
api_query)
3
Example 5
def buildUrl(self, cache_friendly=False):
"""
Construct the URL to get the sound from Goggle API.
If cache_friendly is True, remove token from URL to use as a cache key.
"""
params = collections.OrderedDict()
params["client"] = "tw-ob"
params["ie"] = "UTF-8"
params["idx"] = str(self.segment_num)
if self.segment_count is not None:
params["total"] = str(self.segment_count)
params["textlen"] = str(len(self.text))
params["tl"] = self.lang
lower_text = self.text.lower()
params["q"] = lower_text
return "%s?%s" % (__class__.BASE_URL, urllib.parse.urlencode(params))
3
Example 6
def gravatar_url(gravatar, size=200):
# TODO: 'd' should be https://domain/img/avatar.png
if gravatar:
gravatar_hash = hashlib.md5(gravatar.lower().encode()).hexdigest()
else:
gravatar_hash = ''
return ('//gravatar.proxy.ustclug.org/avatar/' + gravatar_hash + "?" +
parse.urlencode({'d': 'mm', 's': str(size)}))
3
Example 7
def create_search(self, query_expression):
endpoint = self.endpoint_start + "searches"
# sends a POST request to https://<server_ip>/rest/api/ariel/searches
data = {'query_expression': query_expression}
data = urllib.parse.urlencode(data)
data = data.encode('utf-8')
return self.call_api(endpoint, 'POST', self.headers, data=data)
3
Example 8
def reverse(self, view_name=None, query_params=None, **kwargs):
# Specified in integration/vX/conftest.py
version = pytest.api_version
if self.url_kwargs is not None:
kwargs.setdefault('kwargs', self.url_kwargs)
if not view_name:
view_name = self.view_name
url = reverse(view_name, **kwargs)
if query_params:
for param, value in query_params.items():
if isinstance(value, list):
query_params[param] = ','.join(map(str, value))
url += '?' + urllib.parse.urlencode(query_params)
return url
3
Example 9
Project: btc-e.api.python Source File: btceapi_python3.py
def __api_call(self,method,params):
self.__nonce()
params['method'] = method
params['nonce'] = str(self.__nonce_v)
params = urllib.parse.urlencode(params)
headers = {"Content-type" : "application/x-www-form-urlencoded",
"Key" : self.__api_key,
"Sign" : self.__signature(params)}
conn = http.client.HTTPSConnection("btc-e.com")
conn.request("POST", "/tapi", params, headers)
response = conn.getresponse().read().decode()
data = json.loads(response)
conn.close()
return data
3
Example 10
Project: aiohttp Source File: helpers.py
def _gen_form_urlencoded(self, encoding):
# form data (x-www-form-urlencoded)
data = []
for type_options, _, value in self._fields:
data.append((type_options['name'], value))
data = urlencode(data, doseq=True)
return data.encode(encoding)
3
Example 11
def test_oauth(self):
self._seed_course()
# Login
self._login_as(self.user1.email)
# Start OAuth and click "Confirm"
self.page_load('{}/oauth/authorize?{}'.format(
self.get_server_url(),
urllib.parse.urlencode({
'response_type': 'code',
'client_id': self.oauth_client.client_id,
'redirect_uri': self.oauth_client.redirect_uris[0],
'scope': 'email',
}),
))
self.assertIn(self.user1.email, self.driver.page_source)
self._confirm_oauth()
3
Example 12
Project: fbterminal Source File: facebook.py
def authorize(self):
dialog_url = 'https://www.facebook.com/dialog/oauth'
fields = urlencode({'client_id': self.app_id, 'redirect_uri': self.app_url, 'scope': self.permissions})
webbrowser.open(dialog_url + '?' + fields)
serv = Httpserv.HTTPServer(('localhost', 7777), httpServHandler)
serv.handle_request()
code_url = 'https://graph.facebook.com/oauth/access_token'
fields = {'client_id': self.app_id, 'redirect_uri': self.app_url, 'client_secret': self.app_secret, 'code': code}
req = requests.get(code_url, params=fields)
config = ConfigParser()
config.read(self.config_file_path)
config.set('access_token', 'access_token', req.text.split('&')[0].split('=')[1])
config.write(open(self.config_file_path, 'w+'))
3
Example 13
def _request(self, url, req = {}, headers = {}):
""" Send POST request to API server.
:param url: fully-qualified URL with all necessary urlencoded
information
:type url: str
:param req: additional API request parameters
:type req: dict
:param headers: additional HTTPS headers, such as API-Key and API-Sign
:type headers: dict
"""
data = urllib.parse.urlencode(req)
headers.update(self.headers)
self.conn.request("POST", url, data, headers)
response = self.conn.getresponse()
return response.read().decode()
3
Example 14
@register.assignment_tag(takes_context=True)
def gravatar_url(context, user, size):
prefix = "https://" if context['request'].is_secure() else "http://"
default = "%s%s%s_%s.png" % (
prefix,
context['request'].META['HTTP_HOST'],
settings.DEFAULT_PROFILE_PICTURE_PREFIX,
user.gender
)
grav_url = "https://www.gravatar.com/avatar/" + hashlib.md5(user.email.encode()).hexdigest() + "?"
grav_url += urllib.parse.urlencode({'d': default, 's': str(size)})
return grav_url
3
Example 15
def _compose_url(self, path, params=None):
# TODO: fix this, as per our conversation at Oct. 4, 2011, 05:10 UTC
p = {self.API_KEY_PARAM: self.api_key}
if params:
p.update(params)
return self.BASE_URL + path + '?' + urlencode(p)
3
Example 16
Project: graphql-django-view Source File: test_http.py
def test_supports_post_url_encoded_query_with_string_variables(client):
response = client.post(url_string(), urlencode(dict(
query='query helloWho($who: String){ test(who: $who) }',
variables=json.dumps({'who': "Dolly"})
)), 'application/x-www-form-urlencoded')
assert response.status_code == 200
assert response_json(response) == {
'data': {'test': "Hello Dolly"}
}
3
Example 17
Project: watson-framework Source File: filters.py
def merge_query_string(obj, values):
"""Merges an existing dict of query string values and updates the values.
Args:
obj: The original dict
values: The new query string values
Example:
.. code-block:: python
# assuming ?page=2
request().get|merge_query_string({'page': 1}) # ?page=1
"""
qs_parts = dict(obj)
qs_parts.update(values)
return '?{0}'.format(parse.urlencode(qs_parts))
3
Example 18
Project: voyageavecmoi Source File: collect_dataset.py
def wikipedia_query(name):
title = urllib.parse.urlencode({'titles': name.replace(' ', '_')})
data = requests.get('https://fr.wikipedia.org/w/api.php?action=query&prop=revisions&rvlimit=1&rvprop=content&format=json&{}'.format(title)).json()
page = sorted(data['query']['pages'].items(), key=lambda x:int(x[0]))[-1][1]
if 'revisions' not in page:
raise PageDoesNotExist()
content = ''.join(page['revisions'][0]['*'])
redirections = list(redirect_pattern.finditer(content))
if redirections:
return wikipedia_query(redirections[0].group('target'))
return content
3
Example 19
Project: libmunin Source File: generate_genre_list.py
def wiki_get_page(name):
name = name.replace(' ', '_').encode('utf8')
params = {
'title': name,
'action': 'raw',
}
url = "{}?{}".format(BASE_URL, urlencode(params))
return urlopen(url).read().decode('utf8')
3
Example 20
def test_post(self):
params = urllib.parse.urlencode(
{'spam' : 1, 'eggs' : 'python', 'bacon' : 123456})
headers = {'Content-type' : 'application/x-www-form-urlencoded'}
res = self.request('/cgi-bin/file2.py', 'POST', params, headers)
self.assertEqual(res.read(), b'1, python, 123456' + self.linesep)
3
Example 21
def test_urlencode_sequences(self):
# Other tests incidentally urlencode things; test non-covered cases:
# Sequence and object values.
result = urllib.parse.urlencode({'a': [1, 2], 'b': (3, 4, 5)}, True)
# we cannot rely on ordering here
assert set(result.split('&')) == {'a=1', 'a=2', 'b=3', 'b=4', 'b=5'}
class Trivial:
def __str__(self):
return 'trivial'
result = urllib.parse.urlencode({'a': Trivial()}, True)
self.assertEqual(result, 'a=trivial')
3
Example 22
Project: feedy Source File: social_share.py
async def _get_hatena_info(url):
base_url = 'http://b.hatena.ne.jp/entry/json/?'
params = [
('url', url),
]
async with aiohttp.ClientSession() as session:
async with session.get(base_url + parse.urlencode(params)) as response:
res = await response.json()
return {
'hatebu_count': res.get('count') if res else None,
'hatebu_related': res.get('related') if res else None,
'hatebu_bookmarks': res.get('bookmarks') if res else None,
'hatebu_screenshot': res.get('screenshot') if res else None,
}
3
Example 23
def load_more(self, link_name, more_children, callback):
'''
Args:
link_name (str): fullname of the link
more_children (dict): more comments object reddit gave you
callback (func): same kind of callback as rest of api
'''
data = urllib.parse.urlencode({
'api_type': 'json',
'children': ','.join(more_children['children']),
'link_id': link_name
})
return self.send_request('GET', '/api/morechildren?' + data,
self.__load_more_cb, user_data=callback)
3
Example 24
Project: putio.py Source File: putio.py
@property
def authentication_url(self):
"""Redirect your users to here to authenticate them."""
params = {
'client_id': self.client_id,
'response_type': self.type,
'redirect_uri': self.callback_url
}
return AUTHENTICATION_URL + "?" + urlencode(params)
3
Example 25
Project: CloudBot Source File: chatbot.py
def cb_think(text):
SESSION['stimulus'] = text
payload = urllib.parse.urlencode(SESSION)
digest = hashlib.md5(payload[9:35].encode('utf-8')).hexdigest()
target_url = "{}&icognocheck={}".format(payload, digest)
parsed = sess.post(API_URL, data=target_url, headers=HEADERS)
data = parsed.text.split('\r')
SESSION['sessionid'] = data[1]
if parsed.status_code == 200:
return html.unescape(str(data[0]))
else:
print("CleverBot API Returned "+str(parsed.status_code))
return "Error: API returned "+str(parsed.status_code)
3
Example 26
Project: mock-django Source File: tests.py
def test__set_raw_post_data(self):
wsgi_r = WsgiHttpRequest()
wsgi_r._set_raw_post_data('')
self.assertEqual({}, wsgi_r.POST)
self.assertEqual(urlencode({}), wsgi_r._raw_post_data)
3
Example 27
def RetrieveAuthorizationToken(self, req, level):
from tapiriik.services import Service
# might consider a real OAuth client
code = req.GET.get("code")
params = {"grant_type": "authorization_code", "code": code, "client_id": RUNKEEPER_CLIENT_ID, "client_secret": RUNKEEPER_CLIENT_SECRET, "redirect_uri": WEB_ROOT + reverse("oauth_return", kwargs={"service": "runkeeper"})}
response = requests.post("https://runkeeper.com/apps/token", data=urllib.parse.urlencode(params), headers={"Content-Type": "application/x-www-form-urlencoded"})
if response.status_code != 200:
raise APIException("Invalid code")
token = response.json()["access_token"]
# This used to check with GetServiceRecordWithAuthDetails but that's hideously slow on an unindexed field.
uid = self._getUserId(ServiceRecord({"Authorization": {"Token": token}})) # meh
return (uid, {"Token": token})
3
Example 28
Project: Polyglot Source File: __init__.py
def make_url(ns_profnum, path, path_args=None):
'''
Create a URL from the given path.
:param ns_profnum: Node Server ID
:param path: List or subdirectories in path.
:param path_args: Dictionary of arguments to add to the path.
'''
url = '{}://{}:{}/rest/ns/{}/'.format(HTTPS, ADDRESS, PORT, ns_profnum)
url += '/'.join([quote(str(item)) for item in path if item is not None])
if path_args is not None:
if len(path_args) > 0:
url += '?{}'.format(urlencode(path_args))
return url
3
Example 29
Project: sanction Source File: __init__.py
def transport_query(url, access_token, data=None, method=None, headers=None):
parts = urlsplit(url)
query = dict(parse_qsl(parts.query))
query.update({
'access_token': access_token
})
url = urlunsplit((parts.scheme, parts.netloc, parts.path,
urlencode(query), parts.fragment))
try:
req = Request(url, data=data, method=method)
except TypeError:
req = Request(url, data=data)
req.get_method = lambda: method
if headers is not None:
req.headers.update(headers)
return req
3
Example 30
def synccheck(self):
params = {
'r': int(time.time()),
'sid': self.sid,
'uin': self.uin,
'skey': self.skey,
'deviceid': self.deviceId,
'synckey': self.synckey,
'_': int(time.time()),
}
url = 'https://' + self.syncHost + '/cgi-bin/mmwebwx-bin/synccheck?' + parse.urlencode(params)
data = self._get(url)
pm = re.search(r'window.synccheck={retcode:"(\d+)",selector:"(\d+)"}', data)
retcode = pm.group(1)
selector = pm.group(2)
return [retcode, selector]
3
Example 31
Project: osrc Source File: frontend.py
@frontend.route("/optout/<username>/login")
def optout_login(username):
state = "".join([random.choice(string.ascii_uppercase + string.digits)
for x in range(24)])
flask.session["state"] = state
params = dict(
client_id=flask.current_app.config["GITHUB_ID"],
redirect_uri=flask.url_for(".optout_callback", username=username,
_external=True),
state=state,
)
return flask.redirect("https://github.com/login/oauth/authorize?{0}"
.format(urlencode(params)))
3
Example 32
def url_concat(url, args):
"""Concatenate url and argument dictionary regardless of whether
url has existing query parameters.
>>> url_concat("http://example.com/foo?a=b", dict(c="d"))
'http://example.com/foo?a=b&c=d'
"""
if not args:
return url
if url[-1] not in ('?', '&'):
url += '&' if ('?' in url) else '?'
return url + urlencode(args)
3
Example 33
Project: feedhq Source File: utils.py
def remove_utm_tags(guid):
parts = list(urlsplit(guid))
qs = parse_qs(parts[3]) # [3] is query component
filtered = sorted([(k, v) for k, v in qs.items()
if not k.startswith('utm_')])
parts[3] = urlencode(filtered, doseq=True)
return urlunsplit(parts)
3
Example 34
Project: ava Source File: utils.py
def get_signin_url():
# Build the query parameters for the signin URL.
params = {'client_id': settings.OFFICE365_OAUTH2_CLIENT_ID,
'redirect_uri': settings.OFFICE365_OAUTH2_REDIRECT_URL,
'response_type': 'code'
}
signin_url = authorize_url.format(urlencode(params))
return signin_url
3
Example 35
Project: flask-graphql Source File: test_graphqlview.py
def test_allows_post_with_url_encoding(client):
response = client.post(url_string(), data=urlencode(dict(query='{test}')), content_type='application/x-www-form-urlencoded')
assert response.status_code == 200
assert response_json(response) == {
'data': {'test': "Hello World"}
}
3
Example 36
def api_request(self, uri, params=None):
url = "%s/api/v1/%s" % (self.hound_url, uri)
if params:
data = urllib.parse.urlencode(params)
data = data.encode('utf-8') # data should be bytes
else:
data = None
req = urllib.request.urlopen(urllib.request.Request(url, data, headers=self.custom_headers))
encoding = req.headers.get_content_charset()
response_data = req.read().decode(encoding)
if self.debug:
logger.debug("API response: %s" % response_data)
return json.loads(response_data)
3
Example 37
Project: django-dynamic-scraper Source File: task_utils.py
def _run_spider(self, **kwargs):
param_dict = {
'project': 'default',
'spider': kwargs['spider'],
'id': kwargs['id'],
'run_type': kwargs['run_type'],
'do_action': kwargs['do_action']
}
params = urllib.parse.urlencode(param_dict)
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = http.client.HTTPConnection("localhost:6800")
conn.request("POST", "/schedule.json", params, headers)
conn.getresponse()
3
Example 38
def url_concat(url, args):
"""Concatenate url and arguments regardless of whether
url has existing query parameters.
``args`` may be either a dictionary or a list of key-value pairs
(the latter allows for multiple values with the same key.
>>> url_concat("http://example.com/foo", dict(c="d"))
'http://example.com/foo?c=d'
>>> url_concat("http://example.com/foo?a=b", dict(c="d"))
'http://example.com/foo?a=b&c=d'
>>> url_concat("http://example.com/foo?a=b", [("c", "d"), ("c", "d2")])
'http://example.com/foo?a=b&c=d&c=d2'
"""
if not args:
return url
if url[-1] not in ('?', '&'):
url += '&' if ('?' in url) else '?'
return url + urlencode(args)
3
Example 39
def _build_fs(self, link, cursor = None):
self.project.log("transaction", "Calculating total dropbox items...", "info", True)
if cursor:
response = Common.webrequest(link, self.oauth_provider.get_auth_header(), self.oauth_provider.http_intercept, urllib.parse.urlencode({'cursor': cursor}))
else:
response = Common.webrequest(link, self.oauth_provider.get_auth_header(), self.oauth_provider.http_intercept, "")
json_response = json.loads(response)
has_more = json_response['has_more']
cursor = json_response['cursor']
for item in json_response['entries']:
self.files.append(item[1])
if has_more:
self._build_fs(link, cursor)
3
Example 40
@app.route('/bench')
def bench_test():
total = int(request.args.get('total', 10000))
show = int(request.args.get('show', 20))
nlist = [random.randint(1, total) for _ in range(show)]
result = []
result.append("<html><head></head><body>")
args = dict(request.args)
for nl in nlist:
args['n'] = nl
argstr = urlencode(sorted(args.items()), doseq=True)
result.append("<a href='/bench?{0}'>follow {1}</a><br>".format(argstr, nl))
result.append("</body></html>")
return "".join(result)
3
Example 41
def _get(self, path, callback=None, params=None, model_class=None):
if params:
path += '?' + urlencode(params)
return self._request('GET',
path=path,
callback=callback,
model_class=model_class)
3
Example 42
Project: pyembed Source File: consumer.py
def __format_url(oembed_url, max_width=None, max_height=None):
scheme, netloc, path, query_string, fragment = urlsplit(oembed_url)
query_params = parse_qsl(query_string)
if max_width is not None:
query_params.append(('maxwidth', max_width))
if max_height:
query_params.append(('maxheight', max_height))
new_query_string = urlencode(query_params, doseq=True)
return urlunsplit((scheme, netloc, path, new_query_string, fragment))
3
Example 43
def simplePost(url, POST={}):
try:
import urllib.parse, urllib.request
data = urllib.parse.urlencode(POST)
return urllib.request.urlopen(url, data).read()
except ImportError:
import urllib, urllib2
data = urllib.urlencode(POST)
return urllib2.urlopen(urllib2.Request(url, data)).read()
3
Example 44
Project: braintree_python Source File: test_helper.py
@staticmethod
def simulate_tr_form_post(post_params, url=TransparentRedirect.url()):
form_data = urlencode(post_params)
conn = HTTPConnection(Configuration.environment.server_and_port)
conn.request("POST", url, form_data, TestHelper.__headers())
response = conn.getresponse()
query_string = response.getheader("location").split("?", 1)[1]
conn.close()
return query_string
3
Example 45
@staticmethod
def request(url, params=None, on_complete=None):
"""
Send the HTTP GET request and returns data from specified url
@param url: url to send the request
@param params: parameters to pass to the url
@param on_complete: a callback function
if provided, method will become an async task and will callback when
request is complete
"""
params = params or {}
if params:
url += "?" + urllib.parse.urlencode(params)
return Downloader.download(url, on_complete=on_complete)
3
Example 46
def build_uri(self, path, query_params):
'''
Build the URI for the API call.
'''
url = 'https://api.trello.com/1' + self.clean_path(path)
url += '?' + urlencode(query_params)
return url
3
Example 47
@staticmethod
def environ(path, params):
return {
'PATH_INFO': path,
'QUERY_STRING': urlencode(params),
}
3
Example 48
def auth_redirect(self):
"""The URL redirect logic extracted from tornado.web.authenticated."""
url = self.get_login_url()
if '?' not in url:
if urlparse.urlsplit(url).scheme: # pragma: no cover
next_url = self.request.full_url()
else:
next_url = self.request.uri
url += '?' + urlencode({'next': next_url})
self.redirect(url)
return
3
Example 49
Project: inspectors-general Source File: dod.py
def urls_for(options, only):
year_range = inspector.year_range(options, archive)
for office in only:
# there's always a first year, and it defaults to current year
params = {}
params['searchdate1'] = '01/01/%s' % year_range[0]
params['searchdate2'] = '12/31/%s' % year_range[-1] # could be the same year
params['office'] = OFFICES[office]
params['sort'] = 'report_number'
params['order'] = 'desc'
query_string = urlencode(params)
url = '{0}?{1}'.format(BASE_URL, query_string)
yield url
page = utils.beautifulsoup_from_url(url)
for url in get_pagination_urls(page):
yield url
3
Example 50
Project: pilbox Source File: app_test.py
def _assert_expected_case(self, case):
qs = urlencode(case["source_query_params"])
resp = self.fetch_success("/?%s" % qs)
msg = "/?%s does not match %s" \
% (qs, case["expected_path"])
if case["content_type"]:
self.assertEqual(resp.headers.get("Content-Type", None),
case["content_type"])
with open(case["expected_path"], "rb") as expected:
self.assertEqual(resp.buffer.read(), expected.read(), msg)