Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
switch to import V5 api
Browse files Browse the repository at this point in the history
  • Loading branch information
deefdragon committed Feb 20, 2023
1 parent afe65bc commit ce077da
Show file tree
Hide file tree
Showing 54 changed files with 30,146 additions and 15,771 deletions.
61 changes: 0 additions & 61 deletions api/Utils.go

This file was deleted.

16,508 changes: 16,508 additions & 0 deletions api/api.gen.go

Large diffs are not rendered by default.

1,950 changes: 1,950 additions & 0 deletions api/circuit.gen.go

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions api/config.client.yaml

This file was deleted.

4 changes: 0 additions & 4 deletions api/config.server.yaml

This file was deleted.

4 changes: 0 additions & 4 deletions api/config.types.yaml

This file was deleted.

155 changes: 155 additions & 0 deletions api/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
output: api.gen.go
package: api
output-options:
skip-prune: true
user-templates:
client-with-responses.tmpl: |
// ClientWithResponses builds on ClientInterface to offer response payloads
type ClientWithResponses struct {
ClientInterface
OkOnNon200 bool
}
// StatusFailureError is the error returned if the response was a non-success code.
type StatusFailureError struct {
Resp *http.Response
Err error
}
func (s StatusFailureError)Error() string {
if s.Err != nil {
return s.Err.Error()
}
return fmt.Sprintf("got non success response code: %d", s.Resp.StatusCode )
}
// NewClientWithResponses creates a new ClientWithResponses, which wraps
// Client with return type handling
func NewClientWithResponses(server string, OkOnNon200 bool, opts ...ClientOption) (*ClientWithResponses, error) {
client, err := NewClient(server, opts...)
if err != nil {
return nil, err
}
return &ClientWithResponses{
ClientInterface: client,
OkOnNon200: OkOnNon200,
}, nil
}
// WithBaseURL overrides the baseURL.
func WithBaseURL(baseURL string) ClientOption {
return func(c *Client) error {
newBaseURL, err := url.Parse(baseURL)
if err != nil {
return err
}
c.Server = newBaseURL.String()
return nil
}
}
// ClientWithResponsesInterface is the interface specification for the client with responses above.
type ClientWithResponsesInterface interface {
{{range . -}}
{{$hasParams := .RequiresParamObject -}}
{{$pathParams := .PathParams -}}
{{$opid := .OperationId -}}
// {{$opid}} request{{if .HasBody}} with any body{{end}}
{{$opid}}{{if .HasBody}}WithBody{{end}}WithResponse(ctx context.Context{{genParamArgs .PathParams}}{{if .RequiresParamObject}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}, reqEditors... RequestEditorFn) (*{{genResponseTypeName $opid}}, error)
{{range .Bodies}}
{{if .IsSupportedByClient -}}
{{$opid}}{{.Suffix}}WithResponse(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody, reqEditors... RequestEditorFn) (*{{genResponseTypeName $opid}}, error)
{{end -}}
{{end}}{{/* range .Bodies */}}
{{end}}{{/* range . $opid := .OperationId */}}
}
{{range .}}{{$opid := .OperationId}}{{$op := .}}
type {{genResponseTypeName $opid | ucFirst}} struct {
Body []byte
HTTPResponse *http.Response
{{- range getResponseTypeDefinitions .}}
{{.TypeName}} *{{.Schema.TypeDecl}}
{{- end}}
}
// Status returns HTTPResponse.Status
func (r {{genResponseTypeName $opid | ucFirst}}) Status() string {
if r.HTTPResponse != nil {
return r.HTTPResponse.Status
}
return http.StatusText(0)
}
// StatusCode returns HTTPResponse.StatusCode
func (r {{genResponseTypeName $opid | ucFirst}}) StatusCode() int {
if r.HTTPResponse != nil {
return r.HTTPResponse.StatusCode
}
return 0
}
{{end}}
{{range .}}
{{$opid := .OperationId -}}
{{/* Generate client methods (with responses)*/}}
// {{$opid}}{{if .HasBody}}WithBody{{end}}WithResponse request{{if .HasBody}} with arbitrary body{{end}} returning *{{genResponseTypeName $opid}}
func (c *ClientWithResponses) {{$opid}}{{if .HasBody}}WithBody{{end}}WithResponse(ctx context.Context{{genParamArgs .PathParams}}{{if .RequiresParamObject}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}, reqEditors... RequestEditorFn) (*{{genResponseTypeName $opid}}, error){
rsp, err := c.{{$opid}}{{if .HasBody}}WithBody{{end}}(ctx{{genParamNames .PathParams}}{{if .RequiresParamObject}}, params{{end}}{{if .HasBody}}, contentType, body{{end}}, reqEditors...)
if err != nil {
return nil, err
}
r,err := Parse{{genResponseTypeName $opid | ucFirst}}(rsp)
if !c.OkOnNon200 && (r != nil && r.StatusCode() >= 400) {
return r, StatusFailureError{Resp: rsp, Err: err}
}
return r,err
}
{{$hasParams := .RequiresParamObject -}}
{{$pathParams := .PathParams -}}
{{$bodyRequired := .BodyRequired -}}
{{range .Bodies}}
{{if .IsSupportedByClient -}}
func (c *ClientWithResponses) {{$opid}}{{.Suffix}}WithResponse(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody, reqEditors... RequestEditorFn) (*{{genResponseTypeName $opid}}, error) {
rsp, err := c.{{$opid}}{{.Suffix}}(ctx{{genParamNames $pathParams}}{{if $hasParams}}, params{{end}}, body, reqEditors...)
if err != nil {
return nil, err
}
r,err := Parse{{genResponseTypeName $opid | ucFirst}}(rsp)
if !c.OkOnNon200 && (r != nil && r.StatusCode() >= 400) {
return r, StatusFailureError{Resp: rsp, Err: err}
}
return r,err
}
{{end}}
{{end}}
{{end}}{{/* operations */}}
{{/* Generate parse functions for responses*/}}
{{range .}}{{$opid := .OperationId}}
// Parse{{genResponseTypeName $opid | ucFirst}} parses an HTTP response from a {{$opid}}WithResponse call
func Parse{{genResponseTypeName $opid | ucFirst}}(rsp *http.Response) (*{{genResponseTypeName $opid}}, error) {
bodyBytes, err := io.ReadAll(rsp.Body)
defer func() { _ = rsp.Body.Close() }()
if err != nil {
return nil, err
}
response := {{genResponsePayload $opid}}
{{genResponseUnmarshal .}}
return response, nil
}
{{end}}{{/* range . $opid := .OperationId */}}
generate:
client: true
strict-server: true
echo-server: true
models: true
embedded-spec: true
37 changes: 34 additions & 3 deletions api/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
package api

