client

package module
v0.0.0-...-761ca00 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2023 License: MIT Imports: 9 Imported by: 0

README

open-hue/go

Community-driven Go API Client for Philips Hue.

Disclaimer

The code is mostly auto-generated from the OpenAPI Spec and it is currently under active development.

Usage

It is quite early stage but it is functional to the single endpoint implemented.

package main

import (
  "fmt"
  "context"
  hue "github.com/open-hue/go"
)

func main() {
  c, _ := hue.NewAuthenticatedClientWithResponses("https://10.0.0.23", "MyAppKey")
  resp, _ := c.GetLightsWithResponse(context.Background())
  lights := (*resp.JSON200).Data
  for _, l := range lights {
    fmt.Printf("Light {ID:\"%s\", Name: \"%s\"}\n", l.Id, l.Metadata.Name)
  }
}
Built-in examples

There are examples for each endpoint in the examples/ folder.

Most assume environment variables for configuration to simplify things, e.g.:

export HUE_SERVER="https://10.20.30.40"
export HUE_APPLICATION_KEY="this-is-clearly-not-valid"

go run examples/lights/all/main.go

Results in:

Light {ID:"c8sb0f94-50c7-20fb-9070-a063a91bfc00", Name: "Hue lamp 1"}
Light {ID:"71bje066-5ded-22e9-9ekd-5f5f40e6a8ae", Name: "Hue lamp 2"}
Light {ID:"8039b831-d937-282a-83f5-0bd3ea97dac3", Name: "Hue lamp 3"}
Light {ID:"5be4a6f4-4504-2c64-b7f6-dfe3004ff7f3", Name: "LED Strip 1"}

Local development

Directory
├── LICENSE       - Self-explanatory
├── Makefile      - Aliases for internal relevant commands
├── README.md     - This document
├── client.gen.go - Generated golang code
├── client.go     - Configuration
├── go.mod        - Go mod
├── go.sum        - Go dependencies lock
└── spec/         - Sub-module from open-hue/spec
    └── spec.yaml - OpenAPI Spec
Makefile
help         Lists help commands
go/build     Builds client code
go/generate  Generates client code from OpenAPI specification
go/update    Updates go dependencies
spec/update  Refreshes the OpenAPI specification from its source
setup        Installs dependencies
Updating Spec

The spec is managed within open-hue/spec repository. This repository has a git sub-module with a static reference to prevent creating inconsistent builds. All updadtes to the spec must be intentionally pulled. The idea is to eventually pin static releases from the specification repository.

To upddate the internal sub-module reference:

  1. make spec/update
  2. make go/generate

Check for diffs, if there are any relevant changes it means the spec changed and thus the code must be regenerated & recompiled.

Documentation

Overview

Package client provides primitives to interact with the openapi HTTP API.

Code generated by github.com/deepmap/oapi-codegen version v1.13.4 DO NOT EDIT.

Index

Constants

View Source
const ApplicationKeyHeader = "hue-application-key"

ApplicationKeyHeader is used to authenticate requests from the client to Philips Hue Bridge

View Source
const (
	ApplicationKeyScopes = "ApplicationKey.Scopes"
)

Variables

This section is empty.

Functions

func NewGetLightByIdRequest

func NewGetLightByIdRequest(server string, id Id) (*http.Request, error)

NewGetLightByIdRequest generates requests for GetLightById

func NewGetLightsRequest

func NewGetLightsRequest(server string) (*http.Request, error)

NewGetLightsRequest generates requests for GetLights

Types

type Client

type Client struct {
	// The endpoint of the server conforming to this interface, with scheme,
	// https://api.deepmap.com for example. This can contain a path relative
	// to the server, such as https://api.deepmap.com/dev-test, and all the
	// paths in the swagger spec will be appended to the server.
	Server string

	// Doer for performing requests, typically a *http.Client with any
	// customized settings, such as certificate chains.
	Client HttpRequestDoer

	// A list of callbacks for modifying requests which are generated before sending over
	// the network.
	RequestEditors []RequestEditorFn
}

Client which conforms to the OpenAPI3 specification for this service.

func NewAuthenticatedClient

func NewAuthenticatedClient(server string, applicationKey string) (*Client, error)

Creates an authenticacted client with all required options

func NewClient

func NewClient(server string, opts ...ClientOption) (*Client, error)

Creates a new Client, with reasonable defaults

func (*Client) GetLightById

func (c *Client) GetLightById(ctx context.Context, id Id, reqEditors ...RequestEditorFn) (*http.Response, error)

func (*Client) GetLights

func (c *Client) GetLights(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error)

type ClientInterface

