开发者

Converting POST values to conform to different convention

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