//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --config=config.types.yaml tiltify.openapi.yaml
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --config=config.server.yaml tiltify.openapi.yaml
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --config=config.client.yaml tiltify.openapi.yaml
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --config=config.yaml openapi.yaml
//generate API code.

//go:generate go run github.com/vektra/mockery/[email protected] --name ClientWithResponsesInterface --output ../mocks --with-expecter --filename api.gen.go --structname Api
//generate client interface mocks

//go:generate go run github.com/twitchtv/circuitgen --name ClientWithResponsesInterface --pkg ./ --debug --out ./circuit.gen.go --alias ClientWithResponsesCircuit --circuit-major-version 3
//generate circuits for interface

/*
func GenerateREPLACEMEClient(
server string,
subjectString string,
client api.HttpRequestDoer,
manager *circuit.Manager,
conf api.CircuitWrapperClientWithResponsesCircuitConfig,
) (api.ClientWithResponsesInterface, error) {
e, err := api.NewClientWithResponses(server, api.ClientOption(func(cl *api.Client) error {
cl.RequestEditors = []api.RequestEditorFn{
func(ctx context.Context, req *http.Request) error {
req.Header.Add("subject-uuid", subjectString)
return nil
},
}
cl.Client = client
return nil
}))
if err != nil {
return nil, err
}
return api.NewCircuitWrapperClientWithResponsesCircuit(manager, e, conf)
}
*/
12 changes: 0 additions & 12 deletions api/oapi-codegen-tools/examples/campaigns-id-challenges.json

This file was deleted.

8 changes: 0 additions & 8 deletions api/oapi-codegen-tools/examples/campaigns-id-donations.json

This file was deleted.

26 changes: 0 additions & 26 deletions api/oapi-codegen-tools/examples/campaigns-id-polls.json

This file was deleted.

25 changes: 0 additions & 25 deletions api/oapi-codegen-tools/examples/campaigns-id-rewards.json

This file was deleted.

8 changes: 0 additions & 8 deletions api/oapi-codegen-tools/examples/campaigns-id-schedule.json

This file was deleted.

Loading

0 comments on commit ce077da

Please sign in to comment.