53200cbc0d
Nuevas funciones Go con tests en tres dominios: - core: parse_cron_expr, next_cron_time, join_by_key, validate_struct_fields + tipo CronSchedule - datascience: pivot (tabla dinámica), diff_entities (comparación de entidades) - infra: http_get_json, http_post_json, http_download_file, cache_to_sqlite, cron_ticker Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
44 lines
1.3 KiB
Markdown
44 lines
1.3 KiB
Markdown
---
|
|
name: http_get_json
|
|
kind: function
|
|
lang: go
|
|
domain: infra
|
|
version: "1.0.0"
|
|
purity: impure
|
|
signature: "func HttpGetJSON(url string, headers map[string]string, timeout time.Duration) (map[string]any, error)"
|
|
description: "GET request que espera JSON. Agrega Accept: application/json automaticamente. Retorna error con status code si >= 400. Siempre cierra body con defer."
|
|
tags: [http, json, get, client, network, stdlib, infra]
|
|
uses_functions: []
|
|
uses_types: []
|
|
returns: []
|
|
returns_optional: false
|
|
error_type: "error_go_core"
|
|
imports: ["encoding/json", "fmt", "io", "net/http", "time"]
|
|
tested: true
|
|
tests:
|
|
- "httptest.Server con respuesta JSON"
|
|
- "Status 404 → error"
|
|
- "Timeout → error"
|
|
- "Headers custom"
|
|
test_file_path: "functions/infra/http_get_json_test.go"
|
|
file_path: "functions/infra/http_get_json.go"
|
|
---
|
|
|
|
## Ejemplo
|
|
|
|
```go
|
|
result, err := HttpGetJSON(
|
|
"https://api.example.com/users",
|
|
map[string]string{"X-Api-Key": "secret"},
|
|
10*time.Second,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fmt.Println(result["total"])
|
|
```
|
|
|
|
## Notas
|
|
|
|
Solo usa stdlib (net/http, encoding/json). El timeout se configura en el http.Client. El error incluye los primeros 200 bytes del body para facilitar debugging. Los headers custom se fusionan con Accept: application/json (custom tiene precedencia).
|