File size: 3,426 Bytes
f35bff2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
"""
Simple FRED API test
"""

import requests
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from config.settings import FRED_API_KEY

def test_fred_api_direct():
    """Test FRED API directly using requests."""
    print("Testing FRED API directly...")
    
    # Test URL for GDP series
    url = f"https://api.stlouisfed.org/fred/series/observations"
    params = {
        'series_id': 'GDP',
        'api_key': FRED_API_KEY,
        'file_type': 'json',
        'start_date': '2023-01-01',
        'end_date': '2023-12-31'
    }
    
    try:
        response = requests.get(url, params=params)
        
        if response.status_code == 200:
            data = response.json()
            observations = data.get('observations', [])
            
            if observations:
                print("βœ“ API connection successful!")
                print(f"βœ“ Retrieved {len(observations)} GDP observations")
                
                # Get the latest observation
                latest = observations[-1]
                print(f"βœ“ Latest GDP value: ${float(latest['value']):,.2f} billion")
                print(f"βœ“ Date: {latest['date']}")
                return True
            else:
                print("βœ— No observations found")
                return False
        else:
            print(f"βœ— API request failed with status code: {response.status_code}")
            print(f"Response: {response.text}")
            return False
            
    except Exception as e:
        print(f"βœ— API connection failed: {e}")
        return False

def test_series_search():
    """Test searching for series."""
    print("\nTesting series search...")
    
    url = "https://api.stlouisfed.org/fred/series/search"
    params = {
        'search_text': 'GDP',
        'api_key': FRED_API_KEY,
        'file_type': 'json'
    }
    
    try:
        response = requests.get(url, params=params)
        
        if response.status_code == 200:
            data = response.json()
            series = data.get('seriess', [])
            
            if series:
                print("βœ“ Series search successful!")
                print(f"βœ“ Found {len(series)} series matching 'GDP'")
                
                # Show first few results
                for i, s in enumerate(series[:3]):
                    print(f"  {i+1}. {s['id']}: {s['title']}")
                return True
            else:
                print("βœ— No series found")
                return False
        else:
            print(f"βœ— Search request failed: {response.status_code}")
            return False
            
    except Exception as e:
        print(f"βœ— Search failed: {e}")
        return False

def main():
    """Run simple API tests."""
    print("Simple FRED API Test")
    print("=" * 30)
    print(f"API Key: {FRED_API_KEY[:8]}...")
    print()
    
    # Test direct API access
    api_ok = test_fred_api_direct()
    
    # Test series search
    search_ok = test_series_search()
    
    print("\n" + "=" * 30)
    if api_ok and search_ok:
        print("βœ“ All tests passed! Your API key is working correctly.")
        print("The issue is with the fredapi library, not your API key.")
    else:
        print("βœ— Some tests failed. Please check your API key.")
    
    return api_ok and search_ok

if __name__ == "__main__":
    main()