public void Verify(string apiKey) { try { var verificationRequest = new Verification(); verificationRequest.ApiKey = apiKey; var client = new ProwlClient(); _logger.Debug("Verifying API Key: {0}", apiKey); var verificationResult = client.SendVerification(verificationRequest); if (!string.IsNullOrWhiteSpace(verificationResult.ErrorMessage) && verificationResult.ResultCode != "200") { throw new InvalidApiKeyException("API Key: " + apiKey + " is invalid"); } } catch (Exception ex) { _logger.Debug(ex, ex.Message); _logger.Warn("Invalid API Key: {0}", apiKey); throw new InvalidApiKeyException("API Key: " + apiKey + " is invalid"); } }
public virtual bool Verify(string apiKey) { try { var verificationRequest = new Verification(); verificationRequest.ApiKey = apiKey; var client = new ProwlClient(); Logger.Trace("Verifying API Key: {0}", apiKey); var verificationResult = client.SendVerification(verificationRequest); if (String.IsNullOrWhiteSpace(verificationResult.ErrorMessage) && verificationResult.ResultCode == "200") { return(true); } } catch (Exception ex) { Logger.TraceException(ex.Message, ex); Logger.Warn("Invalid API Key: {0}", apiKey); } return(false); }
public void Verification_apikey_wrong_length_throws_validation_ArgumentException() { var fakeHttpInterface = new FakeHttpInterface(); var prowlClient = new ProwlClient(fakeHttpInterface); var verification = new Verification { ApiKey = "12345678678901234567890" }; Assert.Throws(typeof(ArgumentException), delegate { prowlClient.SendVerification(verification); }); }
public void SendVerification_should_call_SendVerification_on_HttpInterface() { var fakeHttpInterface = new FakeHttpInterface(); var prowlClient = new ProwlClient(fakeHttpInterface); var verification = new Verification { ApiKey = "1234567890123456789012345678901234567890", ProviderKey = "ewrwe" }; prowlClient.SendVerification(verification); Assert.True(fakeHttpInterface.SendVerificationCalled); }
private void Run(string[] args) { ArgumentsObject arguments = Configuration.Configure <ArgumentsObject>().CreateAndBind(args); if (arguments.Help || args.Length == 0) { ShowHelp(); return; } var prowlClient = new ProwlClient(); if (arguments.RetrieveToken) { RetrieveToken retrieveToken = new RetrieveToken(); retrieveToken.ProviderKey = arguments.ProviderKey; RetrieveTokenResult result = prowlClient.RetreiveToken(retrieveToken); System.Console.WriteLine("Token retreived\nToken: {0}\nUrl: {1}", result.Token, result.Url); return; } if (arguments.NewKey) { if (string.IsNullOrEmpty(arguments.Token) || string.IsNullOrEmpty(arguments.ProviderKey)) { System.Console.WriteLine("ProviderKey and Token required for this operation."); return; } RetrieveApikey retrieveApikey = new RetrieveApikey(); retrieveApikey.Token = arguments.Token; retrieveApikey.ProviderKey = arguments.ProviderKey; RetrieveApikeyResult retrieveApikeyResult = prowlClient.RetrieveApikey(retrieveApikey); System.Console.WriteLine("New APIKEY: {0}"); return; } if (string.IsNullOrEmpty(arguments.Key)) { System.Console.WriteLine("ApiKey requried"); return; } if (arguments.Verify) { IVerification v = new Verification(); v.ApiKey = arguments.Key; v.ProviderKey = arguments.ProviderKey; System.Console.WriteLine("Sending verification..."); VerificationResult verificationResult = prowlClient.SendVerification(v); System.Console.WriteLine("Verification {3}\n\tVerification returned: {0} \n\tNumber of messages left to send: {1}\n\tReset UNIX timestamp: {2}", verificationResult.ResultCode, verificationResult.RemainingMessageCount.ToString(), verificationResult.TimeStamp, verificationResult.ResultCode == "200" ? "OK" : "NOT OK"); } else { if (string.IsNullOrEmpty(arguments.Event)) { System.Console.WriteLine("Event is required"); return; } if (string.IsNullOrEmpty(arguments.Application)) { System.Console.WriteLine("Application is required"); return; } var notification = new Notification { Application = arguments.Application, Description = arguments.Description, Event = arguments.Event, Url = arguments.Url }; switch (arguments.Priority.ToLower()) { case "verylow": notification.Priority = NotificationPriority.VeryLow; break; case "moderate": notification.Priority = NotificationPriority.Moderate; break; case "high": notification.Priority = NotificationPriority.High; break; case "emergency": notification.Priority = NotificationPriority.Emergency; break; default: notification.Priority = NotificationPriority.Normal; break; } foreach (string s in arguments.Key.Split(new[] { ',', ';' })) { notification.AddApiKey(s); } NotificationResult notificationResult = prowlClient.SendNotification(notification); System.Console.WriteLine("Remaing number of messages: {0}", notificationResult.RemainingMessageCount.ToString()); } }
private void Run(string[] args) { ArgumentsObject arguments = Configuration.Configure<ArgumentsObject>().CreateAndBind(args); if(arguments.Help || args.Length == 0) { ShowHelp(); return; } var prowlClient = new ProwlClient(); if (arguments.RetrieveToken) { RetrieveToken retrieveToken = new RetrieveToken(); retrieveToken.ProviderKey = arguments.ProviderKey; RetrieveTokenResult result = prowlClient.RetreiveToken(retrieveToken); System.Console.WriteLine("Token retreived\nToken: {0}\nUrl: {1}", result.Token, result.Url); return; } if (arguments.NewKey) { if(string.IsNullOrEmpty(arguments.Token) || string.IsNullOrEmpty(arguments.ProviderKey)) { System.Console.WriteLine("ProviderKey and Token required for this operation."); return; } RetrieveApikey retrieveApikey = new RetrieveApikey(); retrieveApikey.Token = arguments.Token; retrieveApikey.ProviderKey = arguments.ProviderKey; RetrieveApikeyResult retrieveApikeyResult = prowlClient.RetrieveApikey(retrieveApikey); System.Console.WriteLine("New APIKEY: {0}"); return; } if (string.IsNullOrEmpty(arguments.Key)) { System.Console.WriteLine("ApiKey requried"); return; } if(arguments.Verify) { IVerification v = new Verification(); v.ApiKey = arguments.Key; v.ProviderKey = arguments.ProviderKey; System.Console.WriteLine("Sending verification..."); VerificationResult verificationResult = prowlClient.SendVerification(v); System.Console.WriteLine("Verification {3}\n\tVerification returned: {0} \n\tNumber of messages left to send: {1}\n\tReset UNIX timestamp: {2}", verificationResult.ResultCode, verificationResult.RemainingMessageCount.ToString(), verificationResult.TimeStamp, verificationResult.ResultCode == "200" ? "OK" : "NOT OK"); } else { if (string.IsNullOrEmpty(arguments.Event)) { System.Console.WriteLine("Event is required"); return; } if (string.IsNullOrEmpty(arguments.Application)) { System.Console.WriteLine("Application is required"); return; } var notification = new Notification { Application = arguments.Application, Description = arguments.Description, Event = arguments.Event, Url = arguments.Url }; switch (arguments.Priority.ToLower()) { case "verylow": notification.Priority = NotificationPriority.VeryLow; break; case "moderate": notification.Priority = NotificationPriority.Moderate; break; case "high": notification.Priority = NotificationPriority.High; break; case "emergency": notification.Priority = NotificationPriority.Emergency; break; default: notification.Priority = NotificationPriority.Normal; break; } foreach (string s in arguments.Key.Split(new[] { ',', ';' })) { notification.AddApiKey(s); } NotificationResult notificationResult= prowlClient.SendNotification(notification); System.Console.WriteLine("Remaing number of messages: {0}", notificationResult.RemainingMessageCount.ToString()); } }