Skip to content

Commit 2c3e276

Browse files
committed
refactor: simplify the check api
1 parent a4c1f90 commit 2c3e276

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

.github/configs/setup-my-action.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ number_list = [1.0, 2.0, 3.0, 4.0, 5.0]
55
input_file = "input.txt"
66
output_file = "output.txt"
77
append_text = "This text will be appended."
8-
api_url = "https://api.example.com/data"
8+
api_url = "https://example.org"

main.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"io"
87
"log"
@@ -89,16 +88,17 @@ func readAndAppendToFile(inputFile, outputFile, appendText string) error {
8988
return os.WriteFile(outputFile, []byte(modifiedContent), 0644)
9089
}
9190

92-
func callAPIAndExtractField(ctx context.Context, apiURL string) (string, error) {
91+
func checkAPIReachability(ctx context.Context, apiURL string) error {
92+
// 创建带有上下文的 HTTP GET 请求
9393
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil)
9494
if err != nil {
95-
return "", fmt.Errorf("failed to create request: %w", err)
95+
return fmt.Errorf("failed to create request: %w", err)
9696
}
9797

9898
client := &http.Client{Timeout: httpTimeout}
9999
resp, err := client.Do(req)
100100
if err != nil {
101-
return "", fmt.Errorf("failed to make API request: %w", err)
101+
return fmt.Errorf("failed to make API request: %w", err)
102102
}
103103
defer func(Body io.ReadCloser) {
104104
err := Body.Close()
@@ -107,22 +107,12 @@ func callAPIAndExtractField(ctx context.Context, apiURL string) (string, error)
107107
}
108108
}(resp.Body)
109109

110-
body, err := io.ReadAll(resp.Body)
111-
if err != nil {
112-
return "", fmt.Errorf("failed to read API response: %w", err)
113-
}
114-
115-
var result map[string]interface{}
116-
if err := json.Unmarshal(body, &result); err != nil {
117-
return "", fmt.Errorf("failed to parse JSON response: %w", err)
110+
// 检查响应状态码是否在 200-299 范围内
111+
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
112+
return fmt.Errorf("API is not reachable, status code: %d", resp.StatusCode)
118113
}
119114

120-
dataField, ok := result["data"].(string)
121-
if !ok {
122-
return "", fmt.Errorf("field 'data' not found in API response or is not a string")
123-
}
124-
125-
return dataField, nil
115+
return nil
126116
}
127117

128118
func setOutput(name, value string) error {
@@ -179,11 +169,11 @@ func main() {
179169
ctx, cancel := context.WithTimeout(context.Background(), contextTimeout)
180170
defer cancel()
181171

182-
responseField, err := callAPIAndExtractField(ctx, config.ApiURL)
172+
err = checkAPIReachability(ctx, config.ApiURL)
183173
if err != nil {
184174
log.Fatalf("API request error: %v", err)
185175
}
186-
if err := setOutput("response_field", responseField); err != nil {
176+
if err := setOutput("response_field", "API Reachable"); err != nil {
187177
log.Fatalf("Failed to set output: %v", err)
188178
}
189179
}

0 commit comments

Comments
 (0)