为 REST API 编写包装器的指南 [英] Guidance for writing a wrapper for a REST API

查看:31
本文介绍了为 REST API 编写包装器的指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些非常随意的 REST 包装和结构性较差的 Web 界面,但都只是为了好玩,很少关注错误检测和处理、超时等.

I've written a few very casual wrappers around REST and less structured web interfaces, but all just for fun, with very little attention to error detection and handling, timeouts, etc.

有人可以就实践或资源给我一些指导,以便为 REST API 开发可靠、专业的 .NET(或其他平台)包装器吗?

Can somebody please give me some pointers, either on practices, or to resources, for developing a solid, professional .NET (or other platform) wrapper for a REST API?

我想到的事情是:

  1. 解释 HTTP 错误代码并从 API 错误中过滤协议错误.
  2. 构建基于 URL 的请求,可能使用模式匹配和令牌替换.
  3. 将回传与请求相匹配.
  4. 如何将 OO 模型映射到 REST 模型.一些请求适合静态方法,例如获取列表,而其他人则处于静态和实例之间,也许是包含所有静态方法的普通提供者模式?

新:从那以后,我发现这几乎太基本了进行 Yahoo REST 调用的指南,但它可以作为起点.

NEW: I've since found this almost too basic guide to making Yahoo REST calls, but it serves as a starting point.

重要提示:一个非常复杂且很少涉及的方面是如何处理 REST API 上的表单身份验证.单独的登录请求、cookie 存储等

推荐答案

尽管我已经以 OOP 风格的方式包装了无数 Web 服务,但我发现很难为您的问题找到一个好的答案.

Although I have wrapped countless web services in an OOP-ish way, I find it difficult to come up with a good answer to your question.

可以在此处找到几个 Yahoo API 示例:http://curlobjects.com/trac/browser/trunk/lib/YahooApi

A couple of Yahoo API examples can be found here: http://curlobjects.com/trac/browser/trunk/lib/YahooApi

1 从 API 中解释 HTTP 错误代码和过滤协议错误错误.

1 Interpreting HTTP error codes and filtering protocol errors from API errors.

我的基本 http 类有一个可以覆盖的 http 错误回调.

My base http class has an http error callback that can be overriden.

2 构建基于 URL 的请求,可能使用模式匹配和代币替换.

2 Building URL based requests, possibly with pattern matching and token substitution.

我更喜欢字符串连接.

4 如何将 OO 模型映射到 REST 模型.一些要求适合静态方法,例如获取列表,以及其他人挂在中间静态和实例,也许是一个普通的提供者模式与所有静态方法?

4 How to map an OO model to the REST model. Some requests suit static methods, e.g. getting lists, and others hang in the middle between static and instance, maybe a plain provider pattern with all static methods?

有时你需要一个大的静态工厂/提供者,有时你需要为每个方法一个类,有时为每组相关方法一个类......确实没有一种正确的方法来解决这个问题.我总是花一些时间来了解服务的结构,然后尝试找到一种优雅的方式来抽象重复的部分.

Sometime you need one big static factory/provider, sometimes you want a class for each method, sometimes a class for each group of related methods... There really isn't one proper way to go about this. I always spend some time getting a feel of the service's structure and then try to find an elegant way to abstract the repetitive parts.

如果您对登录表单、cookies 等有任何更具体的问题,我很乐意回答.

If you have any more specific questions regarding login forms, cookies, etc, I'd be glad to answer them.

提到了 HttpWebRequest,我猜你要么需要设置 ClientCertificate 属性 并正确使用 SSL,或者您可以忽略带有 CertificatePolicy 的所有 SLL 错误.

HttpWebRequest was mentioned and I'm guessing you either need to set the ClientCertificate property and use SSL properly, or you can ignore all SLL errors with a CertificatePolicy.

这篇关于为 REST API 编写包装器的指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