Created
May 5, 2024 19:42
-
-
Save sergei-svistunov/e7971d1115245cdb554d97f1414a480a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"os" | |
"strings" | |
) | |
type RequestData struct { | |
ID int `json:"id"` | |
Method string `json:"method"` | |
Path string `json:"path"` | |
Headers map[string]string `json:"headers"` | |
Checks map[string]interface{} `json:"checks"` | |
} | |
func main() { | |
// Open the JSONL file | |
file, err := os.Open("data.jsonl") | |
if err != nil { | |
fmt.Println("Error opening file:", err) | |
return | |
} | |
defer file.Close() | |
scanner := bufio.NewScanner(file) | |
// Read each line (JSON object) from the file | |
for scanner.Scan() { | |
line := scanner.Text() | |
// Decode JSON object into RequestData struct | |
var requestData RequestData | |
if err := json.Unmarshal([]byte(line), &requestData); err != nil { | |
fmt.Println("Error decoding JSON:", err) | |
continue | |
} | |
// Make HTTP request | |
resp, err := makeRequest(requestData.Method, requestData.Path, requestData.Headers) | |
if err != nil { | |
fmt.Println("Error making request:", err) | |
continue | |
} | |
defer resp.Body.Close() | |
// Check response based on checks field | |
if resp.StatusCode == int(requestData.Checks["code"].(float64)) { | |
fmt.Printf("Request ID %d: Success\n", requestData.ID) | |
} else { | |
fmt.Printf("Request ID %d: Failure\n", requestData.ID) | |
} | |
} | |
if err := scanner.Err(); err != nil { | |
fmt.Println("Error reading file:", err) | |
return | |
} | |
} | |
func makeRequest(method, path string, headers map[string]string) (*http.Response, error) { | |
// Create a new HTTP request | |
req, err := http.NewRequest(method, path, nil) | |
if err != nil { | |
return nil, err | |
} | |
// Add headers to the request | |
for key, value := range headers { | |
req.Header.Set(key, value) | |
} | |
// Perform the request | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
return resp, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment