Skip to content

Commit

Permalink
#16: Simplifying common response struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzrlk committed Aug 15, 2024
1 parent fbbd3f7 commit a358f20
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 462 deletions.
14 changes: 4 additions & 10 deletions internal/idmc/common/client_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@ import "net/http"
// ClientResponse
// Basic details of a parsed api response.
type ClientResponse struct {
Body []byte
HTTPResponse *http.Response
*http.Response
Body []byte
}

// IdmcClientResponse
// Currently aspirational, but intended to be a base struct for all IDMC api
// responses.
type IdmcClientResponse[Dat any, Err any] struct {
ClientResponse
JSON200 *Dat
JSON400 *Err
JSON401 *Err
JSON403 *Err
JSON404 *Err
JSON500 *Err
JSON502 *Err
JSON503 *Err
Success *Dat
Failure *Err
}
11 changes: 0 additions & 11 deletions internal/idmc/common/client_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ type HttpRequestDoer interface {
Do(req *http.Request) (*http.Response, error)
}

type ApiRequest interface {
HttpRequest() *http.Request
}

type ApiResponse interface {
Status() string
StatusCode() int
HttpResponse() *http.Response
BodyData() []byte
}

// Client provides access to client configuration.
type Client interface {
Config() *ClientConfig
Expand Down
30 changes: 2 additions & 28 deletions internal/idmc/templates/client-with-responses.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,6 @@ type {{genResponseTypeName $opid | ucFirst}} struct {
{{- 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
}

// HttpResponse returns HTTPResponse
func (r {{genResponseTypeName $opid | ucFirst}}) HttpResponse() *http.Response {
return r.HTTPResponse
}

// BodyData returns HTTPResponse.Body
func (r {{genResponseTypeName $opid | ucFirst}}) BodyData() []byte {
return r.Body
}

{{end}}


Expand Down Expand Up @@ -129,8 +103,8 @@ func Parse{{genResponseTypeName $opid | ucFirst}}(rsp *http.Response) (*{{genRes

response := &{{genResponseTypeName $opid}}{
ClientResponse: common.ClientResponse{
Body: bodyBytes,
HTTPResponse: rsp,
Response: rsp,
Body: bodyBytes,
},
}

Expand Down
210 changes: 14 additions & 196 deletions internal/idmc/v2/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a358f20

Please sign in to comment.