设计网站 api 的接口
好的,我正在编写一种与 Grooveshark (http://grooveshark.com) 交互的方法。现在我有一个 Grooveshark 类和几个方法,一个获取与服务器的会话,另一个获取基于会话的令牌,另一个用于构造对服务器的 api 调用(其他方法使用它)。现在我像这样使用它......注意在twisted中使用twisted和tidefer
g = Grooveshark()
d = g.get_session()
d.addCallback(lambda x: g.get_token())
## and then something like.... ##
g.search("Song")
我发现这种unpythonic和丑陋的感觉即使在初始化类之后你也必须首先调用两个方法,否则其他方法将不起作用。为了解决这个问题,我试图获取它,以便创建 api 调用的方法能够处理会话和令牌。目前,这两个方法(会话和令牌方法)设置类变量并且不返回任何内容(或者无)。所以我的问题是,在与需要令牌和会话的网站交互时是否使用通用设计?此外,令牌和会话是从服务器检索的,因此我无法让它们在 init 方法中运行(因为它会阻塞或在进行 api 调用之前可能无法完成)
Ok I am programing a way to interface with Grooveshark (http://grooveshark.com). Right now I have a class Grooveshark and several methods, one gets a session with the server, another gets a token that is based on the session and another is used to construct api calls to the server (and other methods use that). Right now I use it like so.... Note uses twisted and t.i.defer in twisted
g = Grooveshark()
d = g.get_session()
d.addCallback(lambda x: g.get_token())
## and then something like.... ##
g.search("Song")
I find this unpythonic and ugly sense even after initializing the class you have to call two methods first or else the other methods won't work. To solve this I am trying to get it so that the method that creates api calls takes care of the session and token. Currently those two methods (the session and token methods) set class variables and don't return anything (well None). So my question is, is there a common design used when interfacing with sites that require tokens and sessions? Also the token and session are retrieved from a server so I can't have them run in the init method (as it would either block or may not be done before a api call is made)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果是这样,那么为什么不将
get_session
部分放入类的__init__
中呢?如果它总是必须在其他任何事情之前执行,那似乎是有道理的。当然,这意味着调用该类将仍然返回一个尚不可用的实例——这对于异步、事件驱动编程来说是不可避免的......你不会“阻塞,直到实例被调用”。准备使用”。一种可能是在调用类时将要执行的回调作为参数传递给该类;一个更 Twisted 正常的方法是让 Grooveshark 成为一个返回延迟的函数(您将向延迟添加要执行的回调,并在该实例时使用该实例作为参数来调用它)终于可以使用了)。
If so, then why not put the
get_session
part in your class's__init__
? If it always must be performed before anything else, that would seem to make sense. Of course, this means that calling the class will still return a yet-unusable instance -- that's kind of inevitable with asynchronous, event-drive programming... you don't "block until the instance is ready for use".One possibility would be to pass the callback to perform as an argument to the class when you call it; a more Twisted-normal one would be to have
Grooveshark
be a function which returns a deferred (you'll add to the deferred the callback to perform, and call it with the instance as the argument when that instance is finally ready to be used).我强烈建议您查看 Facebook 图表 API。仅仅因为您需要会话和一些身份验证并不意味着您可以构建干净的 REST API。 Facebook 使用 OAuth 来处理身份验证,但还有其他可能性。
I would highly recommend looking at the Facebook graph API. Just because you need sessions and some authentication doesn't mean you can build a clean REST API. Facebook uses OAuth to handle the authentication but there are other possibilities.