使用 类 为 API 创建一个 python 包装器

Creating a python wrapper for an API using classes

我正在尝试为 API 制作一个 Python 包装器。我已经能够创建运行良好但不使用 类 的脚本。我想使用 类 对 API 进行真正的包装。我是 Python.

的 OOP 新手

以下是我的尝试,但我对如何将其转换为 OO 类型感到困惑。

import urllib2
from urllib import urlencode
import json

class apiclient:
    def __init__(self,
                request_url,
                hmh_api_key,
                client_id,
                grant_type="password",
                username="username",
                password="password"):

        values = {
                "client_id": client_id,
                "grant_type": grant_type,
                "username": username,
                "password": password
            }

        data = urlencode(values)

        req = urllib2.Request(request_url, data)
        req.add_header("Api-Key", api_key)
        response = urllib2.urlopen(req)

        response_header = response.info().dict
        response_body = response.read()
        json_acceptable_string = response_body.replace("'", "\"")
        response_body_dict = json.loads(json_acceptable_string)

        return response_body_dict ## this is the response

if __name__ == "__main__":

    API_KEY = "75b5cc58a5cdc0a583f91301cefedf0c"
    CLIENT_ID = "ef5f7a03-58e8-48d7-a38a-abbd2696bdb6.hmhco.com"
    REQUEST_URL = "http://some.url"

    client = apiclient(request_url=REQUEST_URL,
                        api_key=API_KEY,
                        client_id=CLIENT_ID)

    print client

没有 类,我得到 JSON 作为 response_body_dict 的响应,但是有 类 我得到 TypeError: __init__() should return None。我应该如何开始设计我的程序。 我只展示了整个程序的一部分,有很多类似的脚本可以向 URL 发送请求并获得 JSON 响应。

谢谢!

你不应该 return 来自 __init__ 函数的东西。

编辑:

如果您需要该值,您应该使用 response_body_dict 作为 class 成员并通过其他方法获取他:

import urllib2
from urllib import urlencode
import json

class apiclient:
    def __init__(self,
                request_url,
                api_key,
                client_id,
                grant_type="password",
                username="username",
                password="password"):

        values = {
                "client_id": client_id,
                "grant_type": grant_type,
                "username": username,
                "password": password
            }

        data = urlencode(values)

        req = urllib2.Request(request_url, data)
        req.add_header("Api-Key", api_key)
        response = urllib2.urlopen(req)

        response_header = response.info().dict
        response_body = response.read()
        json_acceptable_string = response_body.replace("'", "\"")
        self.response_body_dict = json.loads(json_acceptable_string)

    def get_response_body(self):
        return self.response_body_dict

if __name__ == "__main__":

    API_KEY = "75b5cc58a5cdc0a583f91301cefedf0c"
    CLIENT_ID = "ef5f7a03-58e8-48d7-a38a-abbd2696bdb6.hmhco.com"
    REQUEST_URL = "http://some.url"

    client = apiclient(request_url=REQUEST_URL,
                        api_key=API_KEY,
                        client_id=CLIENT_ID)
    response = client.get_response_body()

    print client