mirror of
https://github.com/miniflux/v2.git
synced 2025-06-27 16:36:00 +00:00
fix(karakeep): correct method name and improve error handling in SaveURL
This commit is contained in:
parent
adfc38d237
commit
ebd6c42c42
2 changed files with 26 additions and 34 deletions
|
@ -437,7 +437,7 @@ func SendEntry(entry *model.Entry, userIntegrations *model.Integration) {
|
||||||
)
|
)
|
||||||
|
|
||||||
client := karakeep.NewClient(userIntegrations.KarakeepAPIKey, userIntegrations.KarakeepURL)
|
client := karakeep.NewClient(userIntegrations.KarakeepAPIKey, userIntegrations.KarakeepURL)
|
||||||
if err := client.SaveUrl(entry.URL); err != nil {
|
if err := client.SaveURL(entry.URL); err != nil {
|
||||||
slog.Error("Unable to send entry to Karakeep",
|
slog.Error("Unable to send entry to Karakeep",
|
||||||
slog.Int64("user_id", userIntegrations.UserID),
|
slog.Int64("user_id", userIntegrations.UserID),
|
||||||
slog.Int64("entry_id", entry.ID),
|
slog.Int64("entry_id", entry.ID),
|
||||||
|
|
|
@ -21,40 +21,33 @@ type errorResponse struct {
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type successResponse struct {
|
type saveURLPayload struct {
|
||||||
CreatedAt string `json:"createdAt"`
|
|
||||||
Content struct {
|
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Url string `json:"url"`
|
URL string `json:"url"`
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client interface {
|
type Client struct {
|
||||||
SaveUrl(url string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type client struct {
|
|
||||||
wrapped *http.Client
|
wrapped *http.Client
|
||||||
apiEndpoint string
|
apiEndpoint string
|
||||||
apiToken string
|
apiToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(apiToken string, apiEndpoint string) Client {
|
func NewClient(apiToken string, apiEndpoint string) *Client {
|
||||||
return &client{wrapped: &http.Client{Timeout: defaultClientTimeout}, apiEndpoint: apiEndpoint, apiToken: apiToken}
|
return &Client{wrapped: &http.Client{Timeout: defaultClientTimeout}, apiEndpoint: apiEndpoint, apiToken: apiToken}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) SaveUrl(url string) error {
|
func (c *Client) SaveURL(entryURL string) error {
|
||||||
var payload = map[string]interface{}{
|
requestBody, err := json.Marshal(&saveURLPayload{
|
||||||
"type": "link",
|
Type: "link",
|
||||||
"url": url,
|
URL: entryURL,
|
||||||
}
|
})
|
||||||
b, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("karakeep: unable to encode request body: %v", err)
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest(http.MethodPost, c.apiEndpoint, bytes.NewReader(b))
|
|
||||||
|
req, err := http.NewRequest(http.MethodPost, c.apiEndpoint, bytes.NewReader(requestBody))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("karakeep: unable to create request: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiToken))
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiToken))
|
||||||
|
@ -63,27 +56,26 @@ func (c *client) SaveUrl(url string) error {
|
||||||
|
|
||||||
resp, err := c.wrapped.Do(req)
|
resp, err := c.wrapped.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("karakeep: unable to send request: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
b, err = io.ReadAll(resp.Body)
|
|
||||||
|
responseBody, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("karakeep: failed to parse response: %s", err)
|
return fmt.Errorf("karakeep: failed to parse response: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode >= 400 {
|
if resp.Header.Get("Content-Type") != "application/json" {
|
||||||
|
return fmt.Errorf("karakeep: unexpected content type response: %s", resp.Header.Get("Content-Type"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusCreated {
|
||||||
var errResponse errorResponse
|
var errResponse errorResponse
|
||||||
if err = json.Unmarshal(b, &errResponse); err != nil {
|
if err := json.Unmarshal(responseBody, &errResponse); err != nil {
|
||||||
return fmt.Errorf("karakeep: failed to save URL: status=%d %s", resp.StatusCode, string(b))
|
return fmt.Errorf("karakeep: unable to parse error response: status=%d body=%s", resp.StatusCode, string(responseBody))
|
||||||
}
|
}
|
||||||
return fmt.Errorf("karakeep: failed to save URL: status=%d errorcode=%s %s", resp.StatusCode, errResponse.Code, errResponse.Error)
|
return fmt.Errorf("karakeep: failed to save URL: status=%d errorcode=%s %s", resp.StatusCode, errResponse.Code, errResponse.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var successReponse successResponse
|
|
||||||
if err = json.Unmarshal(b, &successReponse); err != nil {
|
|
||||||
return fmt.Errorf("karakeep: failed to parse response, however the request appears successful, is the url correct?: status=%d %s", resp.StatusCode, string(b))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue