Added parts
and gender
params to the name
suggest.
This commit is contained in:
parent
572fa9e535
commit
eb367fac33
|
@ -52,11 +52,6 @@ type (
|
|||
Suggestions []*AddressSuggestion `json:"suggestions"`
|
||||
}
|
||||
|
||||
// NameResponse result slice for name suggestions
|
||||
NameResponse struct {
|
||||
Suggestions []*NameSuggestion `json:"suggestions"`
|
||||
}
|
||||
|
||||
// BankResponse result slice for bank suggestions
|
||||
BankResponse struct {
|
||||
Suggestions []*BankSuggestion `json:"suggestions"`
|
||||
|
@ -101,13 +96,6 @@ type (
|
|||
Data *model.Address `json:"data"`
|
||||
}
|
||||
|
||||
// NameSuggestion api response for name
|
||||
NameSuggestion struct {
|
||||
Value string `json:"value"`
|
||||
UnrestrictedValue string `json:"unrestricted_value"`
|
||||
Data *model.Name `json:"data"`
|
||||
}
|
||||
|
||||
// PartySuggestion api response for party
|
||||
PartySuggestion struct {
|
||||
Value string `json:"value"`
|
||||
|
|
88
api/suggest/name.go
Normal file
88
api/suggest/name.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
package suggest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ekomobile/dadata/v2/api/model"
|
||||
)
|
||||
|
||||
// Name gender values
|
||||
// https://confluence.hflabs.ru/pages/viewpage.action?pageId=204669115
|
||||
const (
|
||||
NameGenderUnknown = "UNKNOWN" // не удалось однозначно определить
|
||||
NameGenderMale = "MALE"
|
||||
NameGenderFemale = "FEMALE"
|
||||
)
|
||||
|
||||
// Name parts
|
||||
// https://dadata.ru/api/suggest/name/
|
||||
// https://confluence.hflabs.ru/pages/viewpage.action?pageId=204669115
|
||||
const (
|
||||
NamePartSurname = "SURNAME"
|
||||
NamePartName = "NAME"
|
||||
NamePartPatronymic = "PATRONYMIC"
|
||||
)
|
||||
|
||||
type (
|
||||
NameParams struct {
|
||||
Query string `json:"query"`
|
||||
Count int `json:"count"`
|
||||
Parts []string `json:"parts"`
|
||||
Gender string `json:"gender"`
|
||||
}
|
||||
NameOption func(params *NameParams)
|
||||
|
||||
// NameResponse result slice for name suggestions
|
||||
NameResponse struct {
|
||||
Suggestions []*NameSuggestion `json:"suggestions"`
|
||||
}
|
||||
|
||||
// NameSuggestion api response for name
|
||||
NameSuggestion struct {
|
||||
Value string `json:"value"`
|
||||
UnrestrictedValue string `json:"unrestricted_value"`
|
||||
Data *model.Name `json:"data"`
|
||||
}
|
||||
)
|
||||
|
||||
// WithNameParts adds `parts` parameter to suggest/fio request
|
||||
// https://dadata.ru/api/suggest/name/
|
||||
// https://confluence.hflabs.ru/pages/viewpage.action?pageId=204669115
|
||||
func WithNameParts(parts ...string) NameOption {
|
||||
return func(params *NameParams) {
|
||||
params.Parts = parts
|
||||
}
|
||||
}
|
||||
|
||||
// WithNameGender adds `gender` parameter to suggest/fio request
|
||||
// https://dadata.ru/api/suggest/name/
|
||||
// https://confluence.hflabs.ru/pages/viewpage.action?pageId=204669115
|
||||
func WithNameGender(gender string) NameOption {
|
||||
return func(params *NameParams) {
|
||||
params.Gender = gender
|
||||
}
|
||||
}
|
||||
|
||||
func (p *NameParams) applyOption(opts ...NameOption) {
|
||||
for _, o := range opts {
|
||||
o(p)
|
||||
}
|
||||
}
|
||||
|
||||
// Name try to return suggest names by params
|
||||
func (a *Api) Name(ctx context.Context, requestParams *RequestParams, opts ...NameOption) (ret []*NameSuggestion, err error) {
|
||||
var result = &NameResponse{}
|
||||
|
||||
params := NameParams{
|
||||
Query: requestParams.Query,
|
||||
Count: requestParams.Count,
|
||||
}
|
||||
params.applyOption(opts...)
|
||||
|
||||
err = a.Client.Post(ctx, "suggest/fio", ¶ms, result)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ret = result.Suggestions
|
||||
return
|
||||
}
|
|
@ -31,17 +31,6 @@ func (a *Api) Address(ctx context.Context, params *RequestParams) (ret []*Addres
|
|||
return
|
||||
}
|
||||
|
||||
// Name try to return suggest names by params
|
||||
func (a *Api) Name(ctx context.Context, params *RequestParams) (ret []*NameSuggestion, err error) {
|
||||
var result = &NameResponse{}
|
||||
err = a.Client.Post(ctx, "suggest/fio", params, result)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ret = result.Suggestions
|
||||
return
|
||||
}
|
||||
|
||||
// Bank try to return suggest banks by params
|
||||
func (a *Api) Bank(ctx context.Context, params *RequestParams) (ret []*BankSuggestion, err error) {
|
||||
var result = &BankResponse{}
|
||||
|
|
|
@ -72,6 +72,26 @@ func (s *ApiSuggestIntegrationTest) TestName() {
|
|||
s.NotEmpty(res)
|
||||
}
|
||||
|
||||
func (s *ApiSuggestIntegrationTest) TestNameWithParts() {
|
||||
api := NewSuggestApi()
|
||||
params := suggest.RequestParams{
|
||||
Query: "але",
|
||||
}
|
||||
res, err := api.Name(context.Background(), ¶ms, suggest.WithNameParts(suggest.NamePartSurname))
|
||||
s.NoError(err)
|
||||
s.NotEmpty(res)
|
||||
}
|
||||
|
||||
func (s *ApiSuggestIntegrationTest) TestNameWithGender() {
|
||||
api := NewSuggestApi()
|
||||
params := suggest.RequestParams{
|
||||
Query: "але",
|
||||
}
|
||||
res, err := api.Name(context.Background(), ¶ms, suggest.WithNameGender(suggest.NameGenderFemale))
|
||||
s.NoError(err)
|
||||
s.NotEmpty(res)
|
||||
}
|
||||
|
||||
func (s *ApiSuggestIntegrationTest) TestEmail() {
|
||||
api := NewSuggestApi()
|
||||
params := suggest.RequestParams{
|
||||
|
|
Loading…
Reference in New Issue
Block a user