github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/discovery/apidiscovery/repository.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package apidiscovery
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/mysteriumnetwork/node/core/discovery/proposal"
    24  	"github.com/mysteriumnetwork/node/market"
    25  	"github.com/mysteriumnetwork/node/market/mysterium"
    26  )
    27  
    28  type apiRepository struct {
    29  	discoveryAPI *mysterium.MysteriumAPI
    30  }
    31  
    32  // NewRepository constructs a new proposal repository (backed by API).
    33  func NewRepository(api *mysterium.MysteriumAPI) *apiRepository {
    34  	return &apiRepository{discoveryAPI: api}
    35  }
    36  
    37  // Proposal returns proposal by ID.
    38  func (a *apiRepository) Proposal(id market.ProposalID) (*market.ServiceProposal, error) {
    39  	proposals, err := a.discoveryAPI.QueryProposals(mysterium.ProposalsQuery{
    40  		ProviderID:              id.ProviderID,
    41  		ServiceType:             id.ServiceType,
    42  		AccessPolicy:            "all",
    43  		IncludeMonitoringFailed: true,
    44  	})
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	if len(proposals) != 1 {
    49  		return nil, fmt.Errorf("proposal does not exist: %+v", id)
    50  	}
    51  	return &proposals[0], nil
    52  }
    53  
    54  // Proposals returns proposals matching filter.
    55  func (a *apiRepository) Proposals(filter *proposal.Filter) ([]market.ServiceProposal, error) {
    56  	proposals, err := a.discoveryAPI.QueryProposals(filter.ToAPIQuery())
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	filteredProposals := []market.ServiceProposal{}
    62  	for _, p := range proposals {
    63  		if filter.Matches(p) {
    64  			filteredProposals = append(filteredProposals, p)
    65  		}
    66  	}
    67  
    68  	return filteredProposals, nil
    69  }
    70  
    71  // Countries returns number of proposals matching filter per country.
    72  func (a *apiRepository) Countries(filter *proposal.Filter) (map[string]int, error) {
    73  	return a.discoveryAPI.QueryCountries(filter.ToAPIQuery())
    74  }