github.com/aiven/aiven-go-client@v1.36.0/card.go (about)

     1  package aiven
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type (
     8  	// Card represents the card model on Aiven.
     9  	Card struct {
    10  		Brand        string   `json:"brand"`
    11  		CardID       string   `json:"card_id"`
    12  		Country      string   `json:"country"`
    13  		CountryCode  string   `json:"country_code"`
    14  		ExpMonth     int      `json:"exp_month"`
    15  		ExpYear      int      `json:"exp_year"`
    16  		Last4        string   `json:"last4"`
    17  		Name         string   `json:"name"`
    18  		ProjectNames []string `json:"projects"`
    19  	}
    20  
    21  	// CardsHandler is the client that interacts with the cards endpoints on
    22  	// Aiven.
    23  	CardsHandler struct {
    24  		client *Client
    25  	}
    26  
    27  	// CardListResponse is the response for listing cards.
    28  	CardListResponse struct {
    29  		APIResponse
    30  		Cards []*Card `json:"cards"`
    31  	}
    32  )
    33  
    34  // List returns all the cards linked to the authenticated account.
    35  func (h *CardsHandler) List() ([]*Card, error) {
    36  	bts, err := h.client.doGetRequest("/card", nil)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	var r CardListResponse
    42  	errR := checkAPIResponse(bts, &r)
    43  
    44  	return r.Cards, errR
    45  }
    46  
    47  // Get card by card ID. The ID may be either last 4 digits of the card or the actual ID
    48  func (h *CardsHandler) Get(cardID string) (*Card, error) {
    49  	if len(cardID) == 0 {
    50  		return nil, nil
    51  	}
    52  
    53  	cards, err := h.List()
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	for _, card := range cards {
    59  		if card.CardID == cardID || card.Last4 == cardID {
    60  			return card, nil
    61  		}
    62  	}
    63  
    64  	err = Error{Message: fmt.Sprintf("Card with ID %v not found", cardID), Status: 404}
    65  	return nil, err
    66  }