github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/market/mysterium/dto.go (about)

     1  /*
     2   * Copyright (C) 2017 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 mysterium
    19  
    20  import (
    21  	"fmt"
    22  	"net/url"
    23  	"strconv"
    24  
    25  	"github.com/mysteriumnetwork/node/nat"
    26  )
    27  
    28  // ProposalsQuery represents URL query for proposal listing
    29  type ProposalsQuery struct {
    30  	ProviderID      string
    31  	ProviderIDs     []string
    32  	ServiceType     string
    33  	LocationCountry string
    34  
    35  	CompatibilityMin, CompatibilityMax int
    36  	AccessPolicy, AccessPolicySource   string
    37  
    38  	IPType                  string
    39  	NATCompatibility        nat.NATType
    40  	BandwidthMin            float64
    41  	QualityMin              float32
    42  	IncludeMonitoringFailed bool
    43  	PresetID                int
    44  }
    45  
    46  // ToURLValues converts the query to url.Values.
    47  func (q ProposalsQuery) ToURLValues() url.Values {
    48  	values := url.Values{}
    49  	if q.ProviderID != "" {
    50  		values.Set("provider_id", q.ProviderID)
    51  	}
    52  	for _, p := range q.ProviderIDs {
    53  		values.Add("provider_id", p)
    54  	}
    55  	if q.ServiceType != "" {
    56  		values.Set("service_type", q.ServiceType)
    57  	}
    58  	if q.LocationCountry != "" {
    59  		values.Set("location_country", q.LocationCountry)
    60  	}
    61  	if q.IPType != "" {
    62  		values.Set("ip_type", q.IPType)
    63  	}
    64  	if q.NATCompatibility != "" {
    65  		values.Set("nat_compatibility", string(q.NATCompatibility))
    66  	}
    67  	if !(q.CompatibilityMin == 0 && q.CompatibilityMax == 0) {
    68  		values.Set("compatibility_min", strconv.Itoa(q.CompatibilityMin))
    69  		values.Set("compatibility_max", strconv.Itoa(q.CompatibilityMax))
    70  	}
    71  	if q.AccessPolicy != "" {
    72  		values.Set("access_policy", q.AccessPolicy)
    73  	}
    74  	if q.AccessPolicySource != "" {
    75  		values.Set("access_policy_source", q.AccessPolicySource)
    76  	}
    77  	if q.BandwidthMin > 0 {
    78  		values.Set("bandwidth_min", fmt.Sprintf("%.2f", q.BandwidthMin))
    79  	}
    80  	if q.QualityMin != 0 {
    81  		values.Set("quality_min", fmt.Sprintf("%.2f", q.QualityMin))
    82  	}
    83  	if q.IncludeMonitoringFailed {
    84  		values.Set("include_monitoring_failed", fmt.Sprint(q.IncludeMonitoringFailed))
    85  	}
    86  	if q.PresetID != 0 {
    87  		values.Set("preset_id", strconv.Itoa(q.PresetID))
    88  	}
    89  
    90  	return values
    91  }