转换 POST 值以符合不同的约定
标题有点混乱,抱歉。
我正在我的网站上收集大量数据,最终将通过 cURL
将这些数据发送到另一个网站的 API。我可以让一切在技术方面正常工作,但我遇到的最大麻烦是语义。
我根本不喜欢其他网站为其 POST
值使用的命名约定 [大概还有它的数据库结构。]
我的网站
book-title => Book of Thieves
bind-type => wire //enum value
shipping-address-1 => 123 Chesterfield Road
shipping-address-2 => Apt. 204
远程站点
Title => Book of Thieves
BindType => wire-bind //enum value
Shipping1 => 123 Chesterfield Road
Shipping2 => Apt. 204
我的问题是:在内部利用我自己喜欢的数据结构的最佳方法是什么,但是当需要将其POST
到远程站点的API时,将数据转换为他们已经实施了哪些公约?这不仅包括键,还包括值(某些值是预定义的,而不是用户输入的。)
谢谢!
Title is kinda confusing, sorry.
I'm collecting a bunch of data on my website that I will eventually be sending over to another website's API via cURL
. I can make everything work on the technical side of things, but the biggest trouble I'm having is with semantics.
I'm not a fan [at all] of the naming conventions the other website uses for its POST
values [and, presumably, its DB structure.]
My Site
book-title => Book of Thieves
bind-type => wire //enum value
shipping-address-1 => 123 Chesterfield Road
shipping-address-2 => Apt. 204
Remote Site
Title => Book of Thieves
BindType => wire-bind //enum value
Shipping1 => 123 Chesterfield Road
Shipping2 => Apt. 204
My question is: What is the best way to utilize my own preferred data structure internally, but when it comes time to POST
it to the Remote Site's API, convert the data to the conventions that they have implemented? This includes not only the keys, but the values as well (some values are predefined and not user input.)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从模式的角度来看,我会考虑封装到一个类中,该类完全负责与远程站点的交互(没有其他责任)。该领域有多种模式——委托、外观、演示者——但基本逻辑是相同的。
因此,您可以创建一个 TheirBook 类来包装 YourBook 并以其网站所需的形式返回方法。
或者您可以创建一个 PostToTheirSite 类,它接受 YourBook 但使用其语义直接生成 POST 请求。
无论哪种情况,您都会生成一段专用代码,该代码仅负责语义翻译,而不负责任何其他形式的业务逻辑。
From a pattern perspective, I would consider encapsulating into a class that takes full responsibility for interaction with your remote site (with no other responsibilities). There's a variety of patterns that are in that realm--Delegate, Facade, Presenter--but the basic logic would be the same.
So you could create a TheirBook class that wraps YourBook and returns the methods in the form that their site requires.
Or you could create a PostToTheirSite class that takes YourBook but directly generates the POST request using thier semantics.
In either case you're generating one dedicated peice of code that is responsible only for semantic translation and not any other form of business logic.