github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/daemon/api_buy_unsupp.go (about)

     1  /*
     2   * Copyright (C) 2014-2020 Canonical Ltd
     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 version 3 as
     6   * published by the Free Software Foundation.
     7   *
     8   * This program is distributed in the hope that it will be useful,
     9   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11   * GNU General Public License for more details.
    12   *
    13   * You should have received a copy of the GNU General Public License
    14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15   *
    16   */
    17  
    18  package daemon
    19  
    20  import (
    21  	"encoding/json"
    22  	"net/http"
    23  
    24  	"github.com/snapcore/snapd/client"
    25  	"github.com/snapcore/snapd/overlord/auth"
    26  	"github.com/snapcore/snapd/store"
    27  )
    28  
    29  // TODO: this is unsupported and when supported will have a new implementation,
    30  // make these simply return errors and remove all the related code from
    31  // the store package except maybe errors
    32  
    33  var (
    34  	buyCmd = &Command{
    35  		Path: "/v2/buy",
    36  		POST: postBuy,
    37  	}
    38  
    39  	readyToBuyCmd = &Command{
    40  		Path: "/v2/buy/ready",
    41  		GET:  readyToBuy,
    42  	}
    43  )
    44  
    45  func postBuy(c *Command, r *http.Request, user *auth.UserState) Response {
    46  	var opts client.BuyOptions
    47  
    48  	decoder := json.NewDecoder(r.Body)
    49  	err := decoder.Decode(&opts)
    50  	if err != nil {
    51  		return BadRequest("cannot decode buy options from request body: %v", err)
    52  	}
    53  
    54  	s := getStore(c)
    55  
    56  	buyResult, err := s.Buy(&opts, user)
    57  
    58  	if resp := convertBuyError(err); resp != nil {
    59  		return resp
    60  	}
    61  
    62  	return SyncResponse(buyResult, nil)
    63  }
    64  
    65  func readyToBuy(c *Command, r *http.Request, user *auth.UserState) Response {
    66  	s := getStore(c)
    67  
    68  	if resp := convertBuyError(s.ReadyToBuy(user)); resp != nil {
    69  		return resp
    70  	}
    71  
    72  	return SyncResponse(true, nil)
    73  }
    74  
    75  func convertBuyError(err error) Response {
    76  	switch err {
    77  	case nil:
    78  		return nil
    79  	case store.ErrInvalidCredentials:
    80  		return Unauthorized(err.Error())
    81  	case store.ErrUnauthenticated:
    82  		return SyncResponse(&resp{
    83  			Type: ResponseTypeError,
    84  			Result: &errorResult{
    85  				Message: err.Error(),
    86  				Kind:    client.ErrorKindLoginRequired,
    87  			},
    88  			Status: 400,
    89  		}, nil)
    90  	case store.ErrTOSNotAccepted:
    91  		return SyncResponse(&resp{
    92  			Type: ResponseTypeError,
    93  			Result: &errorResult{
    94  				Message: err.Error(),
    95  				Kind:    client.ErrorKindTermsNotAccepted,
    96  			},
    97  			Status: 400,
    98  		}, nil)
    99  	case store.ErrNoPaymentMethods:
   100  		return SyncResponse(&resp{
   101  			Type: ResponseTypeError,
   102  			Result: &errorResult{
   103  				Message: err.Error(),
   104  				Kind:    client.ErrorKindNoPaymentMethods,
   105  			},
   106  			Status: 400,
   107  		}, nil)
   108  	case store.ErrPaymentDeclined:
   109  		return SyncResponse(&resp{
   110  			Type: ResponseTypeError,
   111  			Result: &errorResult{
   112  				Message: err.Error(),
   113  				Kind:    client.ErrorKindPaymentDeclined,
   114  			},
   115  			Status: 400,
   116  		}, nil)
   117  	default:
   118  		return InternalError("%v", err)
   119  	}
   120  }