When you spectate a game in League of Legends, you tell the client to use a HTTP server, and make HTTP requests to it to retrieve data in chunks which make up a game. But what if you could return back the exact same data at a later time, simulating the spectate but viewing it at anytime? That's the concept behind replays and that's how they work.
There is some behavior in the API which I do not fully understand yet, so there are if statements to catch these edge cases.
Before you can even get the game's metadata, you'll need to retrieve necessary information for the game. This call is part of the official Riot Games API.
/observer-mode/rest/consumer/getSpectatorGameInfo/{platformId}/{summonerId}