(w http.ResponseWriter, req *http.Request)
| 98 | } |
| 99 | |
| 100 | func (s *Server) combinedOpenAPISpec(w http.ResponseWriter, req *http.Request) error { |
| 101 | // Parse path parameters |
| 102 | ctx := req.Context() |
| 103 | instanceID := req.PathValue("instance_id") |
| 104 | |
| 105 | // Add observability attributes |
| 106 | observability.AddRequestAttributes(ctx) |
| 107 | s.addInstanceRequestAttributes(ctx, instanceID) |
| 108 | |
| 109 | // Only GET request is allowed |
| 110 | if req.Method != http.MethodGet { |
| 111 | return httputil.Error(http.StatusMethodNotAllowed, fmt.Errorf("GET only")) |
| 112 | } |
| 113 | |
| 114 | // Check if user has access to query for API data |
| 115 | if !auth.GetClaims(ctx, instanceID).Can(runtime.ReadAPI) { |
| 116 | return httputil.Errorf(http.StatusForbidden, "does not have access to custom APIs") |
| 117 | } |
| 118 | |
| 119 | apis := make(map[string]*runtimev1.API) |
| 120 | |
| 121 | // Get all built-in APIs |
| 122 | for name, api := range runtime.BuiltinAPIs { |
| 123 | apis[name] = api |
| 124 | } |
| 125 | |
| 126 | // Get all custom APIs |
| 127 | ctrl, err := s.runtime.Controller(ctx, instanceID) |
| 128 | if err != nil { |
| 129 | return httputil.Error(http.StatusBadRequest, err) |
| 130 | } |
| 131 | |
| 132 | list, err := ctrl.List(ctx, runtime.ResourceKindAPI, "", false) |
| 133 | if err != nil { |
| 134 | return err |
| 135 | } |
| 136 | |
| 137 | for _, res := range list { |
| 138 | apis[res.Meta.Name.Name] = res.GetApi() |
| 139 | } |
| 140 | |
| 141 | // Generate the OpenAPI spec |
| 142 | combinedAPISpec, err := s.generateOpenAPISpec(ctx, instanceID, apis) |
| 143 | if err != nil { |
| 144 | return httputil.Error(http.StatusInternalServerError, err) |
| 145 | } |
| 146 | |
| 147 | // Check accepted format of the OpenAPI spec: JSON or YAML |
| 148 | accept := req.Header.Get("Accept") |
| 149 | |
| 150 | // YAML |
| 151 | if accept == "application/yaml" || accept == "application/x-yaml" { |
| 152 | data, err := yaml.Marshal(combinedAPISpec) |
| 153 | if err != nil { |
| 154 | return httputil.Error(http.StatusInternalServerError, err) |
| 155 | } |
| 156 | |
| 157 | w.Header().Set("Content-Type", "application/yaml") |
nothing calls this directly
no test coverage detected