Ejemplo n.º 1
0
        public bool Initialize(string apiKey = "")
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;

            _logger.WriteLine("Initializing API base");

            HttpClient = new HttpClient()
            {
                BaseAddress = new Uri("https://api.nexusmods.com"),
                Timeout     = TimeSpan.FromSeconds(5)
            };

            if (apiKey != string.Empty)
            {
                _logger.WriteLine("Apikey not empty");

                ApiKey = apiKey;

                HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpClient.DefaultRequestHeaders.Add("APIKEY", ApiKey);
                HttpClient.DefaultRequestHeaders.Add("Application-Name", "Automaton");
                HttpClient.DefaultRequestHeaders.Add("Application-Version", $"{Assembly.GetEntryAssembly().GetName().Version}");

                // Get the premium status of the account
                var response = HttpClient.GetAsync("/v1/users/validate.json").Result;
                IsPremium  = (bool)JObject.Parse(response.Content.ReadAsStringAsync().Result)["is_premium"];
                IsLoggedIn = true;

                RemainingDailyRequests = Convert.ToInt32(response.Headers.GetValues("X-RL-Daily-Remaining").ToList().First());

                HasLoggedInEvent.Invoke();
            }

            else
            {
                _logger.WriteLine("Apikey empty");

                var guid      = Guid.NewGuid();
                var websocket = new WebSocket("wss://sso.nexusmods.com")
                {
                    SslConfiguration = { EnabledSslProtocols = SslProtocols.Tls12 }
                };


                websocket.OnMessage += (sender, args) =>
                {
                    _logger.WriteLine("Key captured");

                    if (args == null || string.IsNullOrEmpty(args.Data))
                    {
                        return;
                    }

                    ApiKey = args.Data;

                    HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpClient.DefaultRequestHeaders.Add("APIKEY", ApiKey);
                    HttpClient.DefaultRequestHeaders.Add("Application-Name", "Automaton");
                    HttpClient.DefaultRequestHeaders.Add("Application-Version", $"{Assembly.GetEntryAssembly().GetName().Version}");

                    // Get the premium status of the account
                    var response = HttpClient.GetAsync("/v1/users/validate.json").Result;
                    IsPremium  = (bool)JObject.Parse(response.Content.ReadAsStringAsync().Result)["is_premium"];
                    IsLoggedIn = true;

                    RemainingDailyRequests = Convert.ToInt32(response.Headers.GetValues("X-RL-Daily-Remaining").ToList().First());

                    _logger.WriteLine($"User premium status: {IsPremium}");
                    _logger.WriteLine("User is logged in");

                    HasLoggedInEvent.Invoke();

                    websocket.Close();
                };

                websocket.Connect();
                websocket.Send("{\"id\": \"" + guid + "\", \"appid\": \"Automaton\"}");

                Process.Start($"https://www.nexusmods.com/sso?id={guid}&application=Automaton");
            }

            return(false);
        }
Ejemplo n.º 2
0
        public async Task New(GameName gameName, string apiKey = "")
        {
            _gameName   = gameName.ToString().ToLower();
            _httpClient = new HttpClient()
            {
                BaseAddress = new Uri("https://api.nexusmods.com"),
                Timeout     = TimeSpan.FromSeconds(5)
            };

            if (apiKey != string.Empty)
            {
                _apiKey = apiKey;

                _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                _httpClient.DefaultRequestHeaders.Add("APIKEY", _apiKey);
                _httpClient.Timeout = TimeSpan.FromMinutes(1);

                // Get the premium status of the account
                var response = await _httpClient.GetAsync("/v1/users/validate.json");

                _isPremium  = (bool)JObject.Parse(response.Content.ReadAsStringAsync().Result)["is_premium"];
                _isLoggedIn = true;

                RemainingDailyRequests = Convert.ToInt32(response.Headers.GetValues("X-RL-Daily-Remaining").ToList().First());

                HasLoggedInEvent.Invoke();

                return;
            }

            // If there is no API key passed through, generate a new one.
            var guid      = Guid.NewGuid();
            var websocket = new WebSocket("wss://sso.nexusmods.com")
            {
                SslConfiguration = { EnabledSslProtocols = SslProtocols.Tls12 }
            };

            websocket.OnMessage += (sender, args) =>
            {
                if (args == null || string.IsNullOrEmpty(args.Data))
                {
                    return;
                }

                _apiKey = args.Data;

                _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                _httpClient.DefaultRequestHeaders.Add("APIKEY", _apiKey);

                // Get the premium status of the account
                var response = _httpClient.GetAsync("/v1/users/validate.json").Result;
                _isPremium  = (bool)JObject.Parse(response.Content.ReadAsStringAsync().Result)["is_premium"];
                _isLoggedIn = true;

                RemainingDailyRequests = Convert.ToInt32(response.Headers.GetValues("X-RL-Daily-Remaining").ToList().First());

                HasLoggedInEvent.Invoke();
            };

            await Task.Factory.StartNew(() =>
            {
                websocket.Connect();
                websocket.Send("{\"id\": \"" + guid + "\", \"appid\": \"The Automaton Framework\"}");
            });

            Process.Start($"https://www.nexusmods.com/sso?id={guid}");
        }