Ejemplo n.º 1
0
        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());
        }
Ejemplo n.º 2
0
        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 static GenericResponse <string> Execute(DbQuery query, EventHandler <FirebaseStorageProgress> eventHandler)
 {
     try
     {
         _eventHandler = eventHandler;
         return(InsertStorage(query));
     }
     catch (Exception ex)
     {
         return(GenericResponse <string> .ErrorMessage(ex.Message));
     }
 }
Ejemplo n.º 4
0
 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());
 }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
        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 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."));
            }
        }
Ejemplo n.º 8
0
        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})"));
            }
        }