def build_http_request(self): params = self.build_parameters() query_string = urlencode(params.get_parameters()) query_string = query_string.replace('+', '%20') # Mimic Javascript's encodeURIComponent() encoding for the query # string just to be sure we are 100% consistent with GA's Javascript client query_string = utils.convert_to_uri_component_encoding(query_string) # Recent versions of ga.js use HTTP POST requests if the query string is too long use_post = len(query_string) > 2036 if not use_post: url = '%s?%s' % (self.config.endpoint, query_string) post = None else: url = self.config.endpoint post = query_string headers = {} headers['Host'] = self.config.endpoint.split('/')[2] headers['User-Agent'] = self.user_agent or '' headers[ 'X-Forwarded-For'] = self.x_forwarded_for and self.x_forwarded_for or '' if use_post: # Don't ask me why "text/plain", but ga.js says so :) headers['Content-Type'] = 'text/plain' headers['Content-Length'] = len(query_string) logger.debug(url) if post: logger.debug(post) return urllib_request(url, post, headers)
def build_http_request(self): params = self.build_parameters() query_string = urllib.urlencode(params.get_parameters()) query_string = query_string.replace('+', '%20') # Mimic Javascript's encodeURIComponent() encoding for the query # string just to be sure we are 100% consistent with GA's Javascript client query_string = utils.convert_to_uri_component_encoding(query_string) # Recent versions of ga.js use HTTP POST requests if the query string is too long use_post = len(query_string) > 2036 if not use_post: url = '%s?%s' % (self.config.endpoint, query_string) post = None else: url = self.config.endpoint post = query_string headers = {} headers['Host'] = self.config.endpoint.split('/')[2] headers['User-Agent'] = self.user_agent headers['X-Forwarded-For'] = self.x_forwarded_for and self.x_forwarded_for or '' if use_post: # Don't ask me why "text/plain", but ga.js says so :) headers['Content-Type'] = 'text/plain' headers['Content-Length'] = len(query_string) logger.debug(url) if post: logger.debug(post) return urllib2.Request(url, post, headers)