This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #46
- Loading branch information
Showing
413 changed files
with
14,682 additions
and
49,588 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
// Copyright (c) 2017-2021 Ingram Micro Inc. | ||
|
||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
log "github.com/sirupsen/logrus" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// Post sends POST request to Concerto API | ||
func (imco *IMCOClient) Post(path string, payload *map[string]interface{}) ([]byte, int, error) { | ||
|
||
url, jsPayload, err := imco.prepareCall(path, payload) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
log.Debugf("Sending POST request to %s with payload %v ", url, jsPayload) | ||
req, err := http.NewRequest("POST", url, jsPayload) | ||
req.Header.Add("Content-Type", "application/json") | ||
if imco.config.BrownfieldToken != "" { | ||
log.Debugf("Including brownfield token %s in POST request as X-Concerto-Brownfield-Token header ", imco.config.BrownfieldToken) | ||
req.Header.Add("X-Concerto-Brownfield-Token", imco.config.BrownfieldToken) | ||
} | ||
if imco.config.CommandPollingToken != "" && imco.config.ServerID != "" { | ||
log.Debugf("Including command polling token %s in POST request as X-IMCO-Command-Polling-Token header ", imco.config.CommandPollingToken) | ||
req.Header.Add("X-IMCO-Command-Polling-Token", imco.config.CommandPollingToken) | ||
log.Debugf("Including server id %s in POST request as X-IMCO-server-ID header ", imco.config.ServerID) | ||
req.Header.Add("X-IMCO-server-ID", imco.config.ServerID) | ||
} | ||
response, err := imco.client.Do(req) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
return imco.receiveResponse(response) | ||
} | ||
|
||
// Put sends PUT request to Concerto API | ||
func (imco *IMCOClient) Put(path string, payload *map[string]interface{}) ([]byte, int, error) { | ||
url, jsPayload, err := imco.prepareCall(path, payload) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
log.Debugf("Sending PUT request to %s with payload %v ", url, jsPayload) | ||
request, err := http.NewRequest("PUT", url, jsPayload) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
request.Header = map[string][]string{"Content-type": {"application/json"}} | ||
response, err := imco.client.Do(request) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
return imco.receiveResponse(response) | ||
} | ||
|
||
// Delete sends DELETE request to Concerto API | ||
func (imco *IMCOClient) Delete(path string) ([]byte, int, error) { | ||
url, _, err := imco.prepareCall(path, nil) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
log.Debugf("Sending DELETE request to %s", url) | ||
request, err := http.NewRequest("DELETE", url, nil) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
request.Header = map[string][]string{"Content-type": {"application/json"}} | ||
response, err := imco.client.Do(request) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
return imco.receiveResponse(response) | ||
} | ||
|
||
// Get sends GET request to Concerto API | ||
func (imco *IMCOClient) Get(path string) ([]byte, int, error) { | ||
|
||
url, _, err := imco.prepareCall(path, nil) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
log.Debugf("Sending GET request to %s", url) | ||
response, err := imco.client.Get(url) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
return imco.receiveResponse(response) | ||
} | ||
|
||
// GetFile sends GET request to Concerto API and receives a file | ||
func (imco *IMCOClient) GetFile(url string, filePath string, discoveryFileName bool) (string, int, error) { | ||
|
||
log.Debugf("Sending GET request to %s", url) | ||
response, err := imco.client.Get(url) | ||
if err != nil { | ||
return "", 0, err | ||
} | ||
|
||
defer response.Body.Close() | ||
log.Debugf("Status code:%d message:%s", response.StatusCode, response.Status) | ||
|
||
realFileName := filePath | ||
if discoveryFileName { | ||
r, err := regexp.Compile("filename=\\\"([^\\\"]*){1}\\\"") | ||
if err != nil { | ||
return "", 0, err | ||
} | ||
realFileName = fmt.Sprintf("%s/%s", filePath, r.FindStringSubmatch(response.Header.Get("Content-Disposition"))[1]) | ||
} | ||
|
||
output, err := os.Create(realFileName) | ||
if err != nil { | ||
return "", response.StatusCode, err | ||
} | ||
defer output.Close() | ||
|
||
n, err := io.Copy(output, response.Body) | ||
if err != nil { | ||
return "", response.StatusCode, err | ||
} | ||
|
||
log.Debugf("%#v bytes downloaded", n) | ||
return realFileName, response.StatusCode, nil | ||
} | ||
|
||
// PutFile sends PUT request to send a file | ||
func (imco *IMCOClient) PutFile(sourceFilePath string, targetURL string) ([]byte, int, error) { | ||
|
||
data, err := os.Open(sourceFilePath) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
req, err := http.NewRequest("PUT", targetURL, data) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
res, err := imco.client.Do(req) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
defer res.Body.Close() | ||
|
||
buf, err := ioutil.ReadAll(res.Body) | ||
status := res.StatusCode | ||
return buf, status, nil | ||
} | ||
|
||
func (imco *IMCOClient) prepareCall(path string, payload *map[string]interface{}) (url string, jsPayload *strings.Reader, err error) { | ||
|
||
if imco.config == nil || imco.client == nil { | ||
return "", nil, fmt.Errorf("cannot call web service without loading configuration") | ||
} | ||
|
||
url = fmt.Sprintf("%s%s", imco.config.APIEndpoint, path) | ||
|
||
if payload == nil { | ||
return url, nil, nil | ||
} | ||
|
||
// payload to json | ||
json, err := json.Marshal(payload) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
return url, strings.NewReader(string(json)), err | ||
} | ||
|
||
func (imco *IMCOClient) receiveResponse(response *http.Response) (body []byte, status int, err error) { | ||
|
||
defer response.Body.Close() | ||
body, err = ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
log.Debugf("Response : %s", body) | ||
log.Debugf("Status code: (%d) %s", response.StatusCode, response.Status) | ||
|
||
return body, response.StatusCode, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2017-2021 Ingram Micro Inc. | ||
|
||
package api | ||
|
||
//import "github.com/stretchr/testify/mock" | ||
// | ||
//// MockIMCOClient service manager | ||
//type MockIMCOClient struct { | ||
// mock.Mock | ||
//} | ||
// | ||
//// Post mocks POST request to IMCO API | ||
//func (m *MockIMCOClient) Post(path string, payload *map[string]interface{}) ([]byte, int, error) { | ||
// args := m.Called(path, payload) | ||
// return args.Get(0).([]byte), args.Int(1), args.Error(2) | ||
//} | ||
// | ||
//// Put mocks PUT request to IMCO API | ||
//func (m *MockIMCOClient) Put(path string, payload *map[string]interface{}) ([]byte, int, error) { | ||
// args := m.Called(path, payload) | ||
// return args.Get(0).([]byte), args.Int(1), args.Error(2) | ||
//} | ||
// | ||
//// Delete mocks DELETE request to IMCO API | ||
//func (m *MockIMCOClient) Delete(path string) ([]byte, int, error) { | ||
// args := m.Called(path) | ||
// return args.Get(0).([]byte), args.Int(1), args.Error(2) | ||
//} | ||
// | ||
//// Get mocks GET request to IMCO API | ||
//func (m *MockIMCOClient) Get(path string) ([]byte, int, error) { | ||
// args := m.Called(path) | ||
// return args.Get(0).([]byte), args.Int(1), args.Error(2) | ||
//} | ||
// | ||
//// GetFile sends GET request to IMCO API and receives a file | ||
//func (m *MockIMCOClient) GetFile(url string, filePath string, discoveryFileName bool) (string, int, error) { | ||
// args := m.Called(url, filePath) | ||
// return args.String(0), args.Int(1), args.Error(2) | ||
//} | ||
// | ||
//// PutFile sends PUT request to send a file | ||
//func (m *MockIMCOClient) PutFile(sourceFilePath string, targetURL string) ([]byte, int, error) { | ||
// args := m.Called(sourceFilePath, targetURL) | ||
// return args.Get(0).([]byte), args.Int(1), args.Error(2) | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Copyright (c) 2017-2021 Ingram Micro Inc. | ||
|
||
package api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Copyright (c) 2017-2021 Ingram Micro Inc. | ||
|
||
package api |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.