109 lines
3.9 KiB
Python
109 lines
3.9 KiB
Python
from ..parser.models import ParsedCurl
|
|
|
|
|
|
def generate_go(parsed: ParsedCurl) -> str:
|
|
"""Generate Go net/http code from parsed curl command."""
|
|
lines = [
|
|
"package main",
|
|
"",
|
|
"import (",
|
|
' "bytes"',
|
|
' "encoding/json"',
|
|
' "fmt"',
|
|
' "net/http"',
|
|
' "time"',
|
|
")",
|
|
"",
|
|
]
|
|
|
|
if parsed.auth and parsed.auth.username and parsed.auth.password:
|
|
lines.extend([
|
|
"func main() {",
|
|
f' url := "{parsed.url}"',
|
|
"",
|
|
f' username := "{parsed.auth.username}"',
|
|
f' password := "{parsed.auth.password}"',
|
|
' authString := username + ":" + password',
|
|
' authEncoded := "Basic " + authString',
|
|
"",
|
|
])
|
|
else:
|
|
lines.extend([
|
|
"func main() {",
|
|
f' url := "{parsed.url}"',
|
|
"",
|
|
])
|
|
|
|
headers_list = []
|
|
for h in parsed.headers:
|
|
headers_list.append(f' "{h.key}": "{h.value}"')
|
|
|
|
if parsed.user_agent:
|
|
headers_list.append(f' "User-Agent": "{parsed.user_agent}"')
|
|
|
|
method_name = parsed.method.value.upper()
|
|
|
|
if parsed.data:
|
|
if parsed.data.data_type.value == "json" and parsed.data.json_data:
|
|
import json
|
|
json_str = json.dumps(parsed.data.json_data)
|
|
lines.append(f" jsonData := []byte({json_str})")
|
|
lines.append("")
|
|
lines.append(f" req, err := http.NewRequest(\"{method_name}\", url, bytes.NewBuffer(jsonData))")
|
|
lines.append(" if err != nil {")
|
|
lines.append(' fmt.Printf(\"Error: %v\\n\", err)')
|
|
lines.append(" return")
|
|
lines.append(" }")
|
|
lines.append("")
|
|
else:
|
|
lines.append(f" data := {repr(parsed.data.content)}")
|
|
lines.append("")
|
|
lines.append(f" req, err := http.NewRequest(\"{method_name}\", url, bytes.NewBufferString(data))")
|
|
lines.append(" if err != nil {")
|
|
lines.append(' fmt.Printf(\"Error: %v\\n\", err)')
|
|
lines.append(" return")
|
|
lines.append(" }")
|
|
lines.append("")
|
|
else:
|
|
lines.append(f" req, err := http.NewRequest(\"{method_name}\", url, nil)")
|
|
lines.append(" if err != nil {")
|
|
lines.append(' fmt.Printf(\"Error: %v\\n\", err)')
|
|
lines.append(" return")
|
|
lines.append(" }")
|
|
lines.append("")
|
|
|
|
if parsed.auth and parsed.auth.username and parsed.auth.password:
|
|
lines.append(' req.Header.Set("Authorization", authEncoded)')
|
|
|
|
for h in parsed.headers:
|
|
lines.append(f' req.Header.Set("{h.key}", "{h.value}")')
|
|
if parsed.user_agent:
|
|
lines.append(f' req.Header.Set("User-Agent", "{parsed.user_agent}")')
|
|
if headers_list:
|
|
lines.append("")
|
|
|
|
if parsed.timeout:
|
|
lines.append(f" client := &http.Client{{Timeout: {parsed.timeout} * time.Second}}")
|
|
else:
|
|
lines.append(" client := &http.Client{}")
|
|
lines.append("")
|
|
lines.append(" resp, err := client.Do(req)")
|
|
lines.append(" if err != nil {")
|
|
lines.append(' fmt.Printf(\"Error: %v\\n\", err)')
|
|
lines.append(" return")
|
|
lines.append(" }")
|
|
lines.append(" defer resp.Body.Close()")
|
|
lines.append("")
|
|
lines.append(" if resp.StatusCode != http.StatusOK {")
|
|
lines.append(' fmt.Printf(\"HTTP error: %d\\n\", resp.StatusCode)')
|
|
lines.append(" return")
|
|
lines.append(" }")
|
|
lines.append("")
|
|
lines.append(" var result map[string]interface{}")
|
|
lines.append(" json.NewDecoder(resp.Body).Decode(&result)")
|
|
lines.append(" jsonData, _ := json.MarshalIndent(result, \"\", \" \")")
|
|
lines.append(' fmt.Printf(\"%s\\n\", jsonData)')
|
|
lines.append("}")
|
|
|
|
return "\n".join(lines)
|