可以使用API代替SDK吗?

huangapple go评论93阅读模式
英文:

Is it ok to use API instead of SDK?

问题

我喜欢快速的代码执行(因此我从Python转到了Go),而且我不喜欢依赖。亚马逊建议使用SDK进行简化的身份验证(但在Lambda中,我可以从环境变量中获取IAM的令牌),并且由于SDK内置了错误重试功能(只需几行代码,我认为很方便)。是的,使用SDK编写代码更快,但是相比使用SDK,纯HTTP API有哪些额外的注意事项呢?我是不是对毫秒级的优化过于痴迷了?这样的优化是否值得呢?

英文:

I like fast code execution (because of that I switched from Python to Go) and I do not like dependencies. Amazon recommends using SDK for simpler authentication (but in Lambda I can get tokens from IAM from environment variables) and because of built into SDK retry on errors (few lines of code, as I think). Yes it is faster to write my code using SDK, but what additional caveats about using pure HTTP API instead of SDK? Am I too crazy about milliseconds? Such optimizations worth it?

答案1

得分: 2

你在AWS上进行的任何操作都是通过API调用实现的,无论是通过CLI、Web控制台还是SDK执行。

SDK使与这些API的交互更加简便。虽然你可能能够对某些调用进行一些小的改进,但总体上你会花费很多时间,而获益却很少。

英文:

Anything you do with AWS is the result of an API call, whether executed by CLI, Web console, or SDK.

The SDKs make it easier to interact with those APIs. While you may be able to come up with some minor improvements for some calls, overall you will spend a lot of time doing it to very little benefit.

答案2

得分: 0

我认为对性能的强调掩盖了真正的权衡。

考虑到有人需要维护你的代码——如果你使用一个API,测试范围很小,但是AWS的API可能会发生变化或被弃用;如果你使用一个SDK,下一个程序员将会插入新的SDK版本并希望它能正常工作,但如果不能正常工作,他们将会被SDK的庞大体量所拖累。

同样,想象一下有人需要对这个应用进行安全审查,或者引入一些SDK尚未涵盖的内容(比如将调用者角色的会计组传递到底层存储)。

我认为没有一个明确的答案。

以下是我的建议:

  • 保持一致——在给定的应用中选择使用API还是SDK
  • 考虑更大的图景(你计划编写多少个应用程序?)
  • 不要害怕以后切换到另一种方法

我过去曾经在使用Docker时做过类似的决策(Docker有更好的API和SDK/库)。以下是当时的情况:

对于测试,我们最终使用了Docker Python绑定的beta版本:正式版本不够用,而绑定(即SDK)整体上非常好用和清晰。

对于日志抓取,我使用了HTTP调用(即API),因为考虑到性能,实际上是在API和SDK之间进行比较时的心理负担,而且绑定(即SDK)不支持异步操作。

英文:

I think the stated focus on performance belies real trade-offs.

Consider that someone will have to maintain your code -- if you use an API, the test area is small, but AWS APIs might change or be deprecated; if you an SDK, next programmer will plug in new SDK version and hope that it works, but if it doesn't they'd be bogged down by sheer weight of the SDK.

Likewise, imagine someone needs to do a security review of this app, or to introduce something not yet covered by SDK (let's imagine propagating accounting group from caller role to underlying storage).

I don't think there is a clear answer.

Here are my suggestions:

  • keep it consistent -- either API or SDK (within given app)
  • consider the bigger picture (how many apps do you plan to write?)
  • don't be afraid to switch to the other approach later

I've had to decide on something similar in the past, with Docker (much nicer APIs and SDKs/libs). Here's how it played out:

For testing, we ended up using beta version of Docker Python bindings: prod version was not enough, and bindings (your SDK) were overall pretty good and clear.

For log scraping, I used HTTP calls (your API), "because performance", in reality comparative mental load using API vs SDK, and because bindings (SDK) did not support asyncio.

huangapple
  • 本文由 发表于 2018年5月8日 05:10:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/50222478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定