realdebrid-torrent/realdebrid/client.go

175 lines
3.2 KiB
Go

package realdebrid
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
type Client struct {
Token string
}
type createResponse struct {
ID string `json:"id"`
}
func (c Client) AddTorrent(ctx context.Context, torrent []byte) (string, error) {
req, err := c.createRequest(ctx, "/torrents/addTorrent", http.MethodPut, bytes.NewReader(torrent))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/x-bittorrent")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return "", fmt.Errorf("status code: %d", resp.StatusCode)
}
var response createResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return "", err
}
return response.ID, nil
}
func (c Client) AddMagnet(ctx context.Context, torrent string) (string, error) {
req, err := c.createPostRequest(ctx, "/torrents/addMagnet", map[string]string{"magnet": torrent})
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return "", fmt.Errorf("status code: %d", resp.StatusCode)
}
var response createResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return "", err
}
return response.ID, nil
}
func (c Client) DeleteTorrent(ctx context.Context, id string) error {
req, err := c.createRequest(ctx, "/torrents/delete/"+id, http.MethodDelete, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("status code: %d", resp.StatusCode)
}
return nil
}
func (c Client) SelectFiles(ctx context.Context, id string) error {
req, err := c.createPostRequest(ctx, "/torrents/selectFiles/"+id, map[string]string{"files": "all"})
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return err
}
return nil
}
func (c Client) StatusTorrent(ctx context.Context, id string) (*TorrentStatus, error) {
req, err := c.createRequest(ctx, "/torrents/info/"+id, http.MethodGet, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, err
}
var status TorrentStatus
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
return nil, err
}
return &status, nil
}
func (c Client) UnrestrictLink(ctx context.Context, link string) (string, error) {
req, err := c.createPostRequest(ctx, "/unrestrict/link", map[string]string{"link": link})
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("status code: %d", resp.StatusCode)
}
var response struct {
Download string `json:"download"`
}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return "", err
}
return response.Download, nil
}