CI: Add silent prescan SonarCloud job
[nonrtric/plt/sme.git] / provider / handler / getapi_handler.go
1 // -
2 //
3 //      ========================LICENSE_START=================================
4 //      O-RAN-SC
5 //      %%
6 //      Copyright (C) 2023: Nordix Foundation
7 //      %%
8 //      Licensed under the Apache License, Version 2.0 (the "License");
9 //      you may not use this file except in compliance with the License.
10 //      You may obtain a copy of the License at
11 //
12 //           http://www.apache.org/licenses/LICENSE-2.0
13 //
14 //      Unless required by applicable law or agreed to in writing, software
15 //      distributed under the License is distributed on an "AS IS" BASIS,
16 //      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 //      See the License for the specific language governing permissions and
18 //      limitations under the License.
19 //      ========================LICENSE_END===================================
20 package handler
21
22 import (
23         "encoding/json"
24         "fmt"
25         "net/http"
26
27         log "github.com/sirupsen/logrus"
28
29         "github.com/labstack/echo/v4"
30         "oransc.org/nonrtric/capifprov/internal/publishserviceapi"
31 )
32
33 func GetApiRequest(server string) echo.HandlerFunc {
34         return func(c echo.Context) error {
35
36                 aefId := c.FormValue("apfId")
37                 if aefId == "" {
38                         return c.Render(http.StatusOK, "getapi.html", map[string]interface{}{
39                                 "isResponse": false,
40                                 "isError":    false,
41                         })
42                 }
43
44                 //server format: http://localhost:8090
45                 url := server + "/published-apis/v1/" + aefId + "/service-apis"
46                 log.Infof("[Get API] to %v for aefId: %v", url, aefId)
47
48                 headers := map[string]string{
49                         "Content-Type": "text/plain",
50                 }
51                 resp, err := makeRequest("GET", url, headers, nil)
52                 if err != nil {
53                         log.Errorf("[Get API] %v", fmt.Sprintf("error: %v", err))
54                         return c.Render(http.StatusBadRequest, "getapi.html", map[string]interface{}{
55                                 "response":   fmt.Sprintf("error: %v", err),
56                                 "isError":    true,
57                                 "isResponse": false,
58                         })
59                 }
60                 log.Infof("[Get API] Response from service: %+v error: %v\n", string(resp), err)
61
62                 var resAPIs []publishserviceapi.ServiceAPIDescription
63                 err = json.Unmarshal(resp, &resAPIs)
64                 if err != nil {
65                         log.Error("[Get API] error unmarshaling parameter ServiceAPIDescription as JSON")
66                         return c.Render(http.StatusBadRequest, "getapi.html", map[string]interface{}{
67                                 "isResponse": false,
68                                 "isError":    true,
69                                 "response":   "error unmarshaling parameter ServiceAPIDescription as JSON",
70                         })
71                 }
72
73                 bytes, _ := json.Marshal(resAPIs)
74                 log.Infof("[Get API] There are %v ServiceAPIDescription objects available", len(resAPIs))
75                 return c.Render(http.StatusOK, "getapi.html", map[string]interface{}{
76                         "isResponse": true,
77                         "response":   string(bytes),
78                 })
79         }
80 }