Skip to content

Commit 8bf196e

Browse files
committed
add xpanse client generation
1 parent 14273e8 commit 8bf196e

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

pkg/common/http_logger.go pkg/logger/http_logger.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
* SPDX-FileCopyrightText: Huawei Inc.
44
*/
55

6-
package common
6+
package logger
77

88
import (
99
"bytes"
1010
"fmt"
11-
"io/ioutil"
11+
"io"
1212
"log/slog"
1313
"net/http"
14-
"xpanse-agent/pkg/logger"
1514
)
1615

1716
type HttpRequestLogger struct {
@@ -20,21 +19,21 @@ type HttpRequestLogger struct {
2019

2120
func (httpRequestLogger *HttpRequestLogger) RoundTrip(req *http.Request) (*http.Response, error) {
2221
// Log the request URL
23-
logger.Logger.Info(fmt.Sprintf("Request URL: %s", req.URL.String()))
22+
Logger.Info(fmt.Sprintf("Request URL: %s", req.URL.String()))
2423

2524
// Read and log the request body if it exists
2625
var bodyBytes []byte
2726
if req.Body != nil {
2827
var err error
29-
bodyBytes, err = ioutil.ReadAll(req.Body)
28+
bodyBytes, err = io.ReadAll(req.Body)
3029
if err != nil {
3130
return nil, err
3231
}
3332
// Log the request body
34-
logger.Logger.Info(fmt.Sprintf("Request Body: %s", string(bodyBytes)))
33+
Logger.Info(fmt.Sprintf("Request Body: %s", string(bodyBytes)))
3534

3635
// Restore the io.ReadCloser to its original state
37-
req.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
36+
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
3837
}
3938

4039
// Make the actual HTTP request using the embedded RoundTripper
@@ -44,18 +43,18 @@ func (httpRequestLogger *HttpRequestLogger) RoundTrip(req *http.Request) (*http.
4443
}
4544

4645
// Log the response status code
47-
logger.Logger.Info(fmt.Sprintf("Response Status: %d", resp.StatusCode))
46+
Logger.Info(fmt.Sprintf("Response Status: %d", resp.StatusCode))
4847

4948
// Read and log the response body
50-
responseBody, err := ioutil.ReadAll(resp.Body)
49+
responseBody, err := io.ReadAll(resp.Body)
5150
if err != nil {
5251
return nil, err
5352
}
5453
// Log the response body
55-
logger.Logger.Info("Response Body: ", slog.String("msg", string(responseBody)))
54+
Logger.Info("Response Body: ", slog.String("msg", string(responseBody)))
5655

5756
// Restore the response body so it can be read later by application code
58-
resp.Body = ioutil.NopCloser(bytes.NewBuffer(responseBody))
57+
resp.Body = io.NopCloser(bytes.NewBuffer(responseBody))
5958

6059
return resp, nil
6160
}

pkg/poller/api_poller.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ import (
1111
"net/http"
1212
"os"
1313
"strings"
14-
"xpanse-agent/pkg/common"
1514
"xpanse-agent/pkg/logger"
1615
"xpanse-agent/pkg/xpanseclient"
1716
)
1817

1918
func PollXpanseApi(serviceId string, resourceName string, xpanseApiEndpoint string) {
2019
hc := &http.Client{
21-
Transport: &common.HttpRequestLogger{
20+
Transport: &logger.HttpRequestLogger{
2221
RoundTripper: http.DefaultTransport,
2322
},
2423
}

0 commit comments

Comments
 (0)