- Created `client.go` for the main client structure and methods to interact with *arr services. - Added `types.go` to define data structures for system status, movies, and series. - Implemented `radarr.go` for Radarr-specific client methods including health checks and movie retrieval. - Introduced `interfaces.go` to define service interfaces for common operations across *arr services. - Established a basic `main.go` for application entry point. - Included a tutorial markdown file to guide users through building the client and understanding Go concepts. - Initialized `go.mod` for module management. - Organized code into appropriate packages for better structure and maintainability.
125 lines
3.3 KiB
Go
125 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type RadarrClient struct {
|
|
*Client
|
|
}
|
|
|
|
func NewRadarrClient(baseURL, apiKey string) *RadarrClient {
|
|
return &RadarrClient{
|
|
Client: NewClient(baseURL, apiKey),
|
|
}
|
|
}
|
|
|
|
func (r *RadarrClient) GetHealth() ([]HealthCheck, error) {
|
|
|
|
// Create the URL for the health endpoint.
|
|
url := fmt.Sprintf("%s/api/v3/health", r.BaseURL)
|
|
|
|
// Create a new GET request to the health endpoint.
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
|
|
// Set the API key and content type headers.
|
|
req.Header.Set("X-Api-Key", r.APIKey)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
// Send the request and get the response.
|
|
resp, err := r.HTTPClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to send request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Check if the response is successful.
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("failed to get health: %s", resp.Status)
|
|
}
|
|
|
|
// Decode the response body into a slice of HealthCheck structs.
|
|
var health []HealthCheck
|
|
if err := json.NewDecoder(resp.Body).Decode(&health); err != nil {
|
|
return nil, fmt.Errorf("failed to decode response body: %w", err)
|
|
}
|
|
|
|
return health, nil
|
|
}
|
|
|
|
func (r *RadarrClient) GetMovies() ([]Movie, error) {
|
|
// Create the URL for the movies endpoint.
|
|
url := fmt.Sprintf("%s/api/v3/movie", r.BaseURL)
|
|
|
|
// Create a new GET request to the movies endpoint.
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
|
|
// Set the API key and content type headers.
|
|
req.Header.Set("X-Api-Key", r.APIKey)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
// Send the request and get the response.
|
|
resp, err := r.HTTPClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to send request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Check if the response is successful.
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("failed to get movies: %s", resp.Status)
|
|
}
|
|
|
|
// Decode the response body into a slice of Movie structs.
|
|
var movies []Movie
|
|
if err := json.NewDecoder(resp.Body).Decode(&movies); err != nil {
|
|
return nil, fmt.Errorf("failed to decode response body: %w", err)
|
|
}
|
|
|
|
return movies, nil
|
|
}
|
|
|
|
// GetMovie retrieves a specific movie by ID.
|
|
func (r *RadarrClient) GetMovie(id int) (*Movie, error) {
|
|
// Create the URL for the movie endpoint.
|
|
url := fmt.Sprintf("%s/api/v3/movie/%d", r.BaseURL, id)
|
|
|
|
// Create a new GET request to the movie endpoint.
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
|
|
// Set the API key and content type headers.
|
|
req.Header.Set("X-Api-Key", r.APIKey)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
// Send the request and get the response.
|
|
resp, err := r.HTTPClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to send request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Check if the response is successful.
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("failed to get movie: %s", resp.Status)
|
|
}
|
|
|
|
// Decode the response body into a Movie struct.
|
|
var movie Movie
|
|
if err := json.NewDecoder(resp.Body).Decode(&movie); err != nil {
|
|
return nil, fmt.Errorf("failed to decode response body: %w", err)
|
|
}
|
|
|
|
return &movie, nil
|
|
}
|