mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-04-22 01:10:36 +00:00
66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package atproto
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
// AppviewMetadata contains branding and configuration from the appview.
|
|
type AppviewMetadata struct {
|
|
ClientName string `json:"clientName"`
|
|
ClientShortName string `json:"clientShortName"`
|
|
BaseURL string `json:"baseUrl"`
|
|
FaviconURL string `json:"faviconUrl"`
|
|
RegistryDomains []string `json:"registryDomains,omitempty"`
|
|
}
|
|
|
|
// FetchAppviewMetadata fetches metadata from the appview's XRPC endpoint.
|
|
func FetchAppviewMetadata(ctx context.Context, appviewURL string) (*AppviewMetadata, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
|
|
reqURL := appviewURL + AppviewGetMetadata
|
|
req, err := http.NewRequestWithContext(ctx, "GET", reqURL, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to fetch metadata: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("metadata endpoint returned status %d", resp.StatusCode)
|
|
}
|
|
|
|
var meta AppviewMetadata
|
|
if err := json.NewDecoder(resp.Body).Decode(&meta); err != nil {
|
|
return nil, fmt.Errorf("failed to decode metadata: %w", err)
|
|
}
|
|
|
|
return &meta, nil
|
|
}
|
|
|
|
// DefaultAppviewMetadata returns fallback metadata derived from the appview URL.
|
|
func DefaultAppviewMetadata(appviewURL string) AppviewMetadata {
|
|
hostname := "ATCR"
|
|
if u, err := url.Parse(appviewURL); err == nil && u.Hostname() != "" {
|
|
hostname = u.Hostname()
|
|
}
|
|
|
|
faviconURL := appviewURL + "/favicon-96x96.png"
|
|
|
|
return AppviewMetadata{
|
|
ClientName: hostname,
|
|
ClientShortName: hostname,
|
|
BaseURL: appviewURL,
|
|
FaviconURL: faviconURL,
|
|
}
|
|
}
|