cli example

This commit is contained in:
i.smyshlyaev
2025-10-16 14:54:24 +03:00
parent 9c813d4754
commit f9b31f5dc4
37 changed files with 412 additions and 1391 deletions

90
internal/client/api.go Normal file
View File

@@ -0,0 +1,90 @@
package client
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"code.linberg.su/linberg/awesome-back/pkg/errors"
)
type APIClient struct {
baseURL string
httpClient *http.Client
}
func NewAPIClient(baseURL string) *APIClient {
return &APIClient{
baseURL: baseURL,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
}
}
type APIResponse struct {
Success bool `json:"success"`
Data json.RawMessage `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}
func (c *APIClient) doRequest(method, path string) (*APIResponse, error) {
url := fmt.Sprintf("%s%s", c.baseURL, path)
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, errors.NewNetworkError("failed to create request").Wrap(err)
}
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, errors.NewNetworkError("request failed").Wrap(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.NewNetworkError("failed to read response").Wrap(err)
}
var apiResponse APIResponse
if err := json.Unmarshal(body, &apiResponse); err != nil {
return nil, errors.NewInternalError("failed to parse response").Wrap(err)
}
// Обработка HTTP ошибок
if resp.StatusCode >= 400 {
switch resp.StatusCode {
case http.StatusNotFound:
return nil, errors.NewNotFoundError(apiResponse.Error)
case http.StatusBadRequest:
return nil, errors.NewInvalidError(apiResponse.Error)
default:
return nil, errors.NewInternalError(fmt.Sprintf("server error: %s", apiResponse.Error))
}
}
return &apiResponse, nil
}
// Get выполняет GET запрос и парсит ответ в target
func (c *APIClient) Get(path string, target interface{}) error {
response, err := c.doRequest("GET", path)
if err != nil {
return err
}
if !response.Success {
return errors.NewInternalError("API request failed")
}
if err := json.Unmarshal(response.Data, target); err != nil {
return errors.NewInternalError("failed to parse response data").Wrap(err)
}
return nil
}

View File

@@ -0,0 +1,131 @@
package handler
import (
"errors"
"fmt"
"strconv"
"awesome_cli/internal/service"
"awesome_cli/pkg/ui"
libErr "code.linberg.su/linberg/awesome-back/pkg/errors"
)
type CoffeeHandler struct {
coffeeService *service.CoffeeService
}
func NewCoffeeHandler(coffeeService *service.CoffeeService) *CoffeeHandler {
return &CoffeeHandler{
coffeeService: coffeeService,
}
}
func (h *CoffeeHandler) HandleList() error {
coffees, err := h.coffeeService.GetAllCoffees()
if err != nil {
return h.handleError(err, "Failed to list coffees")
}
h.printCoffeesTable(coffees)
return nil
}
func (h *CoffeeHandler) HandleGet(coffeeID string) error {
id, err := strconv.Atoi(coffeeID)
if err != nil {
return libErr.NewInvalidError("coffee ID must be a number")
}
coffee, err := h.coffeeService.GetCoffeeByID(id)
if err != nil {
return h.handleError(err, fmt.Sprintf("Failed to get coffee with ID %d", id))
}
h.printCoffeeDetail(coffee)
return nil
}
func (h *CoffeeHandler) HandleGetWithRetry(coffeeID string, maxRetries int) error {
id, err := strconv.Atoi(coffeeID)
if err != nil {
return libErr.NewInvalidError("coffee ID must be a number")
}
fmt.Printf("Fetching coffee with ID %d (max retries: %d)...\n", id, maxRetries)
coffee, err := h.coffeeService.GetCoffeeByIDWithRetry(id, maxRetries)
if err != nil {
return h.handleError(err, fmt.Sprintf("Failed to get coffee with ID %d after retries", id))
}
h.printCoffeeDetail(coffee)
return nil
}
func (h *CoffeeHandler) printCoffeesTable(coffees []service.Coffee) {
t := ui.NewTableBuilder()
t.AddTableHeader("ID", "Name", "Price", "Size", "Description")
for _, coffee := range coffees {
t.AddRow(
strconv.FormatInt(int64(coffee.ID), 10),
coffee.Name,
strconv.FormatInt(int64(coffee.Price), 10),
coffee.Size,
coffee.Description,
)
}
fmt.Println(t.CreateTable())
}
func (h *CoffeeHandler) printCoffeeDetail(coffee *service.Coffee) {
fmt.Printf("ID: %s\n", ui.SetColor(ui.Green, strconv.FormatInt(int64(coffee.ID), 10)))
fmt.Printf("Name: %s\n", ui.SetColor(ui.Green, coffee.Name))
fmt.Printf("Price: %s\n", ui.SetColor(ui.Green, fmt.Sprintf("%.2f ₽", coffee.Price)))
fmt.Printf("Size: %s\n", ui.SetColor(ui.Green, coffee.Size))
fmt.Printf("Description: %s\n", ui.SetColor(ui.Green, coffee.Description))
fmt.Println()
}
// handleError демонстрирует продвинутую обработку ошибок
func (h *CoffeeHandler) handleError(err error, message string) error {
red := func(s string) string {
return ui.SetColor(ui.Red, s)
}
fmt.Printf("%s: %s\n", red("Error"), message)
// Демонстрация использования errors.Is и errors.As
switch {
case libErr.IsNotFound(err):
fmt.Printf("%s: Resource not found\n", red("Type"))
fmt.Printf("%s: %v\n", red("Details"), err)
case libErr.IsInvalid(err):
fmt.Printf("%s: Invalid request\n", red("Type"))
fmt.Printf("%s: %v\n", red("Details"), err)
case libErr.IsNetworkError(err):
fmt.Printf("%s: Network error\n", red("Type"))
fmt.Printf("%s: Please check your connection and try again\n", red("Details"))
default:
// Используем errors.As для получения дополнительной информации
var appErr *libErr.AppError
if errors.As(err, &appErr) {
fmt.Printf("%s: %s\n", red("Type"), string(appErr.Type))
fmt.Printf("%s: %s\n", red("Message"), appErr.Message)
// Демонстрация unwrap цепочки ошибок
if appErr.Err != nil {
fmt.Printf("%s: %v\n", red("Underlying error"), appErr.Err)
}
} else {
fmt.Printf("%s: %v\n", red("Unknown error"), err)
}
}
fmt.Println() // Пустая строка для читабельности
return err
}

View File

@@ -0,0 +1,96 @@
package service
import (
"errors"
"fmt"
"awesome_cli/internal/client"
libErr "code.linberg.su/linberg/awesome-back/pkg/errors"
)
type Coffee struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Price float64 `json:"price"`
Size string `json:"size"`
}
type CoffeeService struct {
apiClient *client.APIClient
}
func NewCoffeeService(apiClient *client.APIClient) *CoffeeService {
return &CoffeeService{
apiClient: apiClient,
}
}
func (s *CoffeeService) GetAllCoffees() ([]Coffee, error) {
var coffees []Coffee
err := s.apiClient.Get("/api/v1/coffees", &coffees)
if err != nil {
return nil, libErr.Wrap(err, "failed to fetch coffees")
}
return coffees, nil
}
func (s *CoffeeService) GetCoffeeByID(id int) (*Coffee, error) {
if id <= 0 {
return nil, libErr.NewInvalidError("coffee ID must be positive")
}
var coffee Coffee
path := fmt.Sprintf("/api/v1/coffees/%d", id)
err := s.apiClient.Get(path, &coffee)
if err != nil {
// Демонстрация использования errors.Is и errors.As
if libErr.IsNotFound(err) {
return nil, libErr.NewNotFoundError(fmt.Sprintf("coffee with ID %d not found", id))
}
return nil, libErr.Wrapf(err, "failed to fetch coffee with ID %d", id)
}
return &coffee, nil
}
// GetCoffeeByIDWithRetry демонстрирует сложную обработку ошибок с retry логикой
func (s *CoffeeService) GetCoffeeByIDWithRetry(id int, maxRetries int) (*Coffee, error) {
var lastErr error
for attempt := 0; attempt <= maxRetries; attempt++ {
coffee, err := s.GetCoffeeByID(id)
if err == nil {
return coffee, nil
}
lastErr = err
// Проверяем тип ошибки для принятия решения о retry
if libErr.IsNetworkError(err) {
// Network errors - можно retry
continue
}
if libErr.IsNotFound(err) || libErr.IsInvalid(err) {
// Client errors - не retry
break
}
// Для других ошибок можно добавить дополнительную логику
var appErr *libErr.AppError
if errors.As(err, &appErr) {
switch appErr.Type {
case libErr.ErrorTypeInternal:
// Server errors - можно retry
continue
default:
// Другие ошибки - не retry
break
}
}
}
return nil, libErr.Wrapf(lastErr, "failed after %d attempts", maxRetries+1)
}