|
| 1 | +package helpers |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "github.com/stacktodate/stacktodate-cli/cmd/lib/cache" |
| 11 | +) |
| 12 | + |
| 13 | +// Component represents a single technology in the stack |
| 14 | +type Component struct { |
| 15 | + Name string `json:"name"` |
| 16 | + Version string `json:"version"` |
| 17 | +} |
| 18 | + |
| 19 | +// ConvertStackToComponents converts the detected stack format to API component format |
| 20 | +func ConvertStackToComponents(stack map[string]StackEntry) []Component { |
| 21 | + components := make([]Component, 0) |
| 22 | + |
| 23 | + for name, entry := range stack { |
| 24 | + components = append(components, Component{ |
| 25 | + Name: name, |
| 26 | + Version: entry.Version, |
| 27 | + }) |
| 28 | + } |
| 29 | + |
| 30 | + return components |
| 31 | +} |
| 32 | + |
| 33 | +// TechStackRequest is used for POST /api/tech_stacks |
| 34 | +type TechStackRequest struct { |
| 35 | + TechStack struct { |
| 36 | + Name string `json:"name"` |
| 37 | + Components []Component `json:"components"` |
| 38 | + } `json:"tech_stack"` |
| 39 | +} |
| 40 | + |
| 41 | +// TechStackResponse is the response from both GET and POST tech stack endpoints |
| 42 | +type TechStackResponse struct { |
| 43 | + Success bool `json:"success,omitempty"` |
| 44 | + Message string `json:"message,omitempty"` |
| 45 | + TechStack struct { |
| 46 | + ID string `json:"id"` |
| 47 | + Name string `json:"name"` |
| 48 | + Components []Component `json:"components"` |
| 49 | + } `json:"tech_stack"` |
| 50 | +} |
| 51 | + |
| 52 | +// CreateTechStack creates a new tech stack on the API |
| 53 | +// Returns the newly created tech stack with UUID |
| 54 | +func CreateTechStack(token, name string, components []Component) (*TechStackResponse, error) { |
| 55 | + apiURL := cache.GetAPIURL() |
| 56 | + url := fmt.Sprintf("%s/api/tech_stacks", apiURL) |
| 57 | + |
| 58 | + request := TechStackRequest{} |
| 59 | + request.TechStack.Name = name |
| 60 | + request.TechStack.Components = components |
| 61 | + |
| 62 | + var response TechStackResponse |
| 63 | + if err := makeAPIRequest("POST", url, token, request, &response); err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + if !response.Success { |
| 68 | + return nil, fmt.Errorf("API error: %s", response.Message) |
| 69 | + } |
| 70 | + |
| 71 | + if response.TechStack.ID == "" { |
| 72 | + return nil, fmt.Errorf("API response missing project ID") |
| 73 | + } |
| 74 | + |
| 75 | + return &response, nil |
| 76 | +} |
| 77 | + |
| 78 | +// GetTechStack retrieves an existing tech stack from the API by UUID |
| 79 | +// This validates that the project exists and returns its details |
| 80 | +func GetTechStack(token, uuid string) (*TechStackResponse, error) { |
| 81 | + apiURL := cache.GetAPIURL() |
| 82 | + url := fmt.Sprintf("%s/api/tech_stacks/%s", apiURL, uuid) |
| 83 | + |
| 84 | + var response TechStackResponse |
| 85 | + if err := makeAPIRequest("GET", url, token, nil, &response); err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + if response.TechStack.ID == "" { |
| 90 | + return nil, fmt.Errorf("API response missing project ID") |
| 91 | + } |
| 92 | + |
| 93 | + return &response, nil |
| 94 | +} |
| 95 | + |
| 96 | +// makeAPIRequest is a private helper that handles common API request logic |
| 97 | +func makeAPIRequest(method, url, token string, requestBody interface{}, response interface{}) error { |
| 98 | + var req *http.Request |
| 99 | + var err error |
| 100 | + |
| 101 | + // Create request with body if provided |
| 102 | + if requestBody != nil { |
| 103 | + requestBodyJSON, err := json.Marshal(requestBody) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("failed to marshal request: %w", err) |
| 106 | + } |
| 107 | + req, err = http.NewRequest(method, url, bytes.NewBuffer(requestBodyJSON)) |
| 108 | + } else { |
| 109 | + req, err = http.NewRequest(method, url, nil) |
| 110 | + } |
| 111 | + |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("failed to create request: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + // Set headers |
| 117 | + req.Header.Set("Content-Type", "application/json") |
| 118 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) |
| 119 | + |
| 120 | + // Make request |
| 121 | + client := &http.Client{} |
| 122 | + resp, err := client.Do(req) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("failed to connect to StackToDate API: %w\n\nPlease check your internet connection and try again", err) |
| 125 | + } |
| 126 | + defer resp.Body.Close() |
| 127 | + |
| 128 | + // Read response body |
| 129 | + body, err := io.ReadAll(resp.Body) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("failed to read response: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + // Handle error responses first |
| 135 | + if resp.StatusCode == http.StatusUnauthorized { |
| 136 | + return fmt.Errorf("authentication failed: invalid or expired token\n\nPlease update your token with: stacktodate global-config set") |
| 137 | + } |
| 138 | + |
| 139 | + if resp.StatusCode == http.StatusNotFound { |
| 140 | + return fmt.Errorf("project not found: UUID does not exist\n\nPlease check the UUID or create a new project") |
| 141 | + } |
| 142 | + |
| 143 | + if resp.StatusCode == http.StatusUnprocessableEntity { |
| 144 | + var errResp struct { |
| 145 | + Message string `json:"message"` |
| 146 | + } |
| 147 | + if err := json.Unmarshal(body, &errResp); err == nil && errResp.Message != "" { |
| 148 | + return fmt.Errorf("validation error: %s", errResp.Message) |
| 149 | + } |
| 150 | + return fmt.Errorf("validation error: the server rejected your request") |
| 151 | + } |
| 152 | + |
| 153 | + if resp.StatusCode >= 500 { |
| 154 | + return fmt.Errorf("StackToDate API is experiencing issues (status %d)\n\nPlease try again later", resp.StatusCode) |
| 155 | + } |
| 156 | + |
| 157 | + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { |
| 158 | + return fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(body)) |
| 159 | + } |
| 160 | + |
| 161 | + // Parse successful response |
| 162 | + if err := json.Unmarshal(body, response); err != nil { |
| 163 | + return fmt.Errorf("failed to parse API response: %w", err) |
| 164 | + } |
| 165 | + |
| 166 | + return nil |
| 167 | +} |
0 commit comments