From 446ba1bfebab94bea0d3a6585ee5fc34e0ed1e58 Mon Sep 17 00:00:00 2001 From: Corentin Pazdera Date: Fri, 9 Feb 2024 13:52:09 +0100 Subject: [PATCH] Add support for Oauth device authentication + setting useragent + add one API endpoint --- BetaSeries.Net/Models/Oauth.cs | 47 +++++++++++++++++++++++++++++++++- BetaSeries.Net/Models/Shows.cs | 3 +++ BetaSeries.Net/RestHelper.cs | 14 ++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/BetaSeries.Net/Models/Oauth.cs b/BetaSeries.Net/Models/Oauth.cs index 6329337..5df4d5f 100644 --- a/BetaSeries.Net/Models/Oauth.cs +++ b/BetaSeries.Net/Models/Oauth.cs @@ -1,14 +1,59 @@ using BetaSeries.Net.Core; +using BetaSeries.Net.Exceptions; +using System.Threading.Tasks; namespace BetaSeries.Net.Models { public abstract class OAUTH { #region Public Classes + private static string _device_code; + private static string _developerKey; + private static string _oautsecret; + + public static void RegisterDeveloperKey(string key) + { + _developerKey = key; + } + + public static void RegisterOAuthSecret(string secret) + { + _oautsecret = secret; + } [Rest] - public class Access_token : RestMethod { } + public class Access_token : RestMethod + { + public static async System.Threading.Tasks.Task PollForAuthorization() + { + while (true) + { + try + { + dynamic result = await Post(new { client_id = _developerKey, client_secret = _oautsecret, code = _device_code }); + + RestHelper.RegisterUserToken(result.access_token.ToString()); + return result; + } + catch (BetaSeriesException) + { + await Task.Delay(5000); + } + } + } + } + + [Rest] + public class Device : RestMethod + { + public static async Task Post() + { + dynamic res = await RestHelper.Post(new { }); + _device_code = res.device_code; + return res; + } + } #endregion Public Classes } } \ No newline at end of file diff --git a/BetaSeries.Net/Models/Shows.cs b/BetaSeries.Net/Models/Shows.cs index 778b62c..69ed9a0 100644 --- a/BetaSeries.Net/Models/Shows.cs +++ b/BetaSeries.Net/Models/Shows.cs @@ -54,6 +54,9 @@ public class Recommendations : RestMethod { } [Rest] public class Search : RestMethod { } + [Rest] + public class Seasons : RestMethod { } + [Rest] public class Show : RestMethod { } diff --git a/BetaSeries.Net/RestHelper.cs b/BetaSeries.Net/RestHelper.cs index 6ea6d4d..41078cb 100644 --- a/BetaSeries.Net/RestHelper.cs +++ b/BetaSeries.Net/RestHelper.cs @@ -19,6 +19,7 @@ public static class RestHelper private static string _apiBaseUri = "https://api.betaseries.com/"; private static string _apiVersion = "3.0"; private static string _developerKey = null; + private static string _userAgent = "BetaSeries.Net/0.0.1"; private static Dictionary _urlByType = new Dictionary(); private static string _userToken = null; @@ -105,6 +106,13 @@ public static async Task Put(dynamic parameters) where T : class public static void RegisterDeveloperKey(string key) { _developerKey = key; + OAUTH.RegisterDeveloperKey(key); + } + + public static void RegisterDeveloperKey(string key, string oauthsecret) + { + RegisterDeveloperKey(key); + OAUTH.RegisterOAuthSecret(oauthsecret); } public static void RegisterUserToken(string token) @@ -112,6 +120,10 @@ public static void RegisterUserToken(string token) _userToken = token; } + public static void SetUserAgent(string userAgent) + { + _userAgent = userAgent; + } #endregion Public Methods #region Private Methods @@ -177,6 +189,8 @@ private static async Task Request(AllowedHttpVerbs method, string ur client.BaseAddress = new Uri(_apiBaseUri); client.DefaultRequestHeaders.Add("X-BetaSeries-Key", _developerKey); client.DefaultRequestHeaders.Add("X-BetaSeries-Version", _apiVersion); + client.DefaultRequestHeaders.UserAgent.ParseAdd(_userAgent); + //add usertoken if (_userToken != null)