def _initHttpClient(): GOOGLE_PROXY = getConfig('google', 'proxy') if GOOGLE_PROXY: proxy_str = GOOGLE_PROXY else: proxy_str = None if not proxy_str: return Http() proxy = proxy_str.strip().split(' ') if len(proxy) != 3: msg = 'SyntaxError in GoogleProxy string, Please check your args or config file.' sys.exit(msg) if proxy[0].lower() == 'http': type = 3 elif proxy[0].lower() == 'sock5': type = 2 elif proxy[0].lower() == 'sock4': type = 1 else: msg = 'Invalid proxy-type in GoogleProxy string, Please check your args or config file.' sys.exit(msg) try: port = int(proxy[2]) except ValueError: msg = 'Invalid port in GoogleProxy string, Please check your args or config file.' sys.exit(msg) else: http_client = Http(proxy_info=ProxyInfo(type, proxy[1], port)) return http_client
def get_authorize_login_url(self, scope=None): http_object = Http() url = self._url_for_authorize(scope=scope) response, content = http_object.request(url) if response["status"] != "200": raise OAuth2AuthExchangeError("The server returned a non-200 response for URL %s" % url) redirected_to = response["content-location"] return redirected_to
def exchange_for_access_token(self, code=None, username=None, password=None, scope=None, user_id=None): data = self._data_for_exchange(code, username, password, scope=scope, user_id=user_id) http_object = Http() url = self.api.access_token_url response, content = http_object.request(url, method="POST", body=data) parsed_content = simplejson.loads(content) if int(response["status"]) != 200: raise OAuth2AuthExchangeError(parsed_content.get("message", "")) return parsed_content["access_token"]
def __init__(self, login, access_token, api_key): """ @param login: bit.ly username @param access_token: bit.ly user access token @param api_key: bit.ly user API key """ from lib.httplib2 import Http self.login = login self.access_token = access_token self.api_key = api_key self.connector = Http()
class api: """ Interface to the bit.ly API; incomplete, containing only what is needed by the socialfeeder application. """ shorten_url = "https://api-ssl.bitly.com/v3/shorten" base_url = "https://api-ssl.bitly.com/" def __init__(self, login, access_token, api_key): """ @param login: bit.ly username @param access_token: bit.ly user access token @param api_key: bit.ly user API key """ from lib.httplib2 import Http self.login = login self.access_token = access_token self.api_key = api_key self.connector = Http() def shorten(self, url): """ Shortens an URL with the given bit.ly account @param url: URL to shorten @return: shortened URL, or full URL on failure """ from urllib import urlencode data = { "format":"json", "longUrl":url, "login":self.login, "apiKey":self.api_key } resp, content = self.connector.request(self.shorten_url + "?" + urlencode(data), "GET") if resp['status'] == '200': from django.utils import simplejson as json info = json.loads(content) if str(info['status_code']) == "200": if info.has_key('data'): return info['data']['url'] else: return info['url'] else: logging.error("Bit.ly failed for some reason" + str(info)) logging.error("Bit.ly error detected " + str(resp)) return url
def make_request(self, url, method="GET", body=None, headers={}): if not "User-Agent" in headers: headers.update({"User-Agent": "%s Python Client" % self.api.api_name}) http_obj = Http() return http_obj.request(url, method, body=body, headers=headers)