akerun

package module
v0.0.0-...-076a76e Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: MIT Imports: 11 Imported by: 0

README

go-akerun

Akerun API Client for Golang

Go Reference workflow

Installation

$ go get -u github.com/Hayao0819/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

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 AkerunGroup

type AkerunGroup struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Memo string `json:"memo"`
}

type AkerunGroupCreateParameter

type AkerunGroupCreateParameter struct {
	Name string `url:"name"`
	Memo string `url:"memo"`
}

type AkerunGroupDetailed

type AkerunGroupDetailed struct {
	ID      string `json:"id"`
	Name    string `json:"name"`
	Memo    string `json:"memo"`
	Akeruns []struct {
		ID       string `json:"id"`
		Name     string `json:"name"`
		ImageURL string `json:"image_url"`
	} `json:"akeruns"`
}

type AkerunGroupList

type AkerunGroupList struct {
	AkerunGroups []AkerunGroup `json:"akerun_groups"`
}

type AkerunGroupUpdateParameter

type AkerunGroupUpdateParameter struct {
	Name string `url:"name"`
	Memo string `url:"memo"`
}

type AkerunList

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

type AkerunListParameter

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) AddAkerunToGroup

func (c *Client) AddAkerunToGroup(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	akerunGroupId string,
	akerunIds ...string,
) error

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) CreateAkerunGroup

func (c *Client) CreateAkerunGroup(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	params AkerunGroupCreateParameter,
) (*AkerunGroup, error)

func (*Client) CreateKey

func (c *Client) CreateKey(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	userId string,
	akerunId string,
	params CreateKeyParameter,
) (*Key, error)

func (*Client) DeleteAkerunGroup

func (c *Client) DeleteAkerunGroup(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	akerunGroupId string,
) error

func (*Client) DeleteKey

func (c *Client) DeleteKey(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	keyId string,
) error

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) ExitUser

func (c *Client) ExitUser(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	userId string,
) error

func (*Client) GetAkerunGroup

func (c *Client) GetAkerunGroup(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	akerunGroupId string,
) (*AkerunGroupDetailed, error)

func (*Client) GetAkerunGroups

func (c *Client) GetAkerunGroups(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
) (*AkerunGroupList, error)

func (*Client) GetAkeruns

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

func (*Client) GetKey

func (c *Client) GetKey(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	params KeyParameter) (*Key, error)

func (*Client) GetKeys

func (c *Client) GetKeys(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	params KeysParameter) (*KeysList, 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) GetUser

func (c *Client) GetUser(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	userId string,
) (*User, error)

func (*Client) GetUsers

func (c *Client) GetUsers(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	params UsersParameter,
) (*UsersList, error)

func (*Client) InviteUser

func (c *Client) InviteUser(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	userId string,
	params InviteUserParameter,
) (*User, error)

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) RegisterUser

func (c *Client) RegisterUser(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	name string,
	params RegisterUserParameter,
) (*User, error)

func (*Client) RemoveAkerunFromGroup

func (c *Client) RemoveAkerunFromGroup(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	akerunGroupId string,
	akerunIds ...string,
) error

func (*Client) Revoke

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

Revoke revokes the specified OAuth2 token.

func (*Client) UpdateAkerunGroup

func (c *Client) UpdateAkerunGroup(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	akerunGroupId string,
	params AkerunGroupUpdateParameter,
) (*AkerunGroup, error)

func (*Client) UpdateKey

func (c *Client) UpdateKey(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	keyId string,
	schedule_type string,
	params UpdateKeyParameter,
) (*Key, error)

func (*Client) UpdateUser

func (c *Client) UpdateUser(
	ctx context.Context,
	oauth2Token *oauth2.Token,
	organizationId string,
	userId string,
	params UpdateUserParameter,
) (*User, error)

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 CreateKeyParameter

type CreateKeyParameter struct {
	ScheduleType      string `url:"schedule_type,omitempty"`
	TemporarySchedule struct {
		StartDateTime string `url:"start_datetime,omitempty"`
		EndDateTime   string `url:"end_datetime,omitempty"`
	} `url:"temporary_schedule,omitempty"`
	RecurringSchedule struct {
		DaysOfWeek []uint32 `url:"days_of_week,omitempty"`
		StartTime  string   `url:"start_time,omitempty"`
		EndTime    string   `url:"end_time,omitempty"`
	} `url:"recurring_schedule,omitempty"`
	EnableKeyUrl   bool   `url:"enable_key_url,omitempty"`
	KeyUrlPassword string `url:"key_url_password,omitempty"`
	Role           string `url:"role,omitempty"`
}

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 InviteUserParameter

