account_logins.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  	"encoding/json"
   6  	"time"
   7  
   8  	"github.com/linode/linodego/internal/parseabletime"
   9  )
  10  
  11  type Login struct {
  12  	ID         int        `json:"id"`
  13  	Datetime   *time.Time `json:"datetime"`
  14  	IP         string     `json:"ip"`
  15  	Restricted bool       `json:"restricted"`
  16  	Username   string     `json:"username"`
  17  	Status     string     `json:"status"`
  18  }
  19  
  20  func (c *Client) ListLogins(ctx context.Context, opts *ListOptions) ([]Login, error) {
  21  	return getPaginatedResults[Login](ctx, c, "account/logins", opts)
  22  }
  23  
  24  // UnmarshalJSON implements the json.Unmarshaler interface
  25  func (i *Login) UnmarshalJSON(b []byte) error {
  26  	type Mask Login
  27  
  28  	l := struct {
  29  		*Mask
  30  
  31  		Datetime *parseabletime.ParseableTime `json:"datetime"`
  32  	}{
  33  		Mask: (*Mask)(i),
  34  	}
  35  
  36  	if err := json.Unmarshal(b, &l); err != nil {
  37  		return err
  38  	}
  39  
  40  	i.Datetime = (*time.Time)(l.Datetime)
  41  
  42  	return nil
  43  }
  44  
  45  func (c *Client) GetLogin(ctx context.Context, loginID int) (*Login, error) {
  46  	e := formatAPIPath("account/logins/%d", loginID)
  47  	return doGETRequest[Login](ctx, c, e)
  48  }
  49