3 // ========================LICENSE_START=================================
6 // Copyright (C) 2023: Nordix Foundation
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
12 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
27 "github.com/labstack/echo/v4"
28 log "github.com/sirupsen/logrus"
29 "oransc.org/nonrtric/capifprov/internal/publishserviceapi"
32 func PublishapiHandler(c echo.Context) error {
33 return c.Render(http.StatusOK, "publishapi.html", map[string]interface{}{
39 func PublishApiFormHandler(server string) echo.HandlerFunc {
40 return func(c echo.Context) error {
42 apfId := c.FormValue("apfId")
44 return c.Render(http.StatusBadRequest, "publishapi.html", map[string]interface{}{
47 "response": "field apfId is needed",
51 //server format: http://localhost:8090
52 url := server + "/published-apis/v1/" + apfId + "/service-apis"
54 log.Infof("[Publish API] url to capif core %v for aefId: %v", url, apfId)
55 var apiDescription publishserviceapi.ServiceAPIDescription
57 err := json.Unmarshal([]byte(c.FormValue("apiDescription")), &apiDescription)
59 log.Error("[Publish API] error unmarshaling parameter ServiceAPIDescription as JSON")
60 return c.Render(http.StatusBadRequest, "publishapi.html", map[string]interface{}{
63 "response": "error unmarshaling parameter ServiceAPIDescription as JSON",
67 headers := map[string]string{
68 "Content-Type": "application/json",
70 resp, err := makeRequest("POST", url, headers, apiDescription)
72 log.Errorf("[Publish API] %v", fmt.Sprintf("error: %v", err))
73 return c.Render(http.StatusBadRequest, "publishapi.html", map[string]interface{}{
76 "response": fmt.Sprintf("error: %v", err),
80 var resAPI publishserviceapi.ServiceAPIDescription
81 err = json.Unmarshal(resp, &resAPI)
83 log.Error("[Publish API] error unmarshaling parameter ServiceAPIDescription as JSON")
84 return c.Render(http.StatusBadRequest, "publishapi.html", map[string]interface{}{
87 "response": "Error unmarshaling parameter ServiceAPIDescription as JSON",
91 bytes, _ := json.Marshal(resAPI)
92 log.Infof("[Publish API] API %v with the id: %v has been register", resAPI.ApiName, *resAPI.ApiId)
93 return c.Render(http.StatusOK, "publishapi.html", map[string]interface{}{
95 "response": string(bytes),