Skip to content

Commit

Permalink
Merge pull request #11 from golift/test
Browse files Browse the repository at this point in the history
more boolean magic
  • Loading branch information
davidnewhall authored Jan 3, 2023
2 parents 8bffa72 + 9b59ca6 commit 4ce2e02
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
22 changes: 13 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package deluge
import (
"encoding/json"
"net/http"
"strings"
)

// Deluge WebUI methods.
Expand Down Expand Up @@ -64,9 +65,9 @@ type XferStatus2 struct {
MaxUploadSpeed float64 `json:"max_upload_speed"`
Message string `json:"message"`
MoveOnCompletedPath string `json:"move_on_completed_path"`
MoveOnCompleted bool `json:"move_on_completed"`
MoveOnCompleted Bool `json:"move_on_completed"`
MoveCompletedPath string `json:"move_completed_path"`
MoveCompleted bool `json:"move_completed"`
MoveCompleted Bool `json:"move_completed"`
NextAnnounce float64 `json:"next_announce"`
NumPeers int64 `json:"num_peers"`
NumSeeds int64 `json:"num_seeds"`
Expand Down Expand Up @@ -205,11 +206,11 @@ type XferStatus struct {
NumPieces int64 `json:"num_pieces"`
TrackerStatus string `json:"tracker_status"`
TotalSeeds int64 `json:"total_seeds"`
MoveOnCompleted bool `json:"move_on_completed"`
MoveOnCompleted Bool `json:"move_on_completed"`
NextAnnounce int64 `json:"next_announce"`
StopAtRatio bool `json:"stop_at_ratio"`
FileProgress []float64 `json:"file_progress"`
MoveCompleted bool `json:"move_completed"`
MoveCompleted Bool `json:"move_completed"`
PieceLength int64 `json:"piece_length"`
AllTimeDownload int64 `json:"all_time_download"`
MoveOnCompletedPath string `json:"move_on_completed_path"`
Expand Down Expand Up @@ -260,9 +261,9 @@ type XferStatusCompat struct {
MaxUploadSpeed float64 `json:"max_upload_speed"`
Message string `json:"message"`
MoveOnCompletedPath string `json:"move_on_completed_path"`
MoveOnCompleted bool `json:"move_on_completed"`
MoveOnCompleted Bool `json:"move_on_completed"`
MoveCompletedPath string `json:"move_completed_path"`
MoveCompleted bool `json:"move_completed"`
MoveCompleted Bool `json:"move_completed"`
NextAnnounce float64 `json:"next_announce"`
NumPeers int64 `json:"num_peers"`
NumSeeds int64 `json:"num_seeds"`
Expand Down Expand Up @@ -356,14 +357,17 @@ type XferStatusCompat struct {
}

// Bool provides a container and unmarshalling for fields that may be
// boolean or numbrs in the WebUI API.
// boolean or numbers or strings in the WebUI API.
type Bool bool

// UnmarshalJSON parses fields that may be numbers or booleans.
// https://stackoverflow.com/questions/30856454/how-to-unmarshall-both-0-and-false-as-bool-from-json/56832346#56832346
func (bit *Bool) UnmarshalJSON(b []byte) error {
txt := string(b)
*bit = Bool(txt == "1" || txt == "true")
txt := strings.Trim(string(b), `"`)
*bit = Bool(strings.EqualFold(txt, "1") ||
strings.EqualFold(txt, "true") ||
strings.EqualFold(txt, "yes") ||
strings.EqualFold(txt, "active"))

return nil
}
18 changes: 10 additions & 8 deletions deluge.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,19 @@ func (d Deluge) DelReq(ctx context.Context, method string, params interface{}) (
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, d.url, bytes.NewBuffer(data))
if err == nil {
if d.auth != "" {
// In case Deluge is also behind HTTP auth.
req.Header.Add("Authorization", d.auth)
}
if err != nil {
return req, fmt.Errorf("creating request: %w", err)
}

req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
if d.auth != "" {
// In case Deluge is also behind HTTP auth.
req.Header.Add("Authorization", d.auth)
}

return req, fmt.Errorf("creating request: %w", err)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")

return req, nil
}

// GetXfers gets all the Transfers from Deluge.
Expand Down

0 comments on commit 4ce2e02

Please sign in to comment.