package user

import (
	"context"
	"encoding/json"
	"net/http"

	"repository.basistech.ru/BASIS/dynamix-golang-sdk/v14/internal/validators"
)

// APIListRequest struct for getting API list.
type APIListRequest struct {
	// ID of the user.
	// Required: true
	UserID string `url:"userId" json:"userId" validate:"required"`
}

// APIList gets a list of all API functions that a given user has
// access to according to their apiaccess group membership.
func (u User) APIList(ctx context.Context, req APIListRequest) (*APIsEndpoints, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/user/apiList"

	info := APIsEndpoints{}

	res, err := u.client.DecortApiCall(ctx, http.MethodPost, url, req)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal(res, &info)
	if err != nil {
		return nil, err
	}

	return &info, nil
}
