Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Yurumi.Configurations.SendGridConfiguration"/> class.
        /// </summary>
        /// <param name="category">Category.</param>
        /// <param name="enableClickTracking">If set to <c>true</c> enable click tracking.</param>
        public SendGridConfiguration(string category, bool enableClickTracking)
        {
            var token  = new Token(category, new Filters(new ClickTrack(new Settings(enableClickTracking))));
            var output = JsonConvert.SerializeObject(token, new BoolConverter());

            HeaderValues.Add(_header, output);
        }
Exemplo n.º 2
0
        /// /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="tag">Tag name.</param>
        /// <param name="enableClickTracking">Enable click tracking.</param>
        /// <param name="variables">Object to be serialized as a collection of variables.</param>
        public MailGun(string tag, bool enableClickTracking, object variables = null)
        {
            HeaderValues.Add(_headerTag, tag);
            HeaderValues.Add(_headerTrack, enableClickTracking ? "yes" : "no");

            if (variables != null)
            {
                var output = JsonConvert.SerializeObject(variables, Formatting.None,
                                                         new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });

                HeaderValues.Add(_headerVariables, output);
            }
        }
Exemplo n.º 3
0
        void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            if (!this.isBusy)
            {
                return;
            }
            if (!this.IgnoreExpiresHeader)
            {
                // Since Expires is a static guess by the server, use Expires only when the server did
                // not send back any Etag or LastModifiedTime, which makes conditional GET impossible
                if (this.etag == null && this.lastModifiedTime == null && this.expires != null && (DateTime.UtcNow < this.expires.Value.ToUniversalTime()))
                {
                    return;
                }
            }
            this.expires = null;
            HttpRequestMessage request = new HttpRequestMessage("GET", this.uri);
            if (this.etag != null)
            {
                var ifNoneMatch = new HeaderValues<EntityTag>();
                ifNoneMatch.Add(this.etag);
                request.Headers.IfNoneMatch = ifNoneMatch;
            }
            request.Headers.IfModifiedSince = this.lastModifiedTime;
            bool stopTimer = false;
            try
            {
                HttpResponseMessage response = null;
                
                try
                {
                    response = this.HttpClient.Send(request);
                }
                catch (Exception ex)
                {
                    if (!this.IgnoreSendErrors)
                    {
                        stopTimer = InvokeHandler(ex);
                    }
                    if (response != null)
                    {
                        response.Dispose();
                    }
                    return;
                }

                using (response)
                {
                    switch (response.StatusCode)
                    {
                        case HttpStatusCode.NotModified:
                            // the resource has not been modified
                            response.Dispose();
                            break;
                        case HttpStatusCode.OK:
                            // the resource has been modified. Fire the event, along with the response message
                            this.etag = response.Headers.ETag;
                            this.expires = response.Headers.Expires;
                            this.lastModifiedTime = response.Headers.LastModified;
                            try
                            {
                                stopTimer = InvokeHandler(response);
                            }
                            finally
                            {
                                response.Dispose();
                            }
                            break;
                        default:
                            // this is an unexpected error. Fire the event, if errors are not to be suppressed
                            try
                            {
                                if (!this.IgnoreNonOKStatusCodes)
                                {
                                    stopTimer = InvokeHandler(response);
                                }
                            }
                            finally
                            {
                                response.Dispose();
                            }
                            break;
                    }
                }
            }
            finally
            {
                if (stopTimer)
                {
                    StopPolling();
                }
            }
        }
        void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            if (!this.isBusy)
            {
                return;
            }
            if (!this.IgnoreExpiresHeader)
            {
                // Since Expires is a static guess by the server, use Expires only when the server did
                // not send back any Etag or LastModifiedTime, which makes conditional GET impossible
                if (this.etag == null && this.lastModifiedTime == null && this.expires != null && (DateTime.UtcNow < this.expires.Value.ToUniversalTime()))
                {
                    return;
                }
            }
            this.expires = null;
            HttpRequestMessage request = new HttpRequestMessage("GET", this.uri);

            if (this.etag != null)
            {
                var ifNoneMatch = new HeaderValues <EntityTag>();
                ifNoneMatch.Add(this.etag);
                request.Headers.IfNoneMatch = ifNoneMatch;
            }
            request.Headers.IfModifiedSince = this.lastModifiedTime;
            bool stopTimer = false;

            try
            {
                HttpResponseMessage response = null;

                try
                {
                    response = this.HttpClient.Send(request);
                }
                catch (Exception ex)
                {
                    if (!this.IgnoreSendErrors)
                    {
                        stopTimer = InvokeHandler(ex);
                    }
                    if (response != null)
                    {
                        response.Dispose();
                    }
                    return;
                }

                using (response)
                {
                    switch (response.StatusCode)
                    {
                    case HttpStatusCode.NotModified:
                        // the resource has not been modified
                        response.Dispose();
                        break;

                    case HttpStatusCode.OK:
                        // the resource has been modified. Fire the event, along with the response message
                        this.etag             = response.Headers.ETag;
                        this.expires          = response.Headers.Expires;
                        this.lastModifiedTime = response.Headers.LastModified;
                        try
                        {
                            stopTimer = InvokeHandler(response);
                        }
                        finally
                        {
                            response.Dispose();
                        }
                        break;

                    default:
                        // this is an unexpected error. Fire the event, if errors are not to be suppressed
                        try
                        {
                            if (!this.IgnoreNonOKStatusCodes)
                            {
                                stopTimer = InvokeHandler(response);
                            }
                        }
                        finally
                        {
                            response.Dispose();
                        }
                        break;
                    }
                }
            }
            finally
            {
                if (stopTimer)
                {
                    StopPolling();
                }
            }
        }