dadata/README.md

105 lines
2.5 KiB
Markdown
Raw Permalink Normal View History

2019-04-08 14:49:07 +03:00
# Client for DaData.ru
Forked from https://github.com/webdeskltd/dadata.
[![GitHub release](https://img.shields.io/github/release/ekomobile/dadata.svg)](https://github.com/ekomobile/dadata/releases)
2022-03-30 23:30:20 +03:00
[![Go Report Card](https://goreportcard.com/badge/github.com/ekomobile/dadata)](https://goreportcard.com/report/github.com/ekomobile/dadata/v2)
[![GoDoc](https://godoc.org/github.com/ekomobile/dadata/v2?status.svg)](https://godoc.org/github.com/ekomobile/dadata/v2)
2019-04-08 14:49:07 +03:00
DaData API v2
Implemented [Clean](https://dadata.ru/api/clean/) and [Suggest](https://dadata.ru/api/suggest/) methods.
## Installation
2021-09-07 12:17:59 +03:00
`go get github.com/ekomobile/dadata/v2`
2019-04-08 14:49:07 +03:00
## Usage
```go
import (
2019-04-09 17:08:32 +03:00
"context"
2019-04-08 14:49:07 +03:00
"fmt"
2019-04-09 17:08:32 +03:00
"github.com/ekomobile/dadata/v2"
"github.com/ekomobile/dadata/v2/api/suggest"
2019-04-08 14:49:07 +03:00
)
2019-04-09 17:08:32 +03:00
func DaDataExample() {
api := dadata.NewSuggestApi()
2019-04-08 14:49:07 +03:00
2019-04-09 17:08:32 +03:00
params := suggest.RequestParams{
Query: "ул Свободы",
2019-04-08 14:49:07 +03:00
}
2019-04-09 17:08:32 +03:00
suggestions, err := api.Address(context.Background(), &params)
if err != nil {
return
2019-04-08 14:49:07 +03:00
}
2019-04-09 17:08:32 +03:00
for _, s := range suggestions {
fmt.Printf("%s", s.Value)
}
2019-04-08 14:49:07 +03:00
}
```
2022-03-31 00:16:33 +03:00
## Configuration
### Credentials
2022-03-30 23:30:20 +03:00
`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.
```go
creds := client.Credentials{
ApiKeyValue: "<YOUR_API_KEY>",
SecretKeyValue: "<YOUR_SECRET_KEY>",
}
api := NewSuggestApi(client.WithCredentialProvider(&creds))
```
2022-03-31 00:16:33 +03:00
### HTTP client
HTTP client may be overridden with custom one:
```go
httpClient := &http.Client{}
api := NewSuggestApi(WithHttpClient(httpClient))
```
2024-05-14 23:23:03 +03:00
### Low level client usage
Pass and consume raw bytes/strings:
```go
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":"г Москва, ул Хабаровская",...`
```
2022-03-31 00:16:33 +03:00
2019-04-08 14:49:07 +03:00
## Licence
MIT see [LICENSE](LICENSE)