client

package
v2.11.1 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a transport for API calls.

func NewClient

func NewClient(endpointURL *url.URL, opts ...Option) *Client

NewClient Create new client of DaData. Api and secret keys see on profile page (https://dadata.ru/profile/). By default client uses `DADATA_API_KEY` and `DADATA_SECRET_KEY` environment variables.

Example
var err error
endpointUrl, err := url.Parse("https://suggestions.dadata.ru/suggestions/api/4_1/rs/")
if err != nil {
	return
}

api := suggest.Api{
	Client: NewClient(endpointUrl),
}

params := suggest.RequestParams{
	Query: "ул Свободы",
}

suggestions, err := api.Address(context.Background(), &params)
if err != nil {
	return
}

for _, s := range suggestions {
	fmt.Printf("%s", s.Value)
}
Output:

func (*Client) Get

func (c *Client) Get(ctx context.Context, apiMethod string, params url.Values, result interface{}) (err error)

func (*Client) Post

func (c *Client) Post(ctx context.Context, apiMethod string, body, result interface{}) (err error)

type CredentialProvider

type CredentialProvider interface {
	ApiKey() string
	SecretKey() string
}

CredentialProvider provides DaData API access keys.

type Credentials

type Credentials struct {
	ApiKeyValue    string
	SecretKeyValue string
}

Credentials provides constant credential values.

Example
var err error
endpointUrl, err := url.Parse("https://suggestions.dadata.ru/suggestions/api/4_1/rs/")
if err != nil {
	return
}

creds := Credentials{
	ApiKeyValue:    "<YOUR_API_KEY>",
	SecretKeyValue: "<YOUR_SECRET_KEY>",
}

api := suggest.Api{
	Client: NewClient(endpointUrl, WithCredentialProvider(&creds)),
}

params := suggest.RequestParams{
	Query: "ул Свободы",
}

suggestions, err := api.Address(context.Background(), &params)
if err != nil {
	return
}

for _, s := range suggestions {
	fmt.Printf("%s", s.Value)
}
Output:

func (*Credentials) ApiKey

func (c *Credentials) ApiKey() string

func (*Credentials) SecretKey

func (c *Credentials) SecretKey() string

type EnvironmentCredentials

type EnvironmentCredentials struct {
	ApiKeyName    string
	SecretKeyName string
}

EnvironmentCredentials provides credentials from environment variables.

func (*EnvironmentCredentials) ApiKey

func (c *EnvironmentCredentials) ApiKey() string

func (*EnvironmentCredentials) SecretKey

func (c *EnvironmentCredentials) SecretKey() string

type Option

type Option func(opts *clientOptions)

Option applies some option to clientOptions.

func WithCredentialProvider

func WithCredentialProvider(c CredentialProvider) Option

WithCredentialProvider sets credential provider.

func WithDecoderFactory

func WithDecoderFactory(f transport.DecoderFactory) Option

func WithEncoderFactory

func WithEncoderFactory(f transport.EncoderFactory) Option
Example
var err error
endpointUrl, err := url.Parse("https://suggestions.dadata.ru/suggestions/api/4_1/rs/")
if err != nil {
	return
}

// Customize json encoding
encoderFactory := func(w io.Writer) transport.Encoder {
	e := json.NewEncoder(w)
	e.SetIndent("", "  ")
	return func(v interface{}) error {
		return e.Encode(v)
	}
}

// Customize json decoding
decoderFactory := func(r io.Reader) transport.Decoder {
	d := json.NewDecoder(r)
	d.DisallowUnknownFields()
	return func(v interface{}) error {
		return d.Decode(v)
	}
}

api := suggest.Api{
	Client: NewClient(endpointUrl, WithEncoderFactory(encoderFactory), WithDecoderFactory(decoderFactory)),
}

params := suggest.RequestParams{
	Query: "ул Свободы",
}

suggestions, err := api.Address(context.Background(), &params)
if err != nil {
	return
}

for _, s := range suggestions {
	fmt.Printf("%s", s.Value)
}
Output:

func WithHttpClient

func WithHttpClient(c *http.Client) Option

WithHttpClient sets custom http.Client.

type ResponseError

type ResponseError struct {
	Status     string // e.g. "200 OK"
	StatusCode int    // e.g. 200
}

ResponseError indicates an HTTP non-200 response code.

func (*ResponseError) Error

func (e *ResponseError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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