akerun

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2023 License: MIT Imports: 11 Imported by: 0

README

go-akerun

Akerun API Client for Golang

Go Reference example workflow

Installation

$ go get -u github.com/yshimada0330/go-akerun

Usage

Authenticate

Create a new client for the Akerun API.

$ export CLIENT_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$ export CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$ export REDIRECT_URL=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
clientID := os.Getenv("CLIENT_ID")
clientSecret := os.Getenv("CLIENT_SECRET")
redirectURL := os.Getenv("REDIRECT_URL")
conf := akerun.NewConfig(clientID, clientSecret, redirectURL)
client := akerun.NewClient(conf)

Getting an Authentication Code

url := client.AuthCodeURL("state")
fmt.Println(url)

Creating an Access Token

ctx := context.Background()
token, err := client.Exchange(ctx, authCode)
if err != nil {
    log.Fatal(err)
}
fmt.Println(token.AccessToken)
fmt.Println(token.RefreshToken)

Refresh an Access Token

ctx := context.Background()
token := &oauth2.Token{
    AccessToken:  accessToken,
    RefreshToken: refreshToken,
    Expiry:       time.Now().Add(-time.Hour), # expires
}
reshTokentoken, err := client.RefreshToken(ctx, token)
if err != nil {
    log.Fatal(err)
}
fmt.Println(reshTokentoken.AccessToken)
fmt.Println(reshTokentoken.RefreshToken)

Revoke an Access Token

ctx := context.Background()
err := client.Revoke(ctx, token)
if err != nil {
    log.Fatal(err)
}
Example

Get a list of Organization IDs to which the token owner belongs.

token := &oauth2.Token{
    AccessToken:  token.AccessToken,
    RefreshToken: token.RefreshToken,
}
params := &akerun.OrganizationsParameter{
    Limit: 100,
}
result, err := client.GetOrganizations(ctx, token, *params)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("%#v\n", result)

Documentation

Index

Constants

View Source
const (
	APIUrl         = "https://api.akerun.com"
	APIVerison     = "/v3"
	Oauth2AuthURL  = "https://api.akerun.com/oauth/authorize"
	Oauth2TokenURL = "https://api.akerun.com/oauth/token"
)

Akerun API URLs

Variables

This section is empty.

Functions

This section is empty.

Types

type Akerun added in v0.0.5

type Akerun struct {
	ID                  string `json:"id"`
	Name                string `json:"name"`
	ImageURL            string `json:"image_url"`
	OpenDoorAlert       bool   `json:"open_door_alert"`
	OpenDoorAlertSecond int    `json:"open_door_alert_second"`
	PushButton          bool   `json:"push_button"`
	NormalSoundVolume   int    `json:"normal_sound_volume"`
	AlertSoundVolume    int    `json:"alert_sound_volume"`
	BatteryPercentage   int    `json:"battery_percentage"`
	Autolock            bool   `json:"autolock"`
	AutolockOffSchedule struct {
		StartTime  string `json:"start_time"`
		EndTime    string `json:"end_time"`
		DaysOfWeek []int  `json:"days_of_week"`
	} `json:"autolock_off_schedule"`
	AkerunRemote struct {
		ID string `json:"id"`
	} `json:"akerun_remote"`
	NFCReaderInside struct {
		ID                string `json:"id"`
		BatteryPercentage int    `json:"battery_percentage"`
	} `json:"nfc_reader_inside"`
	NFCReaderOutside struct {
		ID                string `json:"id"`
		BatteryPercentage int    `json:"battery_percentage"`
	} `json:"nfc_reader_outside"`
	DoorSensor struct {
		ID                string `json:"id"`
		BatteryPercentage int    `json:"battery_percentage"`
	} `json:"door_sensor"`
}

type AkerunList added in v0.0.5

type AkerunList struct {
	Akeruns []Akerun `json:"akeruns"`
}

type AkerunListParameter added in v0.0.5

type AkerunListParameter struct {
	Limit     uint32   `url:"limit,omitempty"`
	AkerunIds []string `url:"akerun_ids[],omitempty"`
	IdAfter   string   `url:"id_after,omitempty"`
	IdBefore  string   `url:"id_before,omitempty"`
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents the Akerun client.

func NewClient

func NewClient(config *Config) *Client

NewClient creates a new Akerun client.

func (*Client) AuthCodeURL

func (c *Client) AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string

AuthCodeURL returns a URL to OAuth 2.0 provider's consent page that asks for permissions for the required scopes explicitly.

func (*Client) Exchange

func (c *Client) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)

Exchange converts an authorization code into a token.

func (*Client) GetAkeruns added in v0.0.5

func (c *Client) GetAkeruns(ctx context.Context, oauth2Token *oauth2.Token, organizationId string, params AkerunListParameter) (*AkerunList, error)

func (*Client) GetOrganization

func (c *Client) GetOrganization(ctx context.Context, oauth2Token *oauth2.Token, id string) (*Organization, error)

GetOrganization retrieves the details of an organization with the specified ID.

func (*Client) GetOrganizations

func (c *Client) GetOrganizations(
	ctx context.Context, oauth2Token *oauth2.Token, params OrganizationsParameter) (*OrganizationList, error)

GetOrganizations returns a list of organizations.

func (*Client) GetTokenInfo

func (c *Client) GetTokenInfo(ctx context.Context, token *oauth2.Token) (*TokenInfo, error)

GetTokenInfo retrieves the token information for the given OAuth2 token.

func (*Client) RefreshToken

func (c *Client) RefreshToken(ctx context.Context, token *oauth2.Token) (*oauth2.Token, error)

RefreshToken returns a new token that carries the same authorization as token, but with a renewed access token.

func (*Client) Revoke

func (c *Client) Revoke(ctx context.Context, token *oauth2.Token) error

Revoke revokes the specified OAuth2 token.

type Config

type Config struct {
	APIUrl string
	Oauth2 *oauth2.Config
}

Config represents the configuration for the Akerun client.

func NewConfig

func NewConfig(clientID, clientSecret, redirectURL string) *Config

NewConfig creates a new configuration for the Akerun client.

type Error

type Error struct {
	StatusCode int
	RawError   string
}

Error represents an error returned by the Akerun API.

func (*Error) Error

func (e *Error) Error() string

Error returns the error message.

type Organization

type Organization struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

Organization represents the detailed information of an organization.

type OrganizationList added in v0.0.5

type OrganizationList struct {
	Organizations []id `json:"organizations"`
}

OrganizationList represents a list of organizations in Akerun API.

type OrganizationsParameter added in v0.0.5

type OrganizationsParameter struct {
	Limit    uint32 `url:"limit,omitempty"`
	IdAfter  string `url:"id_after,omitempty"`
	IdBefore string `url:"id_before,omitempty"`
}

OrganizationsParameter represents the parameters for GetOrganizations method.

type TokenInfo

type TokenInfo struct {
	ApplicationName string `json:"application_name"`
	AccessToken     string `json:"access_token"`
	RefreshToken    string `json:"refresh_token"`
	CreatedAt       string `json:"created_at"`
	ExpiresAt       string `json:"expires_at"`
}

TokenInfo represents the structure of the token information.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL