File: identity.go

package info (click to toggle)
golang-github-dnsimple-dnsimple-go 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,428 kB
  • sloc: makefile: 3
file content (51 lines) | stat: -rw-r--r-- 1,320 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package dnsimple

import (
	"context"
)

// IdentityService handles communication with several authentication identity
// methods of the DNSimple API.
//
// See https://developer.dnsimple.com/v2/identity/
type IdentityService struct {
	client *Client
}

// WhoamiData represents an authenticated context
// that contains information about the current logged User and/or Account.
type WhoamiData struct {
	User    *User    `json:"user,omitempty"`
	Account *Account `json:"account,omitempty"`
}

// WhoamiResponse represents a response from an API method that returns a Whoami struct.
type WhoamiResponse struct {
	Response
	Data *WhoamiData `json:"data"`
}

// Whoami gets the current authenticate context.
//
// See https://developer.dnsimple.com/v2/whoami
func (s *IdentityService) Whoami(ctx context.Context) (*WhoamiResponse, error) {
	path := versioned("/whoami")
	whoamiResponse := &WhoamiResponse{}

	resp, err := s.client.get(ctx, path, whoamiResponse)
	if err != nil {
		return nil, err
	}

	whoamiResponse.HTTPResponse = resp
	return whoamiResponse, nil
}

// Whoami is a state-less shortcut to client.Whoami() that returns only the relevant Data.
func Whoami(ctx context.Context, c *Client) (data *WhoamiData, err error) {
	resp, err := c.Identity.Whoami(ctx)
	if resp != nil {
		data = resp.Data
	}
	return
}