Skip to content

Commit

Permalink
Merge pull request #50 from statsbomb/public-api
Browse files Browse the repository at this point in the history
Add status check to public api functions
  • Loading branch information
scotty779 authored Jun 15, 2023
2 parents cf11afa + d4386c5 commit 5188bac
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="statsbombpy",
version="1.10.0",
version="1.10.1",
description="easily stream StatsBomb data into Python",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
29 changes: 19 additions & 10 deletions statsbombpy/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,44 @@
from statsbombpy.config import OPEN_DATA_PATHS


def get_response(path):
response = req.get(path)
response.raise_for_status()
data = response.json()
return data


def competitions():
competitions = req.get(OPEN_DATA_PATHS["competitions"]).json()
competitions = get_response(OPEN_DATA_PATHS["competitions"])
competitions = ents.competitions(competitions)
return competitions


def matches(competition_id: int, season_id: int) -> dict:
matches = req.get(
OPEN_DATA_PATHS["matches"].format(
competition_id=competition_id, season_id=season_id
)
).json()
path = OPEN_DATA_PATHS["matches"].format(
competition_id=competition_id, season_id=season_id
)
matches = get_response(path)
matches = ents.matches(matches)
return matches


def lineups(match_id: int):
lineups = req.get(OPEN_DATA_PATHS["lineups"].format(match_id=match_id)).json()
path = OPEN_DATA_PATHS["lineups"].format(match_id=match_id)
lineups = get_response(path)
lineups = ents.lineups(lineups)
return lineups


def events(match_id: int) -> dict:
events = req.get(OPEN_DATA_PATHS["events"].format(match_id=match_id)).json()
path = OPEN_DATA_PATHS["events"].format(match_id=match_id)
events = get_response(path)
events = ents.events(events, match_id)
return events


def frames(match_id: int) -> dict:
frames = req.get(OPEN_DATA_PATHS["frames"].format(match_id=match_id)).json()
path = OPEN_DATA_PATHS["frames"].format(match_id=match_id)
frames = get_response(path)
frames = ents.frames(frames, match_id)
return frames
return frames
21 changes: 18 additions & 3 deletions tests/statsbombpy_test/test_sb.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from unittest import TestCase, main

import pandas as pd

from requests.exceptions import HTTPError
from statsbombpy import sb
from statsbombpy.api_client import matches


class TestBaseGetters(TestCase):
Expand Down Expand Up @@ -42,6 +41,10 @@ def test_matches(self):
"Ernesto Valverde Tejedor",
)

with self.assertRaises(HTTPError) as cm:
matches = sb.matches(competition_id=1, season_id=1, creds={})
self.assertEqual(cm.exception.response.status_code, 404)

def test_lineups(self):
lineups = sb.lineups(match_id=7562)
self.assertIsInstance(lineups, dict)
Expand All @@ -59,6 +62,10 @@ def test_lineups(self):
"England",
)

with self.assertRaises(HTTPError) as cm:
lineups = sb.lineups(match_id=1, creds={})
self.assertEqual(cm.exception.response.status_code, 404)


class TestEventGetters(TestCase):
def test_events(self):
Expand Down Expand Up @@ -88,6 +95,10 @@ def test_events(self):
self.assertIsInstance(events, pd.DataFrame)
self.assertTrue("visible_teammates" in events.columns)

with self.assertRaises(HTTPError) as cm:
events = sb.events(match_id=1, creds={})
self.assertEqual(cm.exception.response.status_code, 404)

def test_competition_events(self):
events = sb.competition_events(
country="Europe",
Expand Down Expand Up @@ -137,6 +148,10 @@ def test_frames(self):
frames = sb.frames(match_id=3847567, creds={})
self.assertIsInstance(frames, pd.DataFrame)

with self.assertRaises(HTTPError) as cm:
frames = sb.frames(match_id=1, creds={})
self.assertEqual(cm.exception.response.status_code, 404)

def test_competition_frames(self):
frames = sb.competition_frames(
country="Europe",
Expand Down Expand Up @@ -216,4 +231,4 @@ def test_team_season_stats(self):


if __name__ == "__main__":
main()
main()

0 comments on commit 5188bac

Please sign in to comment.