package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
type SnipURLAPI struct {
apiKey string
baseURL string
client *http.Client
}
type CreateLinkRequest struct {
Amount int `json:"amount"`
Description string `json:"description"`
}
type APIResponse struct {
Success bool `json:"success"`
Data interface{} `json:"data,omitempty"`
Error string `json:"error,omitempty"`
Message string `json:"message,omitempty"`
}
func NewSnipURLAPI(apiKey string) *SnipURLAPI {
return &SnipURLAPI{
apiKey: apiKey,
baseURL: "https://snipurl.org/api/v1",
client: &http.Client{},
}
}
func (s *SnipURLAPI) makeRequest(method, endpoint string, body interface{}) (*APIResponse, error) {
url := s.baseURL + endpoint
var reqBody io.Reader
if body != nil {
jsonData, err := json.Marshal(body)
if err != nil {
return nil, err
}
reqBody = bytes.NewBuffer(jsonData)
}
req, err := http.NewRequest(method, url, reqBody)
if err != nil {
return nil, err
}
req.Header.Set("X-API-Key", s.apiKey)
req.Header.Set("Content-Type", "application/json")
resp, err := s.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var apiResp APIResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
return nil, err
}
return &apiResp, nil
}
func (s *SnipURLAPI) CreatePaymentLink(amount int, description string) (*APIResponse, error) {
return s.makeRequest("POST", "/links", CreateLinkRequest{
Amount: amount,
Description: description,
})
}
func (s *SnipURLAPI) GetLinks() (*APIResponse, error) {
return s.makeRequest("GET", "/links", nil)
}
func (s *SnipURLAPI) GetStatistics() (*APIResponse, error) {
return s.makeRequest("GET", "/links/stats", nil)
}
func main() {
api := NewSnipURLAPI("your_api_key_here")
// Create a payment link
result, err := api.CreatePaymentLink(2100, "Bitcoin book purchase")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
if result.Success {
fmt.Printf("Payment link created successfully\n")
fmt.Printf("Response: %+v\n", result.Data)
} else {
fmt.Printf("API Error: %s\n", result.Error)
}
// Get statistics
stats, err := api.GetStatistics()
if err != nil {
fmt.Printf("Error getting stats: %v\n", err)
return
}
fmt.Printf("Statistics: %+v\n", stats.Data)
}