From dcba846dfc27dba95824c56ad69e34bd94ab3ff7 Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Mon, 24 Dec 2018 10:36:19 -0500 Subject: [PATCH] Add better content negotiation. --- xonstat/__init__.py | 99 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 78 insertions(+), 21 deletions(-) diff --git a/xonstat/__init__.py b/xonstat/__init__.py index 877e4b8..b192bc3 100644 --- a/xonstat/__init__.py +++ b/xonstat/__init__.py @@ -48,25 +48,35 @@ def main(global_config, **settings): config.add_view(view=summary_stats_json, route_name="summary_stats_json", renderer="json", accept="application/json") + config.add_route("summary_stats_json_suffix", "/summary.json") + config.add_view(summary_stats_json, route_name="summary_stats_json_suffix", renderer="json") + # MAIN SUBMISSION ROUTE config.add_route("submit_stats", "stats/submit") config.add_view(submit_stats, route_name="submit_stats", renderer="submit_stats.mako") # PLAYER ROUTES - config.add_route("player_game_index", "/player/{player_id:\d+}/games") - config.add_view(player_game_index, route_name="player_game_index", renderer="player_game_index.mako") + config.add_route("player_game_index", "/player/{player_id:\d+}/games") + config.add_view(player_game_index, route_name="player_game_index", accept="text/html", + renderer="player_game_index.mako") + config.add_view(player_game_index_json, route_name="player_game_index", + accept="application/json", renderer="jsonp") config.add_route("player_game_index_json", "/player/{player_id:\d+}/games.json") config.add_view(player_game_index_json, route_name="player_game_index_json", renderer="jsonp") - config.add_route("player_info", "/player/{id:\d+}") - config.add_view(player_info, route_name="player_info", renderer="player_info.mako") + config.add_route("player_info", "/player/{id:\d+}") + config.add_view(player_info, route_name="player_info", accept="text/html", + renderer="player_info.mako") + config.add_view(player_info_json, route_name="player_info", accept="application/json", + renderer="jsonp") config.add_route("player_info_json", "/player/{id:\d+}.json") config.add_view(player_info_json, route_name="player_info_json", renderer="jsonp") config.add_route("player_hashkey_info_text", "/player/me") - config.add_view(player_hashkey_info_text, route_name="player_hashkey_info_text", renderer="player_hashkey_info_text.mako") + config.add_view(player_hashkey_info_text, route_name="player_hashkey_info_text", + renderer="player_hashkey_info_text.mako") config.add_route("player_hashkey_info_json", "/player/me.json") config.add_view(player_hashkey_info_json, route_name="player_hashkey_info_json", renderer="jsonp") @@ -81,26 +91,35 @@ def main(global_config, **settings): config.add_route("player_accuracy", "/player/{id:\d+}/accuracy") config.add_view(player_accuracy_json, route_name="player_accuracy", renderer="jsonp") - config.add_route("player_index", "/players") - config.add_view(player_index, route_name="player_index", renderer="player_index.mako") + config.add_route("player_index", "/players") + config.add_view(player_index, route_name="player_index", accept="text/html", + renderer="player_index.mako") + config.add_view(player_index_json, route_name="player_index", accept="application/json", + renderer="jsonp") config.add_route("player_index_json", "/players.json") config.add_view(player_index_json, route_name="player_index_json", renderer="jsonp") - config.add_route("player_captimes", "/player/{player_id:\d+}/captimes") - config.add_view(player_captimes, route_name="player_captimes", renderer="player_captimes.mako") + config.add_route("player_captimes", "/player/{player_id:\d+}/captimes") + config.add_view(player_captimes, route_name="player_captimes", accept="text/html", + renderer="player_captimes.mako") + config.add_view(player_captimes_json, route_name="player_captimes", accept="application/json", + renderer="jsonp") config.add_route("player_captimes_json", "/player/{player_id:\d+}/captimes.json") config.add_view(player_captimes_json, route_name="player_captimes_json", renderer="jsonp") config.add_route("player_weaponstats_data_json", "/player/{id:\d+}/weaponstats.json") - config.add_view(player_weaponstats_data_json, route_name="player_weaponstats_data_json", renderer="jsonp") + config.add_view(player_weaponstats_data_json, route_name="player_weaponstats_data_json", + renderer="jsonp") config.add_route("top_players_index", "/topactive") - config.add_view(top_players_index, route_name="top_players_index", renderer="top_players_index.mako") + config.add_view(top_players_index, route_name="top_players_index", + renderer="top_players_index.mako") config.add_route("top_servers_index", "/topservers") - config.add_view(top_servers_index, route_name="top_servers_index", renderer="top_servers_index.mako") + config.add_view(top_servers_index, route_name="top_servers_index", + renderer="top_servers_index.mako") config.add_route("top_maps_index", "/topmaps") config.add_view(top_maps_index, route_name="top_maps_index", renderer="top_maps_index.mako") @@ -109,8 +128,11 @@ def main(global_config, **settings): config.add_view(player_versus, route_name="player_versus", renderer="player_versus.mako") # GAME ROUTES - config.add_route("game_info", "/game/{id:\d+}") - config.add_view(game_info, route_name="game_info", renderer="game_info.mako") + config.add_route("game_info", "/game/{id:\d+}") + config.add_view(game_info, route_name="game_info", accept="text/html", + renderer="game_info.mako") + config.add_view(game_info_json, route_name="game_info", accept="application/json", + renderer="jsonp") config.add_route("game_info_json", "/game/{id:\d+}.json") config.add_view(game_info_json, route_name="game_info_json", renderer="jsonp") @@ -134,24 +156,39 @@ def main(global_config, **settings): config.add_view(view=ServerTopMaps, route_name="server_top_maps", attr="json", renderer="json", accept="application/json") + config.add_route("server_top_maps_json", "/server/{id:\d+}/topmaps.json") + config.add_view(view=ServerTopMaps, route_name="server_top_maps_json", attr="json", + renderer="json") + config.add_route("server_top_active", "/server/{id:\d+}/topactive") config.add_view(view=ServerTopPlayers, route_name="server_top_active", attr="html", renderer="server_top_active.mako", accept="text/html") config.add_view(view=ServerTopPlayers, route_name="server_top_active", attr="json", renderer="json", accept="application/json") + config.add_route("server_top_active_json", "/server/{id:\d+}/topactive.json") + config.add_view(view=ServerTopPlayers, route_name="server_top_active_json", attr="json", + renderer="json") + config.add_route("server_top_scorers", "/server/{id:\d+}/topscorers") config.add_view(view=ServerTopScorers, route_name="server_top_scorers", attr="html", renderer="server_top_scorers.mako", accept="text/html") config.add_view(view=ServerTopScorers, route_name="server_top_scorers", attr="json", renderer="json", accept="application/json") + config.add_route("server_top_scorers_json", "/server/{id:\d+}/topscorers.json") + config.add_view(view=ServerTopScorers, route_name="server_top_scorers_json", attr="json", + renderer="json") + config.add_route("server_info", "/server/{id:\d+}") config.add_view(view=ServerInfo, route_name="server_info", attr="html", renderer="server_info.mako", accept="text/html") config.add_view(view=ServerInfo, route_name="server_info", attr="json", renderer="json", accept="application/json") + config.add_route("server_info_json", "/server/{id:\d+}.json") + config.add_view(view=ServerInfo, route_name="server_info_json", attr="json", renderer="json") + # MAP ROUTES config.add_route("map_index", "/maps") config.add_view(view=MapIndex, route_name="map_index", attr="html", @@ -159,39 +196,59 @@ def main(global_config, **settings): config.add_view(view=MapIndex, route_name="map_index", attr="json", renderer="json", accept="application/json") + config.add_route("map_index_json", "/maps.json") + config.add_view(view=MapIndex, route_name="map_index_json", attr="json", renderer="json") + config.add_route("map_top_scorers", "/map/{id:\d+}/topscorers") config.add_view(view=MapTopScorers, route_name="map_top_scorers", attr="html", renderer="map_top_scorers.mako", accept="text/html") - config.add_view(view=MapTopScorers, route_name="map_top_scorers", attr="json", + config.add_view(view=MapTopScorers, route_name="map_top_scorers_json", attr="json", renderer="json", accept="application/json") + config.add_route("map_top_scorers_json", "/map/{id:\d+}/topscorers.json") + config.add_view(view=MapTopScorers, route_name="map_top_scorers", attr="json", renderer="json") + config.add_route("map_top_active", "/map/{id:\d+}/topactive") config.add_view(view=MapTopPlayers, route_name="map_top_active", attr="html", renderer="map_top_active.mako", accept="text/html") config.add_view(view=MapTopPlayers, route_name="map_top_active", attr="json", renderer="json", accept="application/json") + config.add_route("map_top_active_json", "/map/{id:\d+}/topactive.json") + config.add_view(view=MapTopPlayers, route_name="map_top_active_json", attr="json", + renderer="json") + config.add_route("map_top_servers", "/map/{id:\d+}/topservers") config.add_view(view=MapTopServers, route_name="map_top_servers", attr="html", renderer="map_top_servers.mako", accept="text/html") config.add_view(view=MapTopServers, route_name="map_top_servers", attr="json", renderer="json", accept="application/json") - config.add_route("map_info", "/map/{id:\d+}") - config.add_view(map_info, route_name="map_info", renderer="map_info.mako") + config.add_route("map_top_servers_json", "/map/{id:\d+}/topservers.json") + config.add_view(view=MapTopServers, route_name="map_top_servers_json", attr="json", + renderer="json") + + config.add_route("map_info", "/map/{id:\d+}") + config.add_view(map_info, route_name="map_info", accept="text/html", renderer="map_info.mako") + config.add_view(map_info_json, route_name="map_info", accept="application/json", + renderer="json") config.add_route("map_info_json", "/map/{id:\d+}.json") config.add_view(map_info_json, route_name="map_info_json", renderer="jsonp") - config.add_route("map_captimes", "/map/{id:\d+}/captimes") - config.add_view(map_captimes, route_name="map_captimes", renderer="map_captimes.mako") + config.add_route("map_captimes", "/map/{id:\d+}/captimes") + config.add_view(map_captimes, route_name="map_captimes", accept="text/html", + renderer="map_captimes.mako") + config.add_view(map_captimes_json, route_name="map_captimes", accept="application/json", + renderer="json") config.add_route("map_captimes_json", "/map/{id:\d+}/captimes.json") config.add_view(map_captimes_json, route_name="map_captimes_json", renderer="jsonp") # SEARCH ROUTES - config.add_route("search", "search") - config.add_view(search, route_name="search", renderer="search.mako") + config.add_route("search", "search") + config.add_view(search, route_name="search", accept="text/html", renderer="search.mako") + config.add_view(search_json, route_name="search", accept="application/json", renderer="jsonp") config.add_route("search_json", "search.json") config.add_view(search_json, route_name="search_json", renderer="jsonp") -- 2.39.2