github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/entertainment.go (about)

     1  /*
     2   * Copyright (C) 2021 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 endpoints
    19  
    20  import (
    21  	"github.com/gin-gonic/gin"
    22  
    23  	"github.com/mysteriumnetwork/node/consumer/entertainment"
    24  	"github.com/mysteriumnetwork/node/tequilapi/contract"
    25  	"github.com/mysteriumnetwork/node/tequilapi/utils"
    26  )
    27  
    28  type entertainmentEndpoint struct {
    29  	estimator estimator
    30  }
    31  
    32  type estimator interface {
    33  	EstimatedEntertainment(myst float64) entertainment.Estimates
    34  }
    35  
    36  // swagger:operation GET /entertainment Entertainment Estimate
    37  //
    38  //	---
    39  //	summary: Estimate entertainment durations/data cap for the MYST amount specified.
    40  //	description: Estimate entertainment durations/data cap for the MYST amount specified.
    41  //	parameters:
    42  //	- name: amount
    43  //	  in: query
    44  //	  description: Amount of MYST to give entertainment estimates for.
    45  //	  type: integer
    46  //	  required: true
    47  //	responses:
    48  //	  200:
    49  //	    description: Entertainment estimates
    50  //	    schema:
    51  //	      "$ref": "#/definitions/EntertainmentEstimateResponse"
    52  //	  500:
    53  //	    description: Internal server error
    54  //	    schema:
    55  //	      "$ref": "#/definitions/APIError"
    56  func (e *entertainmentEndpoint) Estimate(c *gin.Context) {
    57  	req := contract.EntertainmentEstimateRequest{}
    58  	if err := req.Bind(c.Request); err != nil {
    59  		c.Error(err)
    60  		return
    61  	}
    62  
    63  	estimates := e.estimator.EstimatedEntertainment(req.Amount)
    64  	res := contract.EntertainmentEstimateResponse{
    65  		VideoMinutes:    estimates.VideoMinutes,
    66  		MusicMinutes:    estimates.MusicMinutes,
    67  		BrowsingMinutes: estimates.BrowsingMinutes,
    68  		TrafficMB:       estimates.TrafficMB,
    69  		PriceGiB:        estimates.PricePerGiB,
    70  		PriceMin:        estimates.PricePerMin,
    71  	}
    72  	utils.WriteAsJSON(res, c.Writer)
    73  }
    74  
    75  // AddEntertainmentRoutes registers routes for entertainment.
    76  func AddEntertainmentRoutes(estimator estimator) func(*gin.Engine) error {
    77  	endpoint := &entertainmentEndpoint{estimator: estimator}
    78  	return func(e *gin.Engine) error {
    79  		g := e.Group("/entertainment")
    80  		g.GET("", endpoint.Estimate)
    81  		return nil
    82  	}
    83  }