68 lines
2.0 KiB
Go
68 lines
2.0 KiB
Go
package emby
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
// Library is the output shape for libraries (matches Python output keys).
|
|
type Library struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
ID string `json:"id"`
|
|
}
|
|
|
|
// Authenticate logs in with username/password via POST /Users/AuthenticateByName
|
|
// and installs the resulting access token on the client.
|
|
func (c *Client) Authenticate(ctx context.Context, username, password string) (*AuthResult, error) {
|
|
var res AuthResult
|
|
err := c.Post(ctx, "/Users/AuthenticateByName", nil,
|
|
authRequest{Username: username, Pw: password}, &res)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if res.AccessToken == "" || res.User == nil {
|
|
return nil, fmt.Errorf("authentication failed: empty token or user in response")
|
|
}
|
|
c.RememberPassword(username, password)
|
|
c.setToken(res.AccessToken, res.User.ID)
|
|
return &res, nil
|
|
}
|
|
|
|
// Logout revokes the access token via POST /Sessions/Logout.
|
|
func (c *Client) Logout(ctx context.Context) error {
|
|
return c.Post(ctx, "/Sessions/Logout", nil, nil, nil)
|
|
}
|
|
|
|
// GetLibraryList returns the server's collection-folder libraries.
|
|
func (c *Client) GetLibraryList(ctx context.Context) ([]Library, error) {
|
|
var res QueryResult[LibraryFolder]
|
|
if err := c.Get(ctx, "/Library/MediaFolders", nil, &res); err != nil {
|
|
return nil, err
|
|
}
|
|
libs := make([]Library, 0, len(res.Items))
|
|
for _, it := range res.Items {
|
|
if it.Type == "CollectionFolder" {
|
|
libs = append(libs, Library{Name: it.Name, Type: it.CollectionType, ID: it.ID})
|
|
}
|
|
}
|
|
return libs, nil
|
|
}
|
|
|
|
// GetGenreList returns genre names, optionally restricted to a library.
|
|
func (c *Client) GetGenreList(ctx context.Context, libraryID string) ([]string, error) {
|
|
q := qp("Recursive", "true")
|
|
if libraryID != "" {
|
|
q.Set("ParentId", libraryID)
|
|
}
|
|
var res QueryResult[GenreItem]
|
|
if err := c.Get(ctx, "/Genres", q, &res); err != nil {
|
|
return nil, err
|
|
}
|
|
genres := make([]string, 0, len(res.Items))
|
|
for _, g := range res.Items {
|
|
genres = append(genres, g.Name)
|
|
}
|
|
return genres, nil
|
|
}
|