api: Provide an informative User-agent instead of a client parameter

We previously sent our client type to the server as a GET/POST parameter
of "client=<something>", most commonly "client=API: Python".

We switch here to providing the same information as a User-agent header
sent on each request, which is more standards-compliant.

Also added is some data about the platform the user is using. If your
client string was set to "MyLittleZulip/1.0", the resultant string could
look something like this:
    MyLittleZulip/1.0 (Ubuntu; 12.04)

(imported from commit 39fd187a8f9d4b3c9b63fc623e0836e57a4099ca)
This commit is contained in:
Luke Faraone 2013-12-06 16:37:01 -05:00
parent 6478da983e
commit df29a93a61

View file

@ -28,6 +28,7 @@ import urlparse
import sys import sys
import os import os
import optparse import optparse
import platform
from distutils.version import LooseVersion from distutils.version import LooseVersion
from ConfigParser import SafeConfigParser from ConfigParser import SafeConfigParser
@ -112,9 +113,25 @@ class Client(object):
self.retry_on_errors = retry_on_errors self.retry_on_errors = retry_on_errors
self.client_name = client self.client_name = client
def get_user_agent(self):
vendor = platform.system()
vendor_version = platform.release()
if vendor == "Linux":
vendor, vendor_version, dummy = platform.linux_distribution()
elif vendor == "Windows":
vendor_version = platform.win32_ver()[1]
elif vendor == "Darwin":
vendor_version = platform.mac_ver()[0]
return "{client_name} ({vendor}; {vendor_version})".format(
client_name=self.client_name,
vendor=vendor,
vendor_version=vendor_version,
)
def do_api_query(self, orig_request, url, method="POST", longpolling = False): def do_api_query(self, orig_request, url, method="POST", longpolling = False):
request = {} request = {}
request["client"] = self.client_name
for (key, val) in orig_request.iteritems(): for (key, val) in orig_request.iteritems():
if not (isinstance(val, str) or isinstance(val, unicode)): if not (isinstance(val, str) or isinstance(val, unicode)):
@ -164,6 +181,7 @@ class Client(object):
auth=requests.auth.HTTPBasicAuth(self.email, auth=requests.auth.HTTPBasicAuth(self.email,
self.api_key), self.api_key),
verify=True, timeout=90, verify=True, timeout=90,
headers={"User-agent": self.get_user_agent()},
**kwargs) **kwargs)
# On 50x errors, try again after a short sleep # On 50x errors, try again after a short sleep