github.com/cs3org/reva/v2@v2.27.7/internal/http/services/sciencemesh/providers.go (about)

     1  // Copyright 2018-2023 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package sciencemesh
    20  
    21  import (
    22  	"encoding/json"
    23  	"errors"
    24  	"net/http"
    25  	"strings"
    26  
    27  	gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
    28  	providerpb "github.com/cs3org/go-cs3apis/cs3/ocm/provider/v1beta1"
    29  	rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
    30  	"github.com/cs3org/reva/v2/internal/http/services/reqres"
    31  	"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
    32  )
    33  
    34  type providersHandler struct {
    35  	gatewaySelector *pool.Selector[gateway.GatewayAPIClient]
    36  }
    37  
    38  func (h *providersHandler) init(c *config) error {
    39  	var err error
    40  	h.gatewaySelector, err = pool.GatewaySelector(c.GatewaySvc)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  type provider struct {
    49  	FullName string `json:"full_name"`
    50  	Domain   string `json:"domain"`
    51  }
    52  
    53  // ListProviders lists all the providers filtering by the `search` query parameter.
    54  func (h *providersHandler) ListProviders(w http.ResponseWriter, r *http.Request) {
    55  	ctx := r.Context()
    56  	term := strings.ToLower(r.URL.Query().Get("search"))
    57  
    58  	gc, err := h.gatewaySelector.Next()
    59  	if err != nil {
    60  		reqres.WriteError(w, r, reqres.APIErrorServerError, "error selecting gateway client", err)
    61  		return
    62  	}
    63  	listRes, err := gc.ListAllProviders(ctx, &providerpb.ListAllProvidersRequest{})
    64  	if err != nil {
    65  		reqres.WriteError(w, r, reqres.APIErrorServerError, "error listing all providers", err)
    66  		return
    67  	}
    68  
    69  	if listRes.Status.Code != rpc.Code_CODE_OK {
    70  		reqres.WriteError(w, r, reqres.APIErrorServerError, listRes.Status.Message, errors.New(listRes.Status.Message))
    71  		return
    72  	}
    73  
    74  	filtered := []*provider{}
    75  	for _, p := range listRes.Providers {
    76  		if strings.Contains(strings.ToLower(p.FullName), term) ||
    77  			strings.Contains(strings.ToLower(p.Domain), term) {
    78  			filtered = append(filtered, &provider{
    79  				FullName: p.FullName,
    80  				Domain:   p.Domain,
    81  			})
    82  		}
    83  	}
    84  
    85  	if err := json.NewEncoder(w).Encode(filtered); err != nil {
    86  		reqres.WriteError(w, r, reqres.APIErrorServerError, "error encoding response in json", err)
    87  		return
    88  	}
    89  
    90  	w.Header().Set("Content-Type", "application/json")
    91  	w.WriteHeader(http.StatusOK)
    92  }