转换 POST 值以符合不同的约定

发布于 2024-11-17 17:20:13 字数 703 浏览 1 评论 0原文

标题有点混乱,抱歉。

我正在我的网站上收集大量数据,最终将通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫弦 2024-11-24 17:20:13

从模式的角度来看,我会考虑封装到一个类中,该类完全负责与远程站点的交互(没有其他责任)。该领域有多种模式——委托、外观、演示者——但基本逻辑是相同的。

因此,您可以创建一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文