2019-04-09 17:08:32 +03:00
|
|
|
|
package client
|
|
|
|
|
|
|
|
|
|
import (
|
2024-05-14 23:23:03 +03:00
|
|
|
|
"bytes"
|
2019-04-09 17:08:32 +03:00
|
|
|
|
"context"
|
2023-05-07 14:15:49 +03:00
|
|
|
|
"encoding/json"
|
2019-04-09 17:08:32 +03:00
|
|
|
|
"fmt"
|
2023-05-07 14:15:49 +03:00
|
|
|
|
"io"
|
2019-04-09 17:08:32 +03:00
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
|
|
"github.com/ekomobile/dadata/v2/api/suggest"
|
2023-05-07 14:15:49 +03:00
|
|
|
|
"github.com/ekomobile/dadata/v2/client/transport"
|
2024-05-14 23:23:03 +03:00
|
|
|
|
"github.com/ekomobile/dadata/v2/client/transport/encoder"
|
2019-04-09 17:08:32 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func ExampleNewClient() {
|
|
|
|
|
var err error
|
2019-04-09 17:13:34 +03:00
|
|
|
|
endpointUrl, err := url.Parse("https://suggestions.dadata.ru/suggestions/api/4_1/rs/")
|
2019-04-09 17:08:32 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
api := suggest.Api{
|
2019-04-09 17:13:34 +03:00
|
|
|
|
Client: NewClient(endpointUrl),
|
2019-04-09 17:08:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
params := suggest.RequestParams{
|
|
|
|
|
Query: "ул Свободы",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suggestions, err := api.Address(context.Background(), ¶ms)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, s := range suggestions {
|
|
|
|
|
fmt.Printf("%s", s.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 17:13:34 +03:00
|
|
|
|
func ExampleCredentials() {
|
2019-04-09 17:08:32 +03:00
|
|
|
|
var err error
|
2019-04-09 17:13:34 +03:00
|
|
|
|
endpointUrl, err := url.Parse("https://suggestions.dadata.ru/suggestions/api/4_1/rs/")
|
2019-04-09 17:08:32 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
creds := Credentials{
|
|
|
|
|
ApiKeyValue: "<YOUR_API_KEY>",
|
2022-03-30 23:30:20 +03:00
|
|
|
|
SecretKeyValue: "<YOUR_SECRET_KEY>",
|
2019-04-09 17:08:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
api := suggest.Api{
|
2019-04-09 17:13:34 +03:00
|
|
|
|
Client: NewClient(endpointUrl, WithCredentialProvider(&creds)),
|
2019-04-09 17:08:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
params := suggest.RequestParams{
|
|
|
|
|
Query: "ул Свободы",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suggestions, err := api.Address(context.Background(), ¶ms)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, s := range suggestions {
|
|
|
|
|
fmt.Printf("%s", s.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-07 14:15:49 +03:00
|
|
|
|
|
|
|
|
|
func ExampleWithEncoderFactory() {
|
|
|
|
|
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(), ¶ms)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, s := range suggestions {
|
|
|
|
|
fmt.Printf("%s", s.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-14 23:23:03 +03:00
|
|
|
|
|
|
|
|
|
func ExampleClient_low_level() {
|
|
|
|
|
var err error
|
|
|
|
|
endpointUrl, err := url.Parse("https://suggestions.dadata.ru/suggestions/api/4_1/rs/")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cli := NewClient(endpointUrl,
|
|
|
|
|
WithEncoderFactory(encoder.RawEncoderFactory()),
|
|
|
|
|
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())
|
|
|
|
|
}
|