type ClientInterface interface {
	// GetLights request
	GetLights(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error)

	// GetLightById request
	GetLightById(ctx context.Context, id Id, reqEditors ...RequestEditorFn) (*http.Response, error)
}

The interface specification for the client above.

type ClientOption

type ClientOption func(*Client) error

ClientOption allows setting custom parameters during construction

func WithBaseURL

func WithBaseURL(baseURL string) ClientOption

WithBaseURL overrides the baseURL.

func WithHTTPClient

func WithHTTPClient(doer HttpRequestDoer) ClientOption

WithHTTPClient allows overriding the default Doer, which is automatically created using http.Client. This is useful for tests.

func WithRequestEditorFn

func WithRequestEditorFn(fn RequestEditorFn) ClientOption

WithRequestEditorFn allows setting up a callback function, which will be called right before sending the request. This can be used to mutate the request.

type ClientWithResponses

type ClientWithResponses struct {
	ClientInterface
}

ClientWithResponses builds on ClientInterface to offer response payloads

func NewAuthenticatedClientWithResponses

func NewAuthenticatedClientWithResponses(server string, applicationKey string) (*ClientWithResponses, error)

Creates an authenticacted client with responses and with all required options

func NewClientWithResponses

func NewClientWithResponses(server string, opts ...ClientOption) (*ClientWithResponses, error)

NewClientWithResponses creates a new ClientWithResponses, which wraps Client with return type handling

func (*ClientWithResponses) GetLightByIdWithResponse

func (c *ClientWithResponses) GetLightByIdWithResponse(ctx context.Context, id Id, reqEditors ...RequestEditorFn) (*GetLightByIdResponse, error)

GetLightByIdWithResponse request returning *GetLightByIdResponse

func (*ClientWithResponses) GetLightsWithResponse

func (c *ClientWithResponses) GetLightsWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetLightsResponse, error)

GetLightsWithResponse request returning *GetLightsResponse

type ClientWithResponsesInterface

type ClientWithResponsesInterface interface {
	// GetLightsWithResponse request
	GetLightsWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetLightsResponse, error)

	// GetLightByIdWithResponse request
	GetLightByIdWithResponse(ctx context.Context, id Id, reqEditors ...RequestEditorFn) (*GetLightByIdResponse, error)
}

ClientWithResponsesInterface is the interface specification for the client with responses above.

type Error

type Error struct {
	// Description The description of the error
	Description string `json:"description"`
}

Error Error object

type ErrorsResponse

type ErrorsResponse struct {
	// Errors The error objects
	Errors []Error `json:"errors"`
}

ErrorsResponse Error response

type GetLightByIdResponse

type GetLightByIdResponse struct {
	Body         []byte
	HTTPResponse *http.Response
	JSON200      *LightsResponse
	JSONDefault  *ErrorsResponse
}

func ParseGetLightByIdResponse

func ParseGetLightByIdResponse(rsp *http.Response) (*GetLightByIdResponse, error)

ParseGetLightByIdResponse parses an HTTP response from a GetLightByIdWithResponse call

func (GetLightByIdResponse) Status

func (r GetLightByIdResponse) Status() string

Status returns HTTPResponse.Status

func (GetLightByIdResponse) StatusCode

func (r GetLightByIdResponse) StatusCode() int

StatusCode returns HTTPResponse.StatusCode

type GetLightsResponse

type GetLightsResponse struct {
	Body         []byte
	HTTPResponse *http.Response
	JSON200      *LightsResponse
	JSONDefault  *ErrorsResponse
}

func ParseGetLightsResponse

func ParseGetLightsResponse(rsp *http.Response) (*GetLightsResponse, error)

ParseGetLightsResponse parses an HTTP response from a GetLightsWithResponse call

func (GetLightsResponse) Status

func (r GetLightsResponse) Status() string

Status returns HTTPResponse.Status

func (GetLightsResponse) StatusCode

func (r GetLightsResponse) StatusCode() int

StatusCode returns HTTPResponse.StatusCode

type HttpRequestDoer

type HttpRequestDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

Doer performs HTTP requests.

The standard http.Client implements this interface.

type Id

type Id = string

Id The id of the resource

type Light

type Light struct {
	// Id The id of the resource
	Id Id `json:"id"`

	// Metadata The metadadta for the light resource
	Metadata LightMetadata `json:"metadata"`
}

Light Light object

type LightMetadata

type LightMetadata struct {
	// Name The name of the light resource
	Name string `json:"name"`
}

LightMetadata The metadadta for the light resource

type LightsResponse

type LightsResponse struct {
	// Data Content of the response
	Data []Light `json:"data"`
}

LightsResponse Light response

type RequestEditorFn

type RequestEditorFn func(ctx context.Context, req *http.Request) error

RequestEditorFn is the function signature for the RequestEditor callback function

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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