Documentation
¶
Overview ¶
Пакет hhru — типизированный HTTP-клиент для API HeadHunter (api.hh.ru).
Вызовите New с заполненным Options.HHUserAgent: имя приложения и контактный e-mail, как требует HH. Опциональный [Options.TokenSource] добавляет Bearer ко всем запросам. Для токена приложения используйте ExchangeClientCredentials и AccessToken. Для пользовательского OAuth с refresh_token — NewRefreshingTokenSource или NewRefreshingTokenSourceWithOptions с Clock в тестах.
Сгенерированные подклиенты (по разбиению публичной OpenAPI) лежат в пакетах:
- github.com/Zoomish/go-hhru-api/gen/employer — API работодателя;
- github.com/Zoomish/go-hhru-api/gen/applicant — авторизация и сценарии соискателя;
- github.com/Zoomish/go-hhru-api/gen/public — публичные справочники и подсказки;
- github.com/Zoomish/go-hhru-api/gen/app — эндпоинты в контексте приложения.
На Client они доступны как Employer, Applicant, Public и App.
Надёжность и наблюдаемость через Options: [Options.MaxRetries], пределы паузы [Options.RetryBackoffMin] и [Options.RetryBackoffMax], ограничение частоты [Options.MaxRequestsPerSecond], хуки [Options.RequestHook] и [Options.ResponseHook]. Разбор JSON-ошибок API — ParseAPIError.
Запускаемые примеры — в каталоге examples/ (см. README репозитория). Живые HTTP-тесты — с тегом сборки "integration" в пакете integration/.
Официальная документация OpenAPI: https://api.hh.ru/openapi/redoc
Index ¶
- Constants
- func PagesUntil(ctx context.Context, startPage int, ...) error
- func TokenEndpoint(base string) string
- type APIError
- type Client
- type Clock
- type Options
- type RefreshingSourceOptions
- type RefreshingTokenSource
- type TokenResponse
- type TokenSource
- func AccessToken(token string) TokenSource
- func NewRefreshingTokenSource(httpClient *http.Client, tokenURL, hhUserAgent, clientID, clientSecret string, ...) (TokenSource, error)
- func NewRefreshingTokenSourceWithOptions(httpClient *http.Client, tokenURL, hhUserAgent, clientID, clientSecret string, ...) (TokenSource, error)
Examples ¶
Constants ¶
const DefaultBaseURL = "https://api.hh.ru"
Variables ¶
This section is empty.
Functions ¶
func PagesUntil ¶
func PagesUntil(ctx context.Context, startPage int, fn func(ctx context.Context, page int) (continueNext bool, err error)) error
Example ¶
ctx := context.Background()
sum := 0
_ = hhru.PagesUntil(ctx, 1, func(ctx context.Context, page int) (bool, error) {
sum += page
if page >= 3 {
return false, nil
}
return true, nil
})
fmt.Println(sum)
Output: 6
func TokenEndpoint ¶
Example ¶
u := hhru.TokenEndpoint(hhru.DefaultBaseURL) fmt.Println(u)
Output: https://api.hh.ru/token
Types ¶
type APIError ¶
func ParseAPIError ¶
Example ¶
e := hhru.ParseAPIError(400, []byte(`{"request_id":"rid"}`))
fmt.Println(e.RequestID)
Output: rid
type Client ¶
type Client struct {
Employer *employer.ClientWithResponses
Applicant *applicant.ClientWithResponses
Public *public.ClientWithResponses
App *app.ClientWithResponses
// contains filtered or unexported fields
}
func New ¶
Example ¶
_, err := hhru.New(hhru.Options{
HHUserAgent: "MyBot/1.0 (mailto:you@example.com)",
})
fmt.Println(err == nil)
Output: true
func (*Client) HHUserAgent ¶
type Options ¶
type Options struct {
HTTPClient *http.Client
BaseURL string
HHUserAgent string
DefaultHost string
DefaultLocale string
TokenSource TokenSource
MaxRetries int
RetryBackoffMin time.Duration
RetryBackoffMax time.Duration
MaxRequestsPerSecond float64
RequestHook func(ctx context.Context, req *http.Request) error
ResponseHook func(ctx context.Context, resp *http.Response)
}
type RefreshingSourceOptions ¶
type RefreshingSourceOptions struct {
Clock Clock
}
type RefreshingTokenSource ¶
type TokenResponse ¶
type TokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
}
func ExchangeRefreshToken ¶
type TokenSource ¶
func AccessToken ¶
func AccessToken(token string) TokenSource
Example ¶
ts := hhru.AccessToken("opaque")
tok, _ := ts.Token(context.Background())
fmt.Println(tok)
Output: opaque
func NewRefreshingTokenSource ¶
func NewRefreshingTokenSource(httpClient *http.Client, tokenURL, hhUserAgent, clientID, clientSecret string, initial *TokenResponse) (TokenSource, error)
Example ¶
ts, err := hhru.NewRefreshingTokenSource(nil,
hhru.TokenEndpoint(hhru.DefaultBaseURL),
"MyBot/1.0 (mailto:you@example.com)",
"client-id", "client-secret",
&hhru.TokenResponse{
AccessToken: "initial-access",
RefreshToken: "refresh-token",
ExpiresIn: 3600,
},
)
if err != nil {
fmt.Println("err")
return
}
tok, err := ts.Token(context.Background())
if err != nil || tok == "" {
fmt.Println("bad")
return
}
fmt.Println("ok")
Output: ok
func NewRefreshingTokenSourceWithOptions ¶
func NewRefreshingTokenSourceWithOptions(httpClient *http.Client, tokenURL, hhUserAgent, clientID, clientSecret string, initial *TokenResponse, opts RefreshingSourceOptions) (TokenSource, error)
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
app_token
command
|
|
|
custom_options
command
|
|
|
public_areas
command
|
|
|
public_countries
command
|
|
|
public_industries
command
|
|
|
public_languages
command
|
|
|
public_locales
command
|
|
|
public_position_suggest
command
|
|
|
refreshing_token
command
|
|
|
gen
|
|
|
app
Package app provides primitives to interact with the openapi HTTP API.
|
Package app provides primitives to interact with the openapi HTTP API. |
|
applicant
Package applicant provides primitives to interact with the openapi HTTP API.
|
Package applicant provides primitives to interact with the openapi HTTP API. |
|
employer
Package employer provides primitives to interact with the openapi HTTP API.
|
Package employer provides primitives to interact with the openapi HTTP API. |
|
public
Package public provides primitives to interact with the openapi HTTP API.
|
Package public provides primitives to interact with the openapi HTTP API. |
|
Package integration holds optional live-API tests; run with -tags=integration.
|
Package integration holds optional live-API tests; run with -tags=integration. |
|
scripts
|
|
|
generate
command
|
|
|
split-openapi
command
|