英文:
Should RESTful service in Golang include Client interface?
问题
如果我在Golang中开发Booking REST服务(即在booking包中),是否符合"GO方式"创建BookingClient接口(由结构体支持),允许进行业务操作,以便我的RESTful服务的客户端使用BookingClient(从booking包导入)而不是直接发送HTTP请求?
英文:
If I develop Booking REST service in Golang (i.e., in package booking). Is it a "GO way" to create BookingClient interface (backed up by struct) with business operations allowed, so that clients of my restful service would use BookingClient (imported from package booking) instead of sending http requests directly?
答案1
得分: 2
一般来说,如果你提供一个特定语言的客户端,那只是为了方便一些用户更容易地使用你的API。当然,这前提是你的客户端设计得很好。我不会仅仅提供一个Go语言的接口来表示一组可能的API调用。这只对非常有限的受众有益,可能只适用于开发自己的API客户端的人,而且他们使用的编程语言恰好与你的服务器实现相同。即使是这样,他们可能也不会真的喜欢使用接口(例如,他们可能只需要一组特定的方法)。
如果你想为你的API提供一个客户端,可以去做,但要将其与实际的服务器分开(不同的包,甚至不同的代码库)。一般来说,我们开发HTTP API是为了让各种客户端都能访问,这些客户端可以用任何编程语言编写。与其提供一些接口,我会把时间投入到编写良好的文档上。
在我看来,如果没有提供更多的上下文,你问自己是否应该提供一个Python客户端的答案应该是一样的。然而,如果你的API在公司内部使用,并且你主要使用Go语言进行开发,情况可能会有所不同。
英文:
In general, no – if you provide a client in a particular language it'd only be a convenience, so (some) users can use your API easier. This of course assumes your client is well designed. I wouldn't provide merely an interface in Go just to indicate a set of possible API calls. This would be beneficial to a very narrow range of audience, probably for people developing a client for your API themselves, in programming language which just happened to be the same as implementation of your server. And even then they might not really like the idea of using the interface (e.g. they might only need a specific set of methods).
If you want to provide a client for your API, go ahead, do it, but separate it from the actual server (different package, maybe even different repo). In general one develops APIs over HTTP to allow for wide range of clients to access it, which could be written in any language. Instead of providing some interfaces I would invest my time in writing a good documentation.
In my opinion the answer to your question, assuming there is no more context provided, should be no different if you asked yourself if you should provide a client in, say, Python. The whole situation might change though if, for example, your API is used internally by your company and you develop mainly in Go.
答案2
得分: 1
通常最好这样做,大多数公司都会提供与API直接交互的文档。这样做的主要用例是那些使用与你预期不同的编程语言的人。
你可以看一下我写的一个新的RESTful框架,它包含了使用Go模板自动编译客户端的基础设施,尽管我还没有写过Go客户端编译器。如果你想写一个,我们将非常感激 https://github.com/EverythingMe/vertex
英文:
It's usually preferable to do this, and most companies do, but provide documentation for working directly with the API. The main use case for that is people working with different languages than the ones you intended.
You can have a look at a new RESTful framework I wrote, that includes infrastructure to automatically compile clients with Go templates, although I haven't gotten to writing a Go client compiler. If you want to write one it would be greatly appreciated https://github.com/EverythingMe/vertex
答案3
得分: 1
测试在Go语言中非常重要,因此编写可测试的代码是必须的。如果你使用直接的HTTP请求,相比使用模拟的结构体,编写单元测试会更加困难。
有没有使用Client
而不是调用调用REST端点的函数的原因?通常来说,模拟一个较大的东西,比如一个Client结构体,要比模拟一组小函数更困难。
你应该将客户端放在booking.Client
中,以避免重复(booking.BookingClient
),并且可能将Client
重命名为更具描述性的名称。
英文:
Testing is important in Go, so writing testable code is something you should do. If you use direct http requests you will have a harder time writing unit tests, compared to using a mocked struct.
Is there any reason to use a Client
rather than calling the functions that call the REST endpoints? It's usually harder to mock a bigger thing, such as a Client struct, rather than a group of small functions.
You should put the client at booking.Client
to avoid repeating yourself (booking.BookingClient
) and maybe rename Client
to something more descriptive.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论