feat: initial FRED API Proxy project
- Go web server using Gin framework - Proxy FRED economic data API - Support series, observations, search, releases endpoints
This commit is contained in:
3
tests/go.mod
Normal file
3
tests/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module fred-api-test
|
||||
|
||||
go 1.21
|
||||
88
tests/test_fred_api.go
Normal file
88
tests/test_fred_api.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
APIKey = "987b9c3428a3d0fe90179e3e6298d05e"
|
||||
BaseURL = "https://api.stlouisfed.org/fred"
|
||||
)
|
||||
|
||||
func buildURL(endpoint string, params map[string]string) string {
|
||||
u, _ := url.Parse(BaseURL + endpoint)
|
||||
query := u.Query()
|
||||
query.Set("api_key", APIKey)
|
||||
query.Set("file_type", "json")
|
||||
for k, v := range params {
|
||||
if v != "" {
|
||||
query.Set(k, v)
|
||||
}
|
||||
}
|
||||
u.RawQuery = query.Encode()
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func fetchAndPrint(endpoint string, params map[string]string) {
|
||||
url := buildURL(endpoint, params)
|
||||
fmt.Printf("\n=== Testing %s ===\n", endpoint)
|
||||
fmt.Printf("URL: %s\n\n", url)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading body: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
var prettyJSON map[string]interface{}
|
||||
if err := json.Unmarshal(body, &prettyJSON); err == nil {
|
||||
output, _ := json.MarshalIndent(prettyJSON, "", " ")
|
||||
fmt.Printf("Response:\n%s\n", output)
|
||||
} else {
|
||||
fmt.Printf("Response:\n%s\n", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("========================================")
|
||||
fmt.Println(" FRED API 测试脚本")
|
||||
fmt.Println("========================================")
|
||||
|
||||
fetchAndPrint("/series", map[string]string{"series_id": "GNPCA"})
|
||||
|
||||
fetchAndPrint("/series/observations", map[string]string{
|
||||
"series_id": "GNPCA",
|
||||
"limit": "5",
|
||||
"observation_end": "2000-01-01",
|
||||
})
|
||||
|
||||
fetchAndPrint("/series/search", map[string]string{
|
||||
"search_text": "GDP",
|
||||
"limit": "3",
|
||||
})
|
||||
|
||||
fetchAndPrint("/releases", map[string]string{
|
||||
"limit": "3",
|
||||
})
|
||||
|
||||
fetchAndPrint("/releases/dates", map[string]string{
|
||||
"limit": "3",
|
||||
"start_date": "2026-03-01",
|
||||
"end_date": "2026-03-31",
|
||||
})
|
||||
|
||||
fmt.Println("\n========================================")
|
||||
fmt.Println(" 测试完成!")
|
||||
fmt.Println("========================================")
|
||||
}
|
||||
Reference in New Issue
Block a user