如何在从OpenAPI规范生成的客户端Go代码中使用自定义的Go HTTP客户端?

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

How to use a custom go http client with the client side go code generated from openapi spec

问题

我正在生成一个基于 OpenAPI 规范的 API 客户端库。我使用的生成命令类似于:

openapi-generator generate -g go -i spec.yaml -o code-gen-go -p packageName=mypackage

这将在生成的代码中创建一个类似下面的结构体:

type Configuration struct {
	Host             string            `json:"host,omitempty"`
	Scheme           string            `json:"scheme,omitempty"`
	DefaultHeader    map[string]string `json:"defaultHeader,omitempty"`
	UserAgent        string            `json:"userAgent,omitempty"`
	Debug            bool              `json:"debug,omitempty"`
	Servers          ServerConfigurations
	OperationServers map[string]ServerConfigurations
	HTTPClient       *http.Client
}

这里的 HTTPClient 字段将用于发起请求。理想情况下,你应该导入这个包,将一个客户端赋值给 HTTPClient 字段,然后就可以通过它来发起 HTTP 请求了。

但在我的情况下,我必须使用一个自定义库来发起请求。假设我的库是 customHttp。我必须使用这个库来创建一个类型为 *customHttp.Client 的客户端(它实际上是一个带有一些额外插件的 *http.Client 类型的客户端)。我该如何做到这一点?是否可以在不手动更新自动生成的代码的情况下实现这一点?

我想,如果我能让它生成的代码中 HTTPClient 的类型是实现了 Do 方法的接口,那么我就可以将我的客户端赋值给它了。但我还没有找到如何实现这一点的方法。

英文:

I am working on generating an http client library from an API specification which is in open api format.

The command I am using to generate this is similar to this

openapi-generator generate -g go -i spec.yaml -o code-gen-go -p packageName=mypackage

This creates a struct like the one below in the generated code


type Configuration struct {
	Host             string            `json:"host,omitempty"`
	Scheme           string            `json:"scheme,omitempty"`
	DefaultHeader    map[string]string `json:"defaultHeader,omitempty"`
	UserAgent        string            `json:"userAgent,omitempty"`
	Debug            bool              `json:"debug,omitempty"`
	Servers          ServerConfigurations
	OperationServers map[string]ServerConfigurations
	HTTPClient       *http.Client
}

where HTTPClient field here will be used to make requests. Ideally, one should import this package, assign a client to the HTTPClient field and they should be able to make http requests via this.

But in my case, I have to use a custom library to make requests. Let's say my library is customHttp. I have to use this library to create a client of the type *customHttp.Client ( which is simply a client of type *http.Client but with some additional plugins ). How can I do this? Is it possible to do this without manually updating the auto-generated code?

I figure if I can get it to generate code that the type of HTTPClient is an interface that implements Do method, I will be able to assign my client with it? but I could not figure out how to do that either.

答案1

得分: 0

可以通过修改Go客户端的Mustache模板来自定义生成的代码。

从存储库中获取模板:

openapi-generator-cli author template -g go -o tmp/mygotemplates

现在你有了一个本地副本:修改你想要自定义的模板,例如configuration.mustache。在这里,你可以导入你需要的代码和模块,如果需要的话,还可以重命名现有的代码。添加你自定义的客户端库。

继续使用你自己的模板生成代码:

openapi-generator-cli generate \
-i openapi.yaml \
-t tmp/mygotemplates \
-g go \
-p packageName=myPackage \
-o src

生成的代码现在包含了你的自定义代码和库。这种方法提供了你所需的灵活性,但需要维护一个自定义版本的模板(例如,将来可能需要更新)。

这是一篇关于代码生成的文章,供你参考。

英文:

It is possible to customise the generated code by modifying the Mustache templates for the Go client

Fetch the templates from the repository:

openapi-generator-cli author template -g go -o tmp/mygotemplates

You have now a local copy: modify the templates you want to customise, in this case, it is configuration.mustache.
Here you can import the code and modules you need, renaming existing code too if necessary. Add you custom Client library.

Go on and generate the code using your own templates:

openapi-generator-cli generate \
-i openapi.yaml \
-t tmp/mygotemplates \
-g go \
-p packageName=myPackage \
-o src

The generated code now includes your custom code and libraries. The approach gives the flexibility you need but comes at the price of maintaining a custom version of the templates (which you might need to update in future for example).

Here is an article about code generation as a reference.

huangapple
  • 本文由 发表于 2023年1月12日 18:02:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75094531.html
匿名

发表评论

匿名网友

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

确定