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

Commit

Permalink
chore: Remove unneeded code (#490)
Browse files Browse the repository at this point in the history
* Remove unused functions

Signed-off-by: Arthur Pitman <arthur.pitman@dynatrace.com>

* Deduplicate constant

Signed-off-by: Arthur Pitman <arthur.pitman@dynatrace.com>

* Remove unneeded `fmt.Println` calls

Signed-off-by: Arthur Pitman <arthur.pitman@dynatrace.com>
  • Loading branch information
arthurpitman authored Jul 7, 2022
1 parent 6550348 commit d00898a
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 301 deletions.
280 changes: 0 additions & 280 deletions pkg/api/utils/apiServiceUtils.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package api

import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"

"github.com/keptn/go-utils/pkg/api/models"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

Expand Down Expand Up @@ -63,278 +58,3 @@ func getClientTransport(rt http.RoundTripper) http.RoundTripper {
}
return rt
}

func getAndExpectOK(ctx context.Context, uri string, api APIService) ([]byte, *models.Error) {
body, statusCode, status, err := get(ctx, uri, api)
if err != nil {
return nil, err
}

if statusCode == 200 {
return body, nil
}

if len(body) > 0 {
return nil, handleErrStatusCode(statusCode, body)
}

return nil, buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", statusCode, status))
}

func getAndExpectSuccess(ctx context.Context, uri string, api APIService) ([]byte, *models.Error) {
body, statusCode, status, err := get(ctx, uri, api)
if err != nil {
return nil, err
}

if statusCode >= 200 && statusCode < 300 {
return body, nil
}

if len(body) > 0 {
return nil, handleErrStatusCode(statusCode, body)
}

return nil, buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", statusCode, status))
}

func get(ctx context.Context, uri string, api APIService) ([]byte, int, string, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "GET", uri, nil)
if err != nil {
return nil, 0, "", buildErrorResponse(err.Error())
}
req.Header.Set("Content-Type", "application/json")
addAuthHeader(req, api)

resp, err := api.getHTTPClient().Do(req)
if err != nil {
return nil, 0, "", buildErrorResponse(err.Error())
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, 0, "", buildErrorResponse(err.Error())
}

return body, resp.StatusCode, resp.Status, nil
}

func putWithEventContext(ctx context.Context, uri string, data []byte, api APIService) (*models.EventContext, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "PUT", uri, bytes.NewBuffer(data))
if err != nil {
return nil, buildErrorResponse(err.Error())
}
req.Header.Set("Content-Type", "application/json")
addAuthHeader(req, api)

resp, err := api.getHTTPClient().Do(req)
if err != nil {
return nil, buildErrorResponse(err.Error())
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, buildErrorResponse(err.Error())
}

if resp.StatusCode >= 200 && resp.StatusCode <= 204 {
if len(body) == 0 {
return nil, nil
}

eventContext := &models.EventContext{}

if err = eventContext.FromJSON(body); err != nil {
// failed to parse json
return nil, buildErrorResponse(err.Error() + "\n" + "-----DETAILS-----" + string(body))
}

if eventContext.KeptnContext != nil {
fmt.Println("ID of Keptn context: " + *eventContext.KeptnContext)
}
return eventContext, nil
}

if len(body) > 0 {
return nil, handleErrStatusCode(resp.StatusCode, body)
}

return nil, buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", resp.StatusCode, resp.Status))
}

func put(ctx context.Context, uri string, data []byte, api APIService) (string, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "PUT", uri, bytes.NewBuffer(data))
if err != nil {
return "", buildErrorResponse(err.Error())
}
req.Header.Set("Content-Type", "application/json")
addAuthHeader(req, api)

resp, err := api.getHTTPClient().Do(req)
if err != nil {
return "", buildErrorResponse(err.Error())
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", buildErrorResponse(err.Error())
}

if resp.StatusCode >= 200 && resp.StatusCode <= 204 {
return string(body), nil
}

if len(body) > 0 {
return "", handleErrStatusCode(resp.StatusCode, body)
}

return "", buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", resp.StatusCode, resp.Status))
}

func postWithEventContext(ctx context.Context, uri string, data []byte, api APIService) (*models.EventContext, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "POST", uri, bytes.NewBuffer(data))
if err != nil {
return nil, buildErrorResponse(err.Error())
}
req.Header.Set("Content-Type", "application/json")
addAuthHeader(req, api)

resp, err := api.getHTTPClient().Do(req)
if err != nil {
return nil, buildErrorResponse(err.Error())
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, buildErrorResponse(err.Error())
}

