github.com/cs3org/reva/v2@v2.27.7/pkg/mentix/accservice/accservice.go (about)

     1  // Copyright 2018-2020 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 accservice
    20  
    21  import (
    22  	"encoding/json"
    23  	"fmt"
    24  	"net/url"
    25  	"path"
    26  	"strings"
    27  
    28  	"github.com/pkg/errors"
    29  
    30  	"github.com/cs3org/reva/v2/pkg/mentix/config"
    31  	"github.com/cs3org/reva/v2/pkg/mentix/utils/network"
    32  )
    33  
    34  // RequestResponse holds the response of an accounts service query.
    35  type RequestResponse struct {
    36  	Success bool
    37  	Error   string
    38  	Data    interface{}
    39  }
    40  
    41  type accountsServiceSettings struct {
    42  	URL      *url.URL
    43  	User     string
    44  	Password string
    45  }
    46  
    47  var settings accountsServiceSettings
    48  
    49  // Query performs an account service query.
    50  func Query(endpoint string, params network.URLParams) (*RequestResponse, error) {
    51  	fullURL, err := network.GenerateURL(fmt.Sprintf("%v://%v", settings.URL.Scheme, settings.URL.Host), path.Join(settings.URL.Path, endpoint), params)
    52  	if err != nil {
    53  		return nil, errors.Wrap(err, "error while building the service accounts query URL")
    54  	}
    55  
    56  	data, err := network.ReadEndpoint(fullURL, &network.BasicAuth{User: settings.User, Password: settings.Password}, false)
    57  	if err != nil {
    58  		return nil, errors.Wrap(err, "unable to query the service accounts endpoint")
    59  	}
    60  
    61  	resp := &RequestResponse{}
    62  	if err := json.Unmarshal(data, resp); err != nil {
    63  		return nil, errors.Wrap(err, "unable to unmarshal response data")
    64  	}
    65  	return resp, nil
    66  }
    67  
    68  // GetResponseValue gets a value from an account service query using a dotted path notation.
    69  func GetResponseValue(resp *RequestResponse, path string) interface{} {
    70  	if data, ok := resp.Data.(map[string]interface{}); ok {
    71  		tokens := strings.Split(path, ".")
    72  		for i, name := range tokens {
    73  			if i == len(tokens)-1 {
    74  				if value, ok := data[name]; ok {
    75  					return value
    76  				}
    77  			}
    78  
    79  			if data, ok = data[name].(map[string]interface{}); !ok {
    80  				break
    81  			}
    82  		}
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  // InitAccountsService initializes the global accounts service.
    89  func InitAccountsService(conf *config.Configuration) error {
    90  	URL, err := url.Parse(conf.AccountsService.URL)
    91  	if err != nil {
    92  		return errors.Wrap(err, "unable to parse the accounts service URL")
    93  	}
    94  
    95  	settings.URL = URL
    96  	settings.User = conf.AccountsService.User
    97  	settings.Password = conf.AccountsService.Password
    98  
    99  	return nil
   100  }