internal/curdhost/host.go

42 lines · 1183 bytes

12 symbols in this file
 1package curdhost
 2
 3import (
 4	"fmt"
 5	"net/http"
 6	"net/url"
 7	"strings"
 8)
 9
10// PromptOption is a minimal menu item for provider-driven prompts.
11type PromptOption struct {
12	Key   string
13	Label string
14}
15
16// Host hooks are wired from the main application package during init.
17var (
18	HTTPClient             func() *http.Client
19	Log                    func(string)
20	Out                     func(string)
21	PromptSelect            func(options []PromptOption) (PromptOption, error)
22	CurrentSubStyle         func() string
23	PersistSubStylePreference func(style string) error
24	StoragePath             func() string
25	AnimeNameLanguage      func() string
26	SetCookiesForAnimepahe func(u *url.URL, cookies []*http.Cookie)
27)
28
29func HTTPStatusOK(statusCode int) bool {
30	return statusCode >= http.StatusOK && statusCode < http.StatusMultipleChoices
31}
32
33func HTTPStatusError(context string, statusCode int, body []byte) error {
34	snippet := strings.TrimSpace(string(body))
35	if len(snippet) > 300 {
36		snippet = snippet[:300] + "..."
37	}
38	if snippet != "" {
39		return fmt.Errorf("%s failed with status %d: %s", context, statusCode, snippet)
40	}
41	return fmt.Errorf("%s failed with status %d", context, statusCode)
42}