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===================================
28 "github.com/labstack/echo/v4"
29 log "github.com/sirupsen/logrus"
30 "oransc.org/nonrtric/capifinvoker/internal/discoverserviceapi"
33 func DiscoverAPIs(server string) echo.HandlerFunc {
34 return func(c echo.Context) error {
36 invokerId := c.FormValue("api-invoker-id")
38 return c.Render(http.StatusOK, "discovery.html", map[string]interface{}{
44 //server format: http://localhost:8090
45 urlStr, _ := url.Parse(server + "/service-apis/v1/allServiceAPIs")
46 log.Infof("[Discovery API] apis to %v for invokerId: %v", urlStr, invokerId)
48 //TODO check how to remove empty parameters
49 c.Request().ParseForm()
50 params := c.Request().Form
51 for key, val := range params {
52 if len(val) == 0 || val[0] == "" {
56 urlStr.RawQuery = params.Encode()
58 headers := map[string]string{
59 "Content-Type": "text/plain",
61 resp, err := makeRequest("GET", urlStr.String(), headers, nil)
63 log.Errorf("[Discovery API] %v", fmt.Sprintf("error: %v", err))
64 return c.Render(http.StatusBadRequest, "discovery.html", map[string]interface{}{
65 "response": fmt.Sprintf("error: %v", err),
70 log.Infof("[Discovery API] Response from service: %+v error: %v\n", string(resp), err)
72 var resAPIs discoverserviceapi.DiscoveredAPIs
73 err = json.Unmarshal(resp, &resAPIs)
75 log.Error("[Discovery API] error unmarshaling parameter DiscoveredAPIs as JSON")
76 return c.Render(http.StatusBadRequest, "discovery.html", map[string]interface{}{
79 "response": "error unmarshaling parameter []DiscoveredAPIs as JSON",
83 if len(*resAPIs.ServiceAPIDescriptions) == 0 {
84 log.Info("[Discovery API] There are no APIs availables for the specified parameters.")
85 return c.Render(http.StatusOK, "discovery.html", map[string]interface{}{
89 "response": "There are no APIs availables for the specified parameters.",
93 bytes, _ := json.Marshal(resAPIs)
94 return c.Render(http.StatusOK, "discovery.html", map[string]interface{}{
96 "response": string(bytes),