github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/client/buy.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package client
    21  
    22  import (
    23  	"bytes"
    24  	"encoding/json"
    25  )
    26  
    27  // BuyOptions specifies parameters to buy from the store.
    28  type BuyOptions struct {
    29  	SnapID   string  `json:"snap-id"`
    30  	Price    float64 `json:"price"`
    31  	Currency string  `json:"currency"` // ISO 4217 code as string
    32  }
    33  
    34  // BuyResult holds the state of a buy attempt.
    35  type BuyResult struct {
    36  	State string `json:"state,omitempty"`
    37  }
    38  
    39  func (client *Client) Buy(opts *BuyOptions) (*BuyResult, error) {
    40  	if opts == nil {
    41  		opts = &BuyOptions{}
    42  	}
    43  
    44  	var body bytes.Buffer
    45  	if err := json.NewEncoder(&body).Encode(opts); err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	var result BuyResult
    50  	_, err := client.doSync("POST", "/v2/buy", nil, nil, &body, &result)
    51  
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	return &result, nil
    57  }
    58  
    59  func (client *Client) ReadyToBuy() error {
    60  	var result bool
    61  	_, err := client.doSync("GET", "/v2/buy/ready", nil, nil, nil, &result)
    62  	return err
    63  }