如何:编写 Python API 包装器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/517237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:16:54  来源:igfitidea点击:

HOWTO: Write Python API wrapper?

pythonweb-servicesapirest

提问by hanksims

I'd like to write a python library to wrap a REST-style API offered by a particular Web service. Does anyone know of any good learning resources for such work, preferably aimed at intermediate Python programmers?

我想编写一个 python 库来包装由特定 Web 服务提供的 REST 风格的 API。有没有人知道此类工作有什么好的学习资源,最好是针对中级 Python 程序员的?

I'd like a good article on the subject, but I'd settle for nice, clear code examples.

我想要一篇关于这个主题的好文章,但我会满足于漂亮、清晰的代码示例。

CLARIFICATION:What I'm looking to do is write a Python client to interact with a Web service -- something to construct HTTP requests and parse XML/JSON responses, all wrapped up in Python objects.

澄清:我想要做的是编写一个 Python 客户端来与 Web 服务交互——构建 HTTP 请求和解析 XML/JSON 响应的东西,所有这些都包含在 Python 对象中。

回答by Ricardo Reyes

I can't point you to any article on how to do it, but I think there are a few libraries that can be good models on how to design your own.

我无法向您指出任何关于如何做到这一点的文章,但我认为有一些库可以成为如何设计自己的很好的模型。

PyAwsfor example. I didn't see the source code so I can't tell you how good it is as code example, but the features and the usage examples in their website should be a useful design model

例如PyAws。我没有看到源代码所以我不能告诉你它作为代码示例有多好,但是他们网站上的功能和使用示例应该是一个有用的设计模型

Universal Feed Parseris not a wrapper for a webservice (it's an RSS parser library), but it's a great example of a design that prioritizes usage flexibility and hiding implementation details. I think you can get very good usage ideas for your wrapper there.

Universal Feed Parser不是 Web 服务的包装器(它是一个 RSS 解析器库),但它是优先考虑使用灵活性和隐藏实现细节的设计的一个很好的例子。我认为您可以在那里为您的包装器获得非常好的使用建议。

回答by Koen Bok

My favorite combination is httplib2 (or pycurl for performance) and simplejson. As REST is more "a way of design" then a real "protocol" there is not really a reusable thing (that I know of). On Ruby you have something like ActiveResource. And to be honest, even that would just expose some tables as a webservice, whereas the power of xml/json is that they are more like "views" that can contain multiple objects optimized for your application. I hope this makes sense :-)

我最喜欢的组合是 httplib2(或 pycurl 以提高性能)和 simplejson。由于 REST 更像是“一种设计方式”,而不是真正的“协议”,因此并没有真正可重用的东西(我知道)。在 Ruby 上,你有类似 ActiveResource 的东西。老实说,即使那样也只会将一些表公开为 Web 服务,而 xml/json 的强大之处在于它们更像是“视图”,可以包含针对您的应用程序优化的多个对象。我希望这是有道理的 :-)

回答by Koen Bok

This tutorial pagecould be a good starting place (but it doesn't contain everything you need).

本教程页面可能是一个很好的起点(但它不包含您需要的所有内容)。

回答by Dan Lorenc

You should take a look at PyFacebook. This is a python wrapper for the Facebook API, and it's one of the most nicely done API's I have ever used.

你应该看看 PyFacebook。这是 Facebook API 的 Python 包装器,它是我用过的最好的 API 之一。

回答by Christopher Scott

You could checkout pythenic jobs, a nice, simple, but well-formed "Python wrapper around the Authentic Jobs ... API" as a good example. That's what I'm doing now :)

您可以查看 pythenic jobs,这是一个很好的、简单的但格式良好的“围绕 Authentic Jobs ... API 的 Python 包装器”作为一个很好的例子。这就是我现在正在做的:)