-
Notifications
You must be signed in to change notification settings - Fork 550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add requests Session to API calls #486
base: master
Are you sure you want to change the base?
Conversation
8W9aG
commented
Dec 3, 2024
- A draft PR that implements injectable requests Sessions as outlined in this issue: [Feature]: Allow injectable request sessions #485
@8W9aG - Might take me a few days to get this looked at. Thanks for the commit! |
@8W9aG - Thank you for the PR! After review, I thinking we can simplify the session management to use a global session via NBAHTTP. This approach avoids changing 200+ endpoint files while providing the same functionality. This implementation allows users to configure the session once and have it apply across all endpoint calls or update the session using NBAHTTP.set_session() as needed. Key changes:
Let me know what you think. If a valid path, please update the PR and let me know. Thanks! Proposed Code Change: # Modified NBAHTTP class
class NBAHTTP:
_session = None
@classmethod
def get_session(cls):
if cls._session is None:
cls._session = requests.Session()
return cls._session
@classmethod
def set_session(cls, session):
cls._session = session Sample usage: Copyimport requests
from nba_api.stats.library.http import NBAHTTP
from nba_api.stats.endpoints import AllTimeLeadersGrids
# Set up custom session
session = requests.Session()
session.headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
})
NBAHTTP.set_session(session)
# Use endpoints normally - they'll automatically use the configured session
leaders = AllTimeLeadersGrids(topx=5) Sample Test: import pytest
import requests
from unittest.mock import Mock, patch
from nba_api.stats.library.http import NBAHTTP
from nba_api.stats.endpoints import AllTimeLeadersGrids
@pytest.fixture
def mock_session():
session = Mock(spec=requests.Session)
# Mock the get method to return a response-like object
mock_response = Mock()
mock_response.text = '{"resource": "alltimeleadersgrids", "resultSets": {}}'
mock_response.status_code = 200
mock_response.url = "http://stats.nba.com/stats/alltimeleadersgrids"
session.get.return_value = mock_response
return session
def test_nbahttp_session_management():
# Test default session creation
assert NBAHTTP._session is None
session = NBAHTTP.get_session()
assert isinstance(session, requests.Session)
# Test setting custom session
custom_session = requests.Session()
NBAHTTP.set_session(custom_session)
assert NBAHTTP.get_session() == custom_session
@patch('nba_api.stats.endpoints.alltimeleadersgrids.NBAStatsHTTP.send_api_request')
def test_endpoint_uses_global_session(mock_send_api_request, mock_session):
# Set up global session
NBAHTTP.set_session(mock_session)
# Create endpoint and make request
leaders = AllTimeLeadersGrids(topx=5)
# Verify the session's get method was called
assert mock_session.get.called
# Verify the call parameters
call_kwargs = mock_session.get.call_args.kwargs
assert 'params' in call_kwargs
assert 'TopX' in dict(call_kwargs['params'])
assert dict(call_kwargs['params'])['TopX'] == 5
def test_session_headers_persistence():
# Create session with custom headers
session = requests.Session()
custom_user_agent = 'CustomBot/1.0'
session.headers.update({'User-Agent': custom_user_agent})
# Set as global session
NBAHTTP.set_session(session)
# Verify headers are maintained
current_session = NBAHTTP.get_session()
assert current_session.headers['User-Agent'] == custom_user_agent
@pytest.fixture(autouse=True)
def cleanup():
# Reset session before each test
NBAHTTP._session = None
yield
# Clean up after each test
NBAHTTP._session = None |