if resp.StatusCode >= 200 && resp.StatusCode <= 204 {
if len(body) == 0 {
return nil, nil
}

eventContext := &models.EventContext{}
if err = eventContext.FromJSON(body); err != nil {
// failed to parse json
return nil, buildErrorResponse(err.Error() + "\n" + "-----DETAILS-----" + string(body))
}

if eventContext.KeptnContext != nil {
fmt.Println("ID of Keptn context: " + *eventContext.KeptnContext)
}
return eventContext, nil
}

if len(body) > 0 {
return nil, handleErrStatusCode(resp.StatusCode, body)
}

return nil, buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", resp.StatusCode, resp.Status))
}

func post(ctx context.Context, uri string, data []byte, api APIService) (string, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "POST", uri, bytes.NewBuffer(data))
if err != nil {
return "", buildErrorResponse(err.Error())
}
req.Header.Set("Content-Type", "application/json")
addAuthHeader(req, api)

resp, err := api.getHTTPClient().Do(req)
if err != nil {
return "", buildErrorResponse(err.Error())
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", buildErrorResponse(err.Error())
}

if resp.StatusCode >= 200 && resp.StatusCode <= 204 {
return string(body), nil
}

if len(body) > 0 {
return "", handleErrStatusCode(resp.StatusCode, body)
}

return "", buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", resp.StatusCode, resp.Status))
}

func deleteWithEventContext(ctx context.Context, uri string, api APIService) (*models.EventContext, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "DELETE", uri, nil)
if err != nil {
return nil, buildErrorResponse(err.Error())
}
req.Header.Set("Content-Type", "application/json")
addAuthHeader(req, api)

resp, err := api.getHTTPClient().Do(req)
if err != nil {
return nil, buildErrorResponse(err.Error())
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, buildErrorResponse(err.Error())
}

if resp.StatusCode >= 200 && resp.StatusCode < 300 {
if len(body) == 0 {
return nil, nil
}

eventContext := &models.EventContext{}
if err = eventContext.FromJSON(body); err != nil {
// failed to parse json
return nil, buildErrorResponse(err.Error() + "\n" + "-----DETAILS-----" + string(body))
}
return eventContext, nil
}

return nil, handleErrStatusCode(resp.StatusCode, body)
}

func delete(ctx context.Context, uri string, api APIService) (string, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "DELETE", uri, nil)
if err != nil {
return "", buildErrorResponse(err.Error())
}
req.Header.Set("Content-Type", "application/json")
addAuthHeader(req, api)

resp, err := api.getHTTPClient().Do(req)
if err != nil {
return "", buildErrorResponse(err.Error())
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", buildErrorResponse(err.Error())
}

if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return string(body), nil
}

return "", handleErrStatusCode(resp.StatusCode, body)
}

func buildErrorResponse(errorStr string) *models.Error {
err := models.Error{Message: &errorStr}
return &err
}

func addAuthHeader(req *http.Request, api APIService) {
if api.getAuthHeader() != "" && api.getAuthToken() != "" {
req.Header.Set(api.getAuthHeader(), api.getAuthToken())
}
}
17 changes: 2 additions & 15 deletions pkg/api/utils/errors.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
package api

import (
"fmt"

"github.com/keptn/go-utils/pkg/api/models"
)
import v2 "github.com/keptn/go-utils/pkg/api/utils/v2"

// ErrWithStatusCode message
const ErrWithStatusCode = "error with status code %d"

func handleErrStatusCode(statusCode int, body []byte) *models.Error {
respErr := &models.Error{}
if err := respErr.FromJSON(body); err == nil && respErr != nil {
return respErr
}

return buildErrorResponse(fmt.Sprintf(ErrWithStatusCode, statusCode))
}
const ErrWithStatusCode = v2.ErrWithStatusCode
6 changes: 0 additions & 6 deletions pkg/api/utils/v2/apiServiceUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ func putWithEventContext(ctx context.Context, uri string, data []byte, api APISe
return nil, buildErrorResponse(err.Error() + "\n" + "-----DETAILS-----" + string(body))
}

if eventContext.KeptnContext != nil {
fmt.Println("ID of Keptn context: " + *eventContext.KeptnContext)
}
return eventContext, nil
}

Expand Down Expand Up @@ -224,9 +221,6 @@ func postWithEventContext(ctx context.Context, uri string, data []byte, api APIS
return nil, buildErrorResponse(err.Error() + "\n" + "-----DETAILS-----" + string(body))
}

if eventContext.KeptnContext != nil {
fmt.Println("ID of Keptn context: " + *eventContext.KeptnContext)
}
return eventContext, nil
}

Expand Down

0 comments on commit d00898a

Please sign in to comment.