type InviteUserParameter struct {
	UserImage     string `url:"user_image,omitempty"`
	UserAuthority string `url:"user_authority,omitempty"`
	UserCode      string `url:"user_code,omitempty"`
}

type Key

type Key struct {
	ID                string `json:"id"`
	Role              string `json:"role"`
	ScheduleType      string `json:"schedule_type"`
	TemporarySchedule struct {
		StartDateTime string `json:"start_datetime"`
		EndDateTime   string `json:"end_datetime"`
	} `json:"temporary_schedule"`
	RecurringSchedule struct {
		DaysOfWeek []uint32 `json:"days_of_week"`
		StartTime  string   `json:"start_time"`
		EndTime    string   `json:"end_time"`
	} `json:"recurring_schedule"`
	Keys struct {
		KeyUrl            string `json:"key_url"`
		PasswordProtected bool   `json:"password_protected"`
	} `json:"keys"`
	Akerun struct {
		ID   string `json:"id"`
		Name string `json:"name"`
	} `json:"akerun"`
	User struct {
		ID   string `json:"id"`
		Name string `json:"name"`
	}
}

type KeyParameter

type KeyParameter struct {
	KeyId string `url:"key_id,omitempty"`
}

type KeysList

type KeysList struct {
	Keys []Key `json:"keys"`
}

type KeysParameter

type KeysParameter struct {
	UserId   string `url:"user_id,omitempty"`
	AkerunId string `url:"akerun_id,omitempty"`
	Limit    uint32 `url:"limit,omitempty"`
	IdAfter  string `url:"id_after,omitempty"`
	IdBefore string `url:"id_before,omitempty"`
}

type NFC

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

type Organization

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

Organization represents the detailed information of an organization.

type OrganizationList

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

OrganizationList represents a list of organizations in Akerun API.

type OrganizationsParameter

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 RegisterUserParameter

type RegisterUserParameter struct {
	UserMail      string `url:"user_mail,omitempty"`
	UserImage     string `url:"user_image,omitempty"`
	UserAuthority string `url:"user_authority,omitempty"`
	UserCode      string `url:"user_code,omitempty"`
}

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.

type UpdateKeyParameter

type UpdateKeyParameter struct {
	TemporarySchedule struct {
		StartDateTime string `url:"start_datetime,omitempty"`
		EndDateTime   string `url:"end_datetime,omitempty"`
	} `url:"temporary_schedule,omitempty"`
	RecurringSchedule struct {
		DaysOfWeek []uint32 `url:"days_of_week,omitempty"`
		StartTime  string   `url:"start_time,omitempty"`
		EndTime    string   `url:"end_time,omitempty"`
	} `url:"recurring_schedule,omitempty"`
	EnableKeyUrl   bool   `url:"enable_key_url,omitempty"`
	KeyUrlPassword string `url:"key_url_password,omitempty"`
	Role           string `url:"role,omitempty"`
}

type UpdateUserParameter

type UpdateUserParameter struct {
	UserName      string `url:"user_name,omitempty"`
	UserMail      string `url:"user_mail,omitempty"`
	UserImage     string `url:"user_image,omitempty"`
	UserAuthority string `url:"user_authority,omitempty"`
	UserCode      string `url:"user_code,omitempty"`
}

type User

type User struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	Mail      string `json:"mail"`
	ImageUrl  string `json:"image_url"`
	Authority string `json:"authority"`
	Code      string `json:"code"`
	CreatedAt string `json:"created_at,omitempty"`
	UpdatedAt string `json:"updated_at,omitempty"`
	Nfcs      []NFC  `json:"nfcs"`
}

type UsersList

type UsersList struct {
	Users []User `json:"users"`
}

type UsersParameter

type UsersParameter struct {
	Limit           uint32 `url:"limit,omitempty"`
	IdAfter         string `url:"id_after,omitempty"`
	IdBefore        string `url:"id_before,omitempty"`
	UserCode        string `url:"user_code,omitempty"`
	UserMail        string `url:"user_mail,omitempty"`
	IncludeDateTime bool   `url:"include_date_time,omitempty"`
}

Jump to

Keyboard shortcuts

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