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

     1  /*
     2   * Copyright (C) 2019 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  	"math/big"
    22  
    23  	"github.com/gin-gonic/gin"
    24  	"github.com/mysteriumnetwork/go-rest/apierror"
    25  
    26  	"github.com/mysteriumnetwork/node/tequilapi/contract"
    27  	"github.com/mysteriumnetwork/node/tequilapi/utils"
    28  )
    29  
    30  // Affiliator represents interface to affiliator service
    31  type Affiliator interface {
    32  	RegistrationTokenReward(token string) (*big.Int, error)
    33  }
    34  
    35  type affiliatorEndpoint struct {
    36  	affiliator Affiliator
    37  }
    38  
    39  // NewAffiliatorEndpoint creates and returns affiliator endpoint
    40  func NewAffiliatorEndpoint(affiliator Affiliator) *affiliatorEndpoint {
    41  	return &affiliatorEndpoint{affiliator: affiliator}
    42  }
    43  
    44  // swagger:operation POST /affiliator/token/{token}/reward AffiliatorTokenReward
    45  //
    46  //	---
    47  //	summary: Returns the amount of reward for a token (affiliator)
    48  //	parameters:
    49  //	- in: path
    50  //	  name: token
    51  //	  description: Token for which to lookup the reward
    52  //	  type: string
    53  //	  required: true
    54  //	responses:
    55  //	  200:
    56  //	    description: Token Reward
    57  //	    schema:
    58  //	      "$ref": "#/definitions/TokenRewardAmount"
    59  //	  500:
    60  //	    description: Internal server error
    61  //	    schema:
    62  //	      "$ref": "#/definitions/APIError"
    63  func (a *affiliatorEndpoint) TokenRewardAmount(c *gin.Context) {
    64  	token := c.Param("token")
    65  	reward, err := a.affiliator.RegistrationTokenReward(token)
    66  	if err != nil {
    67  		utils.ForwardError(c, err, apierror.Internal("Could not fetch reward", contract.ErrCodeAffiliatorFailed))
    68  		return
    69  	}
    70  	if reward == nil {
    71  		c.Error(apierror.Internal("No reward for token", contract.ErrCodeAffiliatorNoReward))
    72  		return
    73  	}
    74  
    75  	utils.WriteAsJSON(contract.TokenRewardAmount{
    76  		Amount: reward,
    77  	}, c.Writer)
    78  }
    79  
    80  // AddRoutesForAffiliator attaches Affiliator endpoints to router
    81  func AddRoutesForAffiliator(affiliator Affiliator) func(*gin.Engine) error {
    82  	a := NewAffiliatorEndpoint(affiliator)
    83  
    84  	return func(e *gin.Engine) error {
    85  		affiliatorGroup := e.Group("/affiliator")
    86  		{
    87  			affiliatorGroup.GET("/token/:token/reward", a.TokenRewardAmount)
    88  		}
    89  		return nil
    90  	}
    91  }