profile_apps.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  // ProfileApp represents a ProfileApp object
  12  type ProfileApp struct {
  13  	// When this app was authorized.
  14  	Created *time.Time `json:"-"`
  15  
  16  	// When the app's access to your account expires.
  17  	Expiry *time.Time `json:"-"`
  18  
  19  	// This authorization's ID, used for revoking access.
  20  	ID int `json:"id"`
  21  
  22  	// The name of the application you've authorized.
  23  	Label string `json:"label"`
  24  
  25  	// The OAuth scopes this app was authorized with.
  26  	Scopes string `json:"scopes"`
  27  
  28  	// The URL at which this app's thumbnail may be accessed.
  29  	ThumbnailURL string `json:"thumbnail_url"`
  30  
  31  	// The website where you can get more information about this app.
  32  	Website string `json:"website"`
  33  }
  34  
  35  // UnmarshalJSON implements the json.Unmarshaler interface
  36  func (pa *ProfileApp) UnmarshalJSON(b []byte) error {
  37  	type Mask ProfileApp
  38  
  39  	l := struct {
  40  		*Mask
  41  
  42  		Created *parseabletime.ParseableTime `json:"created"`
  43  		Expiry  *parseabletime.ParseableTime `json:"expiry"`
  44  	}{
  45  		Mask: (*Mask)(pa),
  46  	}
  47  
  48  	if err := json.Unmarshal(b, &l); err != nil {
  49  		return err
  50  	}
  51  
  52  	pa.Created = (*time.Time)(l.Created)
  53  	pa.Expiry = (*time.Time)(l.Expiry)
  54  
  55  	return nil
  56  }
  57  
  58  // GetProfileApp returns the ProfileApp with the provided id
  59  func (c *Client) GetProfileApp(ctx context.Context, appID int) (*ProfileApp, error) {
  60  	e := formatAPIPath("profile/apps/%d", appID)
  61  	return doGETRequest[ProfileApp](ctx, c, e)
  62  }
  63  
  64  // ListProfileApps lists ProfileApps that have access to the Account
  65  func (c *Client) ListProfileApps(ctx context.Context, opts *ListOptions) ([]ProfileApp, error) {
  66  	return getPaginatedResults[ProfileApp](ctx, c, "profile/apps", opts)
  67  }
  68  
  69  // DeleteProfileApp revokes the given ProfileApp's access to the account
  70  func (c *Client) DeleteProfileApp(ctx context.Context, appID int) error {
  71  	e := formatAPIPath("profile/apps/%d", appID)
  72  	return doDELETERequest(ctx, c, e)
  73  }
  74