Exemplo n.º 1
0
        public static bool Save(PushApiEntity entity)
        {
            bool result;

            try
            {
                lock (locker)
                {
                    if (!File.Exists(DatabaseFile))
                    {
                        if (!Directory.Exists(DatabaseDirectory))
                        {
                            Directory.CreateDirectory(DatabaseDirectory);
                        }
                        CreateDatabase();
                    }
                    using (var connection = CreateConnection())
                    {
                        connection.Open();
                        var query = @"INSERT INTO PushApi(Timestamp, ServerId, Request, Response, HttpStatusCode) VALUES(@Timestamp, @ServerId, @Request, @Response, @HttpStatusCode); SELECT last_insert_rowid();";
                        entity.Id = connection.Query <long>(query, entity).First();
                    }
                }

                result = true;
            }
            catch (Exception ex)
            {
                result = false;
            }
            return(result);
        }
Exemplo n.º 2
0
        public async Task <bool> PostData(string messageBody, int httpTimeout = 30)
        {
            DateTime timeStamp = DateTime.UtcNow;
            bool     result    = false;

            if (messageBody != null)
            {
                try
                {
                    var accessKey = server.AccessKey;
                    var secretKey = server.SecretKey;
                    var auth      = new BasicAWSCredentials(accessKey, secretKey);
                    var sqsConfig = new AmazonSQSConfig();
                    //sqsConfig.ServiceURL = "https://sqs.ap-southeast-1.amazonaws.com";
                    sqsConfig.RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(server.Region);

                    using (var client = new AmazonSQSClient(auth, sqsConfig))
                    {
                        var request = new SendMessageRequest
                        {
                            DelaySeconds = (int)TimeSpan.FromSeconds(1).TotalSeconds,
                            MessageBody  = messageBody, // make it stringify
                            QueueUrl     = server.EndPointUri
                        };

                        var response = await client.SendMessageAsync(request);

                        var content = JsonConvert.SerializeObject(response);

                        if (response.HttpStatusCode == HttpStatusCode.OK)
                        {
                            result = true;
                        }
                        else
                        {
                            result = false;
                        }

                        if (!result)
                        {
                            if (LogPacketOnFailure)
                            {
                                PushApiEntity entity = new PushApiEntity
                                {
                                    Timestamp      = DateTimeHelper.CurrentUniversalTime,
                                    Request        = messageBody,
                                    ServerId       = server.Id,
                                    Response       = content,
                                    HttpStatusCode = response.HttpStatusCode
                                };
                                //PushApiRepository.Save(entity);
                                FailedPacketLogger.Instance.Log.Write(JsonConvert.SerializeObject(entity));
                            }
                        }
                        else
                        {
                            if (LogPacketOnSuccess)
                            {
                                PushApiEntity entity = new PushApiEntity
                                {
                                    Timestamp      = DateTimeHelper.CurrentUniversalTime,
                                    Request        = messageBody,
                                    ServerId       = server.Id,
                                    Response       = content,
                                    HttpStatusCode = response.HttpStatusCode
                                };
                                //PushApiRepository.Save(entity);
                                Logger.Instance.Log.Write(JsonConvert.SerializeObject(entity));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    result = false;
                    if (LogPacketOnFailure)
                    {
                        PushApiEntity entity = new PushApiEntity
                        {
                            Timestamp      = DateTimeHelper.CurrentUniversalTime,
                            Request        = messageBody,
                            ServerId       = server.Id,
                            HttpStatusCode = HttpStatusCode.Unused,
                            Response       = "[ERROR_AT_GATEWAY] => " + ex.Message
                        };

                        FailedPacketLogger.Instance.Log.Write(JsonConvert.SerializeObject(entity));
                    }
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        public async Task <bool> PostData(string messageBody, int httpTimeout = 30)
        {
            DateTime timeStamp = DateTime.UtcNow;
            bool     result    = false;

            if (messageBody != null)
            {
                try
                {
                    string messageBodyHash = ComputeSha256Hash(messageBody);

                    Uri        targetUri  = new Uri(server.EndPointUri);
                    HttpMethod httpMethod = HttpMethod.Post;

                    HttpContent httpContent = new StringContent(messageBody);
                    httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

                    HttpRequestMessage httpRequestMessage = new HttpRequestMessage(httpMethod, targetUri);
                    httpRequestMessage.Content = httpContent;
                    httpRequestMessage.Headers.Add("x-api-key", server.XApiKey);
                    httpRequestMessage.Headers.Add("X-Amz-Content-Sha256", messageBodyHash);
                    httpRequestMessage.Headers.Add("X-Amz-Date", timeStamp.ToString("yyyyMMddTHHmmssZ"));
                    httpRequestMessage.Headers.Host = targetUri.Host;

                    string signature           = GetSignature(httpRequestMessage, server.SecretKey, server.Region, server.Service, timeStamp);
                    string authorizationHeader = GetAuthorizationHeader(server.AccessKey, timeStamp, server.Region, server.Service, signature);

                    using (HttpClient httpClient = new HttpClient())
                    {
                        httpClient.Timeout = TimeSpan.FromSeconds(httpTimeout);
                        httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("AWS4-HMAC-SHA256", authorizationHeader);

                        HttpResponseMessage msg = await httpClient.SendAsync(httpRequestMessage);

                        string content = msg.Content.ReadAsStringAsync().Result;

                        if (msg.StatusCode == HttpStatusCode.OK)
                        {
                            result = true;
                        }
                        else
                        {
                            result = false;
                        }

                        if (!result)
                        {
                            if (LogPacketOnFailure)
                            {
                                PushApiEntity entity = new PushApiEntity
                                {
                                    Timestamp      = DateTimeHelper.CurrentUniversalTime,
                                    Request        = messageBody,
                                    ServerId       = server.Id,
                                    Response       = content,
                                    HttpStatusCode = msg.StatusCode
                                };
                                //PushApiRepository.Save(entity);
                                FailedPacketLogger.Instance.Log.Write(JsonConvert.SerializeObject(entity));
                            }
                        }
                        else
                        {
                            if (LogPacketOnSuccess)
                            {
                                PushApiEntity entity = new PushApiEntity
                                {
                                    Timestamp      = DateTimeHelper.CurrentUniversalTime,
                                    Request        = messageBody,
                                    ServerId       = server.Id,
                                    Response       = content,
                                    HttpStatusCode = msg.StatusCode
                                };
                                //PushApiRepository.Save(entity);
                                Logger.Instance.Log.Write(JsonConvert.SerializeObject(entity));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    result = false;
                    if (LogPacketOnFailure)
                    {
                        PushApiEntity entity = new PushApiEntity
                        {
                            Timestamp      = DateTimeHelper.CurrentUniversalTime,
                            Request        = messageBody,
                            ServerId       = server.Id,
                            HttpStatusCode = HttpStatusCode.Unused,
                            Response       = "[ERROR_AT_GATEWAY] => " + ex.Message
                        };

                        FailedPacketLogger.Instance.Log.Write(JsonConvert.SerializeObject(entity));
                    }
                }
            }

            return(result);
        }