profile_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  // ProfileLogin represents a Profile object
  12  type ProfileLogin struct {
  13  	Datetime   *time.Time `json:"datetime"`
  14  	ID         int        `json:"id"`
  15  	IP         string     `json:"ip"`
  16  	Restricted bool       `json:"restricted"`
  17  	Status     string     `json:"status"`
  18  	Username   string     `json:"username"`
  19  }
  20  
  21  // UnmarshalJSON implements the json.Unmarshaler interface
  22  func (i *ProfileLogin) UnmarshalJSON(b []byte) error {
  23  	type Mask ProfileLogin
  24  
  25  	l := struct {
  26  		*Mask
  27  
  28  		Datetime *parseabletime.ParseableTime `json:"datetime"`
  29  	}{
  30  		Mask: (*Mask)(i),
  31  	}
  32  
  33  	if err := json.Unmarshal(b, &l); err != nil {
  34  		return err
  35  	}
  36  
  37  	i.Datetime = (*time.Time)(l.Datetime)
  38  
  39  	return nil
  40  }
  41  
  42  // GetProfileLogin returns the Profile Login of the authenticated user
  43  func (c *Client) GetProfileLogin(ctx context.Context, id int) (*ProfileLogin, error) {
  44  	e := formatAPIPath("profile/logins/%d", id)
  45  	return doGETRequest[ProfileLogin](ctx, c, e)
  46  }
  47  
  48  // ListProfileLogins lists Profile Logins of the authenticated user
  49  func (c *Client) ListProfileLogins(ctx context.Context, opts *ListOptions) ([]ProfileLogin, error) {
  50  	return getPaginatedResults[ProfileLogin](ctx, c, "profile/logins", opts)
  51  }
  52