anilist/user.go

61 lines
1.2 KiB
Go
Raw Normal View History

2022-02-03 17:23:29 +01:00
package anilist
2022-02-06 10:39:04 +01:00
import "context"
2022-02-03 17:23:29 +01:00
func (api *Api) GetUserByName(name string) (*User, error) {
query := `query ($name: String) {
User (name: $name) ` + subSelectionUser + `
}`
vars := map[string]interface{}{
"name": name,
}
resp := responseObj[struct {
User *User
}]{}
err := request(api, query, vars, &resp)
if err != nil {
return nil, err
}
return resp.Data.User, nil
}
func (api *Api) GetUserByID(id int) (*User, error) {
query := `query ($id: Int) {
User (id: $id) ` + subSelectionUser + `
}`
vars := map[string]interface{}{
"id": id,
}
resp := responseObj[struct {
User *User
}]{}
err := request(api, query, vars, &resp)
if err != nil {
return nil, err
}
return resp.Data.User, nil
}
func (api *Api) SearchUsers(ctx context.Context, search string, onError func(error)) *Cursor[User] {
2022-02-03 17:23:29 +01:00
vars := map[string]interface{}{"search": search}
resp := responseObj[*page[User]]{}
2022-02-06 10:39:04 +01:00
return requestPaged(api, ctx, searchUsersQuery, vars, &resp, onError)
2022-02-03 17:23:29 +01:00
}
const (
searchUsersQuery = `query ($search: String, $page: Int) {
Page (page: $page) {
pageInfo ` + subSelectionPageInfo + `
users (search: $search) ` + subSelectionUser + `
}
}`
)