Skip to content

Commit

Permalink
Fix: Response close when defer and add Config file (#1)
Browse files Browse the repository at this point in the history
* Add: add config file

* Fix: response close when defer

* Fix: fix test failed
  • Loading branch information
andy89923 authored Mar 6, 2024
1 parent 74ed715 commit 1a990fc
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 85 deletions.
22 changes: 22 additions & 0 deletions config/nefcfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
info:
version: 1.0.1
description: NEF initial local configuration

configuration:
sbi:
scheme: http # The protocol for sbi (http or https)
registerIPv4: 127.0.0.5 # IP used to register to NRF
bindingIPv4: 127.0.0.5 # IP used to bind the service
port: 8000 # port used to bind the service
tls: # the local path of TLS key
pem: cert/nef.pem # NEF TLS Certificate
key: cert/nef.key # NEF TLS Private key
nrfUri: http://127.0.0.10:8000 # A valid URI of NRF
serviceList: # the SBI services provided by this NEF
- serviceName: nnef-pfdmanagement # Nnef_PFDManagement Service
- serviceName: nnef-oam # OAM service

logger: # log output setting
enable: true # true or false
level: info # how detailed to output, value: trace, debug, info, warn, error, fatal, panic
reportCaller: false # enable the caller report or not, value: true or false
76 changes: 45 additions & 31 deletions internal/sbi/consumer/pcf_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ func (s *npcfService) GetAppSession(appSessionId string) (int, interface{}) {
GetAppSession(context.Background(), appSessionId)

if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusOK {
rspBody = &result
Expand All @@ -78,12 +87,6 @@ func (s *npcfService) GetAppSession(appSessionId string) (int, interface{}) {
} else {
// API Service Internal Error or Server No Response
rspCode, rspBody = handleAPIServiceNoResponse(err)
defer func() {
err := rsp.Request.Response.Body.Close()
if err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
}

return rspCode, rspBody
Expand All @@ -106,13 +109,16 @@ func (s *npcfService) PostAppSessions(asc *models.AppSessionContext) (int, inter
client := s.getClient(uri)

result, rsp, err = client.ApplicationSessionsCollectionApi.PostAppSessions(context.TODO(), *asc)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusCreated {
logger.ConsumerLog.Debugf("PostAppSessions RspData: %+v", result)
Expand Down Expand Up @@ -152,12 +158,6 @@ func (s *npcfService) PutAppSession(
appSessID = appSessionId
result, rsp, err = client.IndividualApplicationSessionContextDocumentApi.
GetAppSession(context.Background(), appSessionId)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
if rsp.Body != nil {
if bodyCloseErr := rsp.Body.Close(); bodyCloseErr != nil {
Expand All @@ -170,8 +170,16 @@ func (s *npcfService) PutAppSession(
// Patch
result, rsp, err = client.IndividualApplicationSessionContextDocumentApi.ModAppSession(
context.Background(), appSessionId, *ascUpdateData)

if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusOK {
logger.ConsumerLog.Debugf("PatchAppSessions RspData: %+v", result)
Expand Down Expand Up @@ -218,13 +226,16 @@ func (s *npcfService) PatchAppSession(appSessionId string,

result, rsp, err = client.IndividualApplicationSessionContextDocumentApi.ModAppSession(
context.Background(), appSessionId, *ascUpdateData)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusOK {
logger.ConsumerLog.Debugf("PatchAppSessions RspData: %+v", result)
Expand Down Expand Up @@ -261,13 +272,16 @@ func (s *npcfService) DeleteAppSession(appSessionId string) (int, interface{}) {

result, rsp, err = client.IndividualApplicationSessionContextDocumentApi.DeleteAppSession(
context.Background(), appSessionId, param)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusOK {
logger.ConsumerLog.Debugf("DeleteAppSessions RspData: %+v", result)
Expand Down
135 changes: 81 additions & 54 deletions internal/sbi/consumer/udr_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ func (s *nudrService) AppDataInfluenceDataGet(influenceIDs []string) (int, inter

result, rsp, err = client.InfluenceDataApi.
ApplicationDataInfluenceDataGet(context.Background(), param)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusOK {
rspBody = &result
Expand Down Expand Up @@ -112,13 +115,16 @@ func (s *nudrService) AppDataInfluenceDataIdGet(influenceID string) (int, interf

result, rsp, err = client.InfluenceDataApi.
ApplicationDataInfluenceDataGet(context.Background(), param)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusOK {
rspBody = &result
Expand Down Expand Up @@ -152,13 +158,16 @@ func (s *nudrService) AppDataInfluenceDataPut(influenceID string,

result, rsp, err = client.IndividualInfluenceDataDocumentApi.
ApplicationDataInfluenceDataInfluenceIdPut(context.TODO(), influenceID, *tiData)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusCreated { // TODO: check more status codes
rspBody = &result
Expand Down Expand Up @@ -194,13 +203,16 @@ func (s *nudrService) AppDataPfdsGet(appIDs []string) (int, interface{}) {
}

result, rsp, err = client.DefaultApi.ApplicationDataPfdsGet(context.TODO(), param)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusOK {
rspBody = &result
Expand Down Expand Up @@ -232,13 +244,16 @@ func (s *nudrService) AppDataPfdsAppIdPut(appID string, pfdDataForApp *models.Pf
client := s.getClient(uri)

result, rsp, err = client.DefaultApi.ApplicationDataPfdsAppIdPut(context.TODO(), appID, *pfdDataForApp)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusOK || rsp.StatusCode == http.StatusCreated {
rspBody = &result
Expand Down Expand Up @@ -269,13 +284,16 @@ func (s *nudrService) AppDataPfdsAppIdDelete(appID string) (int, interface{}) {
client := s.getClient(uri)

rsp, err = client.DefaultApi.ApplicationDataPfdsAppIdDelete(context.TODO(), appID)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if err != nil {
rspCode, rspBody = handleAPIServiceResponseError(rsp, err)
Expand Down Expand Up @@ -305,13 +323,16 @@ func (s *nudrService) AppDataPfdsAppIdGet(appID string) (int, interface{}) {
client := s.getClient(uri)

result, rsp, err = client.DefaultApi.ApplicationDataPfdsAppIdGet(context.TODO(), appID)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusOK {
rspBody = &result
Expand Down Expand Up @@ -345,13 +366,16 @@ func (s *nudrService) AppDataInfluenceDataPatch(

result, rsp, err = client.IndividualInfluenceDataDocumentApi.
ApplicationDataInfluenceDataInfluenceIdPatch(context.Background(), influenceID, *tiSubPatch)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusOK {
rspBody = &result
Expand Down Expand Up @@ -382,13 +406,16 @@ func (s *nudrService) AppDataInfluenceDataDelete(influenceID string) (int, inter

rsp, err = client.IndividualInfluenceDataDocumentApi.
ApplicationDataInfluenceDataInfluenceIdDelete(context.Background(), influenceID)
defer func() {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}()
if rsp != nil {
defer func() {
if rsp.Request.Response != nil {
rsp_err := rsp.Request.Response.Body.Close()
if rsp_err != nil {
logger.ConsumerLog.Errorf("ResponseBody can't be close: %+v", err)
}
}
}()

rspCode = rsp.StatusCode
if rsp.StatusCode == http.StatusOK {
rspBody = &rsp.Body
Expand Down

0 comments on commit 1a990fc

Please sign in to comment.