Skip to content

Commit

Permalink
Moved all status code logic to uni attribute func
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon Silcott committed Sep 4, 2021
1 parent dea42c4 commit 0e41655
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions replier.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ func (r *Replier) NewHTTPResponse(response *NewResponseRequest) error {

// Manage response for token
if response.AccessToken != "" || response.RefreshToken != "" {
return r.generateTokenResponse(response.AccessToken, response.RefreshToken, response.StatusCode)
return r.generateTokenResponse(response.AccessToken, response.RefreshToken)
}

// Manage response for data
if response.Data != nil {
return r.generateDataResponse(response.Data, response.StatusCode)
return r.generateDataResponse(response.Data)
}

return r.generateDefaultResponse()
Expand All @@ -135,25 +135,17 @@ func (r *Replier) generateDefaultResponse() error {
}

// generateDataResponse generates response based on passed data
func (r *Replier) generateDataResponse(data interface{}, statusCode int) error {
func (r *Replier) generateDataResponse(data interface{}) error {
r.transferObject.SetData(data)

if statusCode == 0 {
r.transferObject.SetStatusCode(defaultStatusCode)
}

return sendHTTPResponse(r.transferObject.GetWriter(), r.transferObject)
}

// generateTokenResponse generates token response on passed tokens information
func (r *Replier) generateTokenResponse(accessToken, refreshToken string, statusCode int) error {
func (r *Replier) generateTokenResponse(accessToken, refreshToken string) error {
r.transferObject.SetAccessToken(accessToken)
r.transferObject.SetRefreshToken(refreshToken)

if statusCode == 0 {
r.transferObject.SetStatusCode(defaultStatusCode)
}

return sendHTTPResponse(r.transferObject.GetWriter(), r.transferObject)
}

Expand Down Expand Up @@ -182,7 +174,13 @@ func (r *Replier) setUniversalAttributes(writer http.ResponseWriter, headers map
r.transferObject.SetWriter(writer)
r.setHeaders(headers)
r.transferObject.SetMeta(meta)
r.transferObject.SetStatusCode(statusCode)

if statusCode != 0 {
r.transferObject.SetStatusCode(statusCode)
return
}

r.transferObject.SetStatusCode(defaultStatusCode)
}

// setDefaultContentType handles setting default content type to JSON if
Expand Down

0 comments on commit 0e41655

Please sign in to comment.