Skip to content

Navigation Menu

Sign in
Sign up

Latest commit

History

74 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Client for DaData.ru

Forked from https://github.com/webdeskltd/dadata.

Build Status GitHub release Go Report Card GoDoc

DaData API v2

Implemented Clean and Suggest methods.

Installation

go get github.com/ekomobile/dadata/v2

Usage

import (
	"context"
	"fmt"
	"github.com/ekomobile/dadata/v2"
	"github.com/ekomobile/dadata/v2/api/suggest"
)
func DaDataExample() {
	api := dadata.NewSuggestApi()
	params := suggest.RequestParams{
		Query: "ул Свободы",
	}
	suggestions, err := api.Address(context.Background(), &params)
	if err != nil {
		return
	}
	for _, s := range suggestions {
		fmt.Printf("%s", s.Value)
	}
}

Configuration

Credentials

DADATA_API_KEY and DADATA_SECRET_KEY environment variables are used by default to authenticate client.

Custom credential provider may be used by implementing client.CredentialProvider interface.

Also, there is a "simple" credential provider client.Credentials you may utilize.

creds := client.Credentials{
 ApiKeyValue: "<YOUR_API_KEY>",
 SecretKeyValue: "<YOUR_SECRET_KEY>",
}
api := NewSuggestApi(client.WithCredentialProvider(&creds))

HTTP client

HTTP client may be overridden with custom one:

httpClient := &http.Client{}
api := NewSuggestApi(WithHttpClient(httpClient))

Low level client usage

Pass and consume raw bytes/strings:

 endpointUrl, err := url.Parse("https://suggestions.dadata.ru/suggestions/api/4_1/rs/")
 if err != nil {
 return
 }
 cli := client.NewClient(endpointUrl,
 client.WithEncoderFactory(encoder.RawEncoderFactory()),
 client.WithDecoderFactory(encoder.RawDecoderFactory()), 
 )
 request := bytes.NewBufferString("{ \"query\": \"москва хабар\" }")
 response := &bytes.Buffer{}
 err = cli.Post(context.Background(), "suggest/address", request, response)
 if err != nil {
 return
 }
 fmt.Print(response.String()) // Output: `{"suggestions":[{"value":"г Москва, ул Хабаровская",...`

Licence

MIT see LICENSE

About

Golang client for DaData.ru

Topics

Resources

Stars

29 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages

AltStyle によって変換されたページ (->オリジナル) /