public virtual GenericResponse ValidateUrls() { if (string.IsNullOrEmpty(SelfieUrl)) { return(GenericResponse.ErrorMessage("Selfie Url is not set")); } var selfieResult = Uri.IsWellFormedUriString(SelfieUrl, UriKind.Absolute); if (!selfieResult) { return(GenericResponse.ErrorMessage("Invalid Selfie Url format")); } if (AutomaticCaptureUrls == null || AutomaticCaptureUrls.Count == 0) { return(GenericResponse.ErrorMessage("Automatic Capture Urls not set")); } bool result; foreach (var url in AutomaticCaptureUrls) { result = Uri.IsWellFormedUriString(url, UriKind.Absolute); if (!result) { return(GenericResponse.ErrorMessage("Invalid Automatic Capture Urls format")); } } if (AutomaticCaptureUrls.Count > 5) { return(GenericResponse.ErrorMessage("Error: more than five automatic capture urls")); } return(GenericResponse.Successful()); }
private GenericResponse ValidateSelfie() { if (string.IsNullOrEmpty(Selfie)) { return(GenericResponse.ErrorMessage("Selfie is not set")); } var result = Regex.IsMatch(Selfie, @"data:image/(?<type>.+?),(?<data>.+)"); return(result ? GenericResponse.Successful() : GenericResponse.ErrorMessage("Invalid selfie format")); }
public virtual GenericResponse Validate() { if (string.IsNullOrEmpty(Name)) { return(GenericResponse.ErrorMessage("Name is not set")); } if (string.IsNullOrEmpty(Email)) { return(GenericResponse.ErrorMessage("Email is not set")); } if (!IsValidEmail(Email)) { return(GenericResponse.ErrorMessage("Invalid email format")); } return(GenericResponse.Successful()); }
public GenericResponse AddUser(UserModel user) { database = databaseFactory.GetDatabase(); database.BeginTransaction(); var selfieResult = database.DoQuery(new DbQuery { Type = DbQueryType.Storage, Query = user.Selfie, Path = Guid.NewGuid().ToString() + user.Name.Replace(" ", "-") }); if (!selfieResult.Success) { return(GenericResponse.ErrorMessage(selfieResult.Message)); } user.SelfieUrl = selfieResult.Result; user.AutomaticCaptureUrls = new List <string>(); foreach (var automaticCapture in user.AutomaticCaptures) { selfieResult = database.DoQuery(new DbQuery { Type = DbQueryType.Storage, Query = automaticCapture, Path = Guid.NewGuid().ToString() + user.Name.Replace(" ", "-") }); if (!selfieResult.Success) { return(GenericResponse.ErrorMessage(selfieResult.Message)); } user.AutomaticCaptureUrls.Add(selfieResult.Result); } var result = database.DoQuery(new DbQuery { Type = DbQueryType.Add, Query = JsonConvert.SerializeObject(user) }); return(result.Success ? GenericResponse.Successful() : GenericResponse.ErrorMessage(result.Message)); }
private GenericResponse ValidateAutomaticCaptures() { if (AutomaticCaptures == null || AutomaticCaptures.Count == 0) { return(GenericResponse.ErrorMessage("Automatic captures not set")); } bool result; foreach (var capture in AutomaticCaptures) { result = Regex.IsMatch(capture, @"data:image/(?<type>.+?),(?<data>.+)"); if (!result) { return(GenericResponse.ErrorMessage("Invalid automatic captures")); } } if (AutomaticCaptures.Count > 5) { return(GenericResponse.ErrorMessage("Error: more than five captures received")); } return(GenericResponse.Successful()); }
public override GenericResponse Validate() { var firstReview = base.Validate(); if (!firstReview.Success) { return(firstReview); } var secondReview = ValidateSelfie(); if (!secondReview.Success) { return(secondReview); } var thirdReview = ValidateAutomaticCaptures(); if (!thirdReview.Success) { return(thirdReview); } return(GenericResponse.Successful()); }
private static GenericResponse <string> InsertStorage(DbQuery query) { var base64Data = Regex.Match(query.Query, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value; var binData = Convert.FromBase64String(base64Data); string url = ""; using (var stream = new MemoryStream(binData)) { storage = new FirebaseStorage(ConfigurationHelper.FirebaseStorageUrl) .Child("user") .Child($"{query.Path}.png") .PutAsync(stream); storage.Progress.ProgressChanged += _eventHandler; url = Task.Run(async() => { var downloadUrl = await storage; return(downloadUrl); }).Result; } return(GenericResponse <string> .Successful(url)); }
public static GenericResponse <string> Execute(DbQuery query) { switch (query.Type) { case DbQueryType.Get: var getResult = Task.Run(() => RequestHelper <List <User> > .GetStringRequest(FirebaseDatabaseEndpoint.Users)).Result; return(getResult); case DbQueryType.Add: var addResult = Task.Run(() => RequestHelper <User> .PostRequest(JsonConvert.DeserializeObject <User>(query.Query), FirebaseDatabaseEndpoint.Users)).Result; return(GenericResponse <string> .Successful(JsonConvert.SerializeObject(addResult))); case DbQueryType.Update: var updateResult = Task.Run(() => RequestHelper <User> .PutRequest(JsonConvert.DeserializeObject <User>(query.Query), FirebaseDatabaseEndpoint.Users)).Result; return(GenericResponse <string> .Successful(JsonConvert.SerializeObject(updateResult))); case DbQueryType.Delete: var deleteResult = Task.Run(() => RequestHelper <User> .DeleteRequest(FirebaseDatabaseEndpoint.Users)).Result; return(GenericResponse <string> .Successful(JsonConvert.SerializeObject(deleteResult))); default: return(GenericResponse <string> .ErrorMessage("Database query type not implemented.")); } }
private static async Task <GenericResponse <TR> > BaseRequest <T>(string url, HttpMethodEnum method, T payload) { try { var httpHandler = new HttpClientHandler(); httpHandler.ServerCertificateCustomValidationCallback += (message, cert, chain, errors) => true; var client = new HttpClient(httpHandler); StringContent content = null; if (_hasContent) { client.DefaultRequestHeaders.ConnectionClose = true; content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json"); } Console.WriteLine($"Requesting [{url}]"); HttpResponseMessage response; switch (method) { case HttpMethodEnum.Get: response = await client.GetAsync(url); break; case HttpMethodEnum.Post: response = await client.PostAsync(url, content); break; case HttpMethodEnum.Put: response = await client.PutAsync(url, content); break; case HttpMethodEnum.Delete: response = await client.DeleteAsync(url); break; default: client?.Dispose(); client = null; content?.Dispose(); content = null; return(GenericResponse <TR> .ErrorMessage($"{url} Unknown http method")); } client?.Dispose(); client = null; content?.Dispose(); content = null; if (response.IsSuccessStatusCode) { string responseContent = await response.Content.ReadAsStringAsync(); if (_shouldNotParseJson) { response?.Dispose(); response = null; _unparsedResponse = GenericResponse <string> .Successful(responseContent); return(null); } if (responseContent.Contains("GenericResponse")) { var resultWeappedObject = JsonConvert.DeserializeObject <GenericResponse <TR> >(responseContent); response?.Dispose(); response = null; return(resultWeappedObject); } else { var resultObject = JsonConvert.DeserializeObject <TR>(responseContent); response?.Dispose(); response = null; return(GenericResponse <TR> .Successful(resultObject)); } } else { var errorMessage = $"{url} ({response.ReasonPhrase})"; response?.Dispose(); response = null; return(GenericResponse <TR> .ErrorMessage(errorMessage)); } } catch (Exception ex) { return(GenericResponse <TR> .ErrorMessage($"{url} ({ex})")); } }