Skip to content

Commit

Permalink
Updated with new Request function that does not automatically read an…
Browse files Browse the repository at this point in the history
…d has no retry
  • Loading branch information
Amnesiac9 committed Sep 20, 2024
1 parent 2188020 commit 7d645b4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
41 changes: 36 additions & 5 deletions c7api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (

const SLEEP_TIME = 500 * time.Millisecond

// Basic requests to C7 endpoint wrapped in retry logic with exponential backoff
func Request(method string, url *string, reqBody *[]byte, tenant string, c7AppAuthEncoded string, retryCount int) (*[]byte, error) {
// Basic requests to C7 endpoint wrapped in retry logic with exponential backoff.
func RequestWithRetryAndRead(method string, url string, reqBody *[]byte, tenant string, c7AppAuthEncoded string, retryCount int) (*[]byte, error) {
//
if url == nil || tenant == "" || c7AppAuthEncoded == "" {
if url == "" || tenant == "" || c7AppAuthEncoded == "" {
return nil, fmt.Errorf("error getting JSON from C7: nil or blank value in arguments")
}

Expand All @@ -36,7 +36,7 @@ func Request(method string, url *string, reqBody *[]byte, tenant string, c7AppAu
body := []byte{}

for i := 0; i <= retryCount; i++ {
req, err := http.NewRequest(method, *url, bytes.NewBuffer(*reqBody))
req, err := http.NewRequest(method, url, bytes.NewBuffer(*reqBody))
if err != nil {
return nil, fmt.Errorf("error creating GET request for C7: %v", err)
}
Expand Down Expand Up @@ -74,6 +74,37 @@ func Request(method string, url *string, reqBody *[]byte, tenant string, c7AppAu

}

func Request(method string, url string, reqBody *[]byte, tenant string, c7AppAuthEncoded string) (*http.Response, error) {
//
if url == "" || tenant == "" || c7AppAuthEncoded == "" {
return nil, fmt.Errorf("error getting JSON from C7: nil or blank value in arguments")
}

if reqBody == nil {
reqBody = &[]byte{}
}

client := &http.Client{}
response := &http.Response{StatusCode: 0}

req, err := http.NewRequest(method, url, bytes.NewBuffer(*reqBody))
if err != nil {
return nil, fmt.Errorf("error creating GET request for C7: %v", err)
}

req.Header.Set("tenant", tenant)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", c7AppAuthEncoded)

response, err = client.Do(req)
if err != nil {
return nil, fmt.Errorf("error making GET request to C7: %v", err)
}

return response, nil

}

// Errors will return a custom error type called C7Error if there is an error directly from C7, calling err.Error() on this will return the error message from C7 and the status code.

// Takes in a full URL string and request JSON from C7 and return it as a byte array
Expand Down Expand Up @@ -426,7 +457,7 @@ func MarkNoFulfillmentRequired(orderId string, shipTime time.Time, tenant string
}

// Post the fulfillment to C7
_, err = Request("POST", &url, &fulfillmentJSON, tenant, auth, attempts)
_, err = RequestWithRetryAndRead("POST", url, &fulfillmentJSON, tenant, auth, attempts)
if err != nil {
return errors.New("error posting NFR fulfillment to C7: " + err.Error())
}
Expand Down
8 changes: 4 additions & 4 deletions c7api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestGetC7_New(t *testing.T) {

t.Log("Test", tc.name)

jsonBytes, err := Request(tc.method, &tc.url, &tc.body, tc.tenant, tc.auth, tc.attempts)
jsonBytes, err := RequestWithRetryAndRead(tc.method, tc.url, &tc.body, tc.tenant, tc.auth, tc.attempts)
if err != nil && err.(C7Error).StatusCode != tc.expectedCode {
t.Error("TestGetJSONFromC7, test case: ", tc.name, " Expected status code: ", tc.expectedCode, " got: ", err.(C7Error).StatusCode)
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestPostC7_New(t *testing.T) {

t.Log("Test", tc.name)

jsonBytes, err := Request(tc.method, &tc.url, &tc.body, tc.tenant, tc.auth, tc.attempts)
jsonBytes, err := RequestWithRetryAndRead(tc.method, tc.url, &tc.body, tc.tenant, tc.auth, tc.attempts)
if err != nil && err.(C7Error).StatusCode != tc.expectedCode {
t.Error("TestGetJSONFromC7, test case: ", tc.name, " Expected status code: ", tc.expectedCode, " got: ", err.(C7Error).StatusCode)
}
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestDeleteC7_New(t *testing.T) {

t.Log("Test", tc.name)

jsonBytes, err := Request(tc.method, &tc.url, &tc.body, tc.tenant, tc.auth, tc.attempts)
jsonBytes, err := RequestWithRetryAndRead(tc.method, tc.url, &tc.body, tc.tenant, tc.auth, tc.attempts)
if err != nil && err.(C7Error).StatusCode != tc.expectedCode {
t.Error("TestGetJSONFromC7, test case: ", tc.name, " Expected status code: ", tc.expectedCode, " got: ", err.(C7Error).StatusCode)
}
Expand All @@ -218,7 +218,7 @@ func TestDeleteC7_New(t *testing.T) {
t.Log("Adding Fulfillment for test TestDeleteC7_New")

// Post previous fulfillment for test
jsonBytes, err := Request("POST", &urlStringFulfillment, &goodBytes, tenant, goodAuth, 1)
jsonBytes, err := RequestWithRetryAndRead("POST", urlStringFulfillment, &goodBytes, tenant, goodAuth, 1)
if err != nil || jsonBytes == nil {
t.Error("Error posting fulfillment: ", err.Error())
return
Expand Down

0 comments on commit 7d645b4

Please sign in to comment.