OpenAPI
API の仕様の形式、ドキュメント化や SDK 生成を自動で行える。
OpenAPI仕様 Version 3.1.0(YAML版) https://zenn.dev/robon/articles/518f1f4769301f
コード生成
oapi-codegen/oapi-codegen: Generate Go client and server boilerplate from OpenAPI 3 specifications https://github.com/oapi-codegen/oapi-codegen
oapi-codegen -config cfg.yaml openapi.yaml > code.gen.go
security の部分を考慮したコードを生成してくれていない?
自分で WithRequestEditorFn を書いてあげるしかない
code:main.go
c, _ := NewClient("https://api.fanbox.cc", WithHTTPClient(&hc), WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
req.Header.Set("Cookie", fmt.Sprintf("FANBOXSESSID=%s", "SESSIONID"))
return nil
}))
ogen-go/ogen: OpenAPI v3 code generator for go https://github.com/ogen-go/ogen
security の部分を考慮したコードを生成してくれる
ただし次のように記述する必要がある
code:main.go
type SecuritySource struct{}
func (s SecuritySource) CsrfToken(ctx context.Context, operationName string) (api.CsrfToken, error) {
return api.CsrfToken{
APIKey: "",
}, nil
}
func (s SecuritySource) SessionId(ctx context.Context, operationName string) (api.SessionId, error) {
return api.SessionId{
APIKey: "",
}, nil
}
func main() {
s := &SecuritySource{}
c, err := api.NewClient("https://api.fanbox.cc", s)
...
}
OpenAPI のスキーマからコードが生成される
同じスキーマから常に同じコードが生成される
ということはスキーマをバージョニングしたら、コードもそれと同じバージョンになると良さそう
例えばこんな感じでリポジトリを用意する
https://github.com/defaultcf/fanbox-specification
https://github.com/defaultcf/fanbox-go
specification で定義を更新し、新しいバージョンでタグを打つ
すると SDK のリポジトリでそれをトリガーにワークフローを走らせる
ここがまぁ問題というか
リポジトリが別なので workflow_dispatch するかどうかという感じ
新しいバージョンを使用したコードを生成し、コミット、タグを打つ
新しいスキーマをダウンロードして、ビルドしてコミットしてリリースするワークフローを考える
code:download-artifact.bash
curl -H "Authorization: token $(gh auth token)" "https://api.github.com/repos/OWNER/REPO/releases/tags/TAG" \
| jq '.assets0.url' \
| xargs -n1 curl -H "Authorization: token $(gh auth token)" -H "Accept: application/octet-stream"
output
あとは goreleaser など使ってリリースを自動化するが、別リポジトリの changelog を取得する方法を考える必要がありそう