From df29a93a6120aa421c79b5b82311db78a196036f Mon Sep 17 00:00:00 2001 From: Luke Faraone Date: Fri, 6 Dec 2013 16:37:01 -0500 Subject: [PATCH] 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=", 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) --- zulip/__init__.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/zulip/__init__.py b/zulip/__init__.py index c5fcfc7..3b13c3d 100644 --- a/zulip/__init__.py +++ b/zulip/__init__.py @@ -28,6 +28,7 @@ import urlparse import sys import os import optparse +import platform from distutils.version import LooseVersion from ConfigParser import SafeConfigParser @@ -112,9 +113,25 @@ class Client(object): self.retry_on_errors = retry_on_errors 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): request = {} - request["client"] = self.client_name for (key, val) in orig_request.iteritems(): if not (isinstance(val, str) or isinstance(val, unicode)): @@ -164,6 +181,7 @@ class Client(object): auth=requests.auth.HTTPBasicAuth(self.email, self.api_key), verify=True, timeout=90, + headers={"User-agent": self.get_user_agent()}, **kwargs) # On 50x errors, try again after a short sleep