PocketBase view collection benutzen. Dokumentation aktualisieren.
This commit is contained in:
@@ -1,50 +1,37 @@
|
||||
package pocketbase
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Client handles interactions with the PocketBase API.
|
||||
type Client struct {
|
||||
baseURL string
|
||||
user string
|
||||
password string
|
||||
category string
|
||||
endpoint *url.URL
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
// New creates a new PocketBase client.
|
||||
func New(baseURL, user, password, category string) *Client {
|
||||
// Normalise: ensure baseURL does not already end with /api.
|
||||
baseURL = strings.TrimSuffix(baseURL, "/api")
|
||||
baseURL = strings.TrimSuffix(baseURL, "/")
|
||||
func New(endpoint string) *Client {
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil || u == nil {
|
||||
return &Client{
|
||||
endpoint: &url.URL{},
|
||||
http: &http.Client{Timeout: 30 * time.Second},
|
||||
}
|
||||
}
|
||||
return &Client{
|
||||
baseURL: baseURL,
|
||||
user: user,
|
||||
password: password,
|
||||
category: category,
|
||||
endpoint: u,
|
||||
http: &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type authRequest struct {
|
||||
Identity string `json:"identity"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type authResponse struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
type record struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
@@ -53,64 +40,11 @@ type recordsResponse struct {
|
||||
Items []record `json:"items"`
|
||||
}
|
||||
|
||||
// GetBookmarkURLs authenticates and retrieves URLs from the bookmarks collection.
|
||||
// GetBookmarkURLs retrieves URLs from the collection.
|
||||
func (c *Client) GetBookmarkURLs(ctx context.Context) ([]string, error) {
|
||||
token, err := c.authenticate(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("pocketbase auth: %w", err)
|
||||
}
|
||||
|
||||
urls, err := c.fetchURLs(ctx, token)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("pocketbase fetch URLs: %w", err)
|
||||
}
|
||||
|
||||
return urls, nil
|
||||
}
|
||||
|
||||
func (c *Client) authenticate(ctx context.Context) (string, error) {
|
||||
reqBody, err := json.Marshal(authRequest{
|
||||
Identity: c.user,
|
||||
Password: c.password,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
authURL := c.baseURL + "/api/collections/users/auth-with-password"
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", authURL, bytes.NewBuffer(reqBody))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("unexpected status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var authResp authResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&authResp); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return authResp.Token, nil
|
||||
}
|
||||
|
||||
func (c *Client) fetchURLs(ctx context.Context, token string) ([]string, error) {
|
||||
// Filter: category=<value>, perPage=200, fields=url
|
||||
filter := fmt.Sprintf(`category="%s"`, c.category)
|
||||
u, err := url.Parse(c.baseURL + "/api/collections/bookmarks/records")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid base URL: %w", err)
|
||||
}
|
||||
// Clone the endpoint URL to avoid mutation on repeated calls.
|
||||
u := *c.endpoint
|
||||
q := u.Query()
|
||||
q.Set("filter", filter)
|
||||
q.Set("perPage", "200")
|
||||
q.Set("fields", "url")
|
||||
u.RawQuery = q.Encode()
|
||||
@@ -119,7 +53,6 @@ func (c *Client) fetchURLs(ctx context.Context, token string) ([]string, error)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating request: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", token)
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user