Exemple #1
0
        /// <summary>
        /// Adds the right ETag action (e.g. If-Match) header to the given HTTP request if the body contains ETag.
        /// </summary>
        private void AddETag(HttpRequestMessage request)
        {
            IDirectResponseSchema body = GetBody() as IDirectResponseSchema;

            if (body != null && !string.IsNullOrEmpty(body.ETag))
            {
                var        etag   = body.ETag;
                ETagAction action = ETagAction == ETagAction.Default ? GetDefaultETagAction(HttpMethod) : ETagAction;
                try
                {
                    switch (action)
                    {
                    case ETagAction.IfMatch:
                        request.Headers.IfMatch.Add(new EntityTagHeaderValue(etag));
                        break;

                    case ETagAction.IfNoneMatch:
                        request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(etag));
                        break;
                    }
                }
                // When ETag is invalid we are going to create a request anyway.
                // See https://code.google.com/p/google-api-dotnet-client/issues/detail?id=464 for more details.
                catch (FormatException ex)
                {
                    Logger.Error(ex, "Can't set {0}. Etag is: {1}.", action, etag);
                }
            }
        }
Exemple #2
0
        private static void RunSample(TasksService service, bool modify, ETagAction behaviour)
        {
            CommandLine.WriteAction("Testing for E-Tag case " + behaviour + " with modified=" + modify + "...");

            // Create a new task list.
            TaskList list = service.Tasklists.Insert(new TaskList()
            {
                Title = "E-Tag Collision Test"
            }).Fetch();

            // Add a task
            Task myTask = service.Tasks.Insert(new Task()
            {
                Title = "My Task"
            }, list.Id).Fetch();

            // Retrieve a second instance of this task, modify it and commit it
            if (modify)
            {
                Task myTaskB = service.Tasks.Get(list.Id, myTask.Id).Fetch();
                myTaskB.Title = "My Task B!";
                service.Tasks.Update(myTaskB, list.Id, myTaskB.Id).Fetch();
            }

            // Modfiy the original task, and see what happens
            myTask.Title = "My Task A!";
            var request = service.Tasks.Update(myTask, list.Id, myTask.Id);

            request.ETagAction = behaviour;

            try
            {
                request.Fetch();
                CommandLine.WriteResult("Result", "Success!");
            }
            catch (GoogleApiRequestException ex)
            {
                CommandLine.WriteResult(
                    "Result", "Failure! (" + ex.RequestError.Code + " - " + ex.RequestError.Message + ")");
            }
            finally
            {
                // Delete the created list.
                service.Tasklists.Delete(list.Id).Fetch();
                CommandLine.WriteLine();
            }
        }
        private static void RunSample(TasksService service, bool modify, ETagAction behaviour)
        {
            Console.WriteLine("Testing for E-Tag case " + behaviour + " with modified=" + modify + "...");

            // Create a new task list.
            TaskList list = service.Tasklists.Insert(new TaskList()
            {
                Title = "E-Tag Collision Test"
            }).Execute();

            // Add a task.
            Task myTask = service.Tasks.Insert(new Task()
            {
                Title = "My Task"
            }, list.Id).Execute();

            // Retrieve a second instance of this task, modify it and commit it.
            if (modify)
            {
                Task myTaskB = service.Tasks.Get(list.Id, myTask.Id).Execute();
                myTaskB.Title = "My Task B!";
                service.Tasks.Update(myTaskB, list.Id, myTaskB.Id).Execute();
            }

            // Modify the original task, and see what happens.
            myTask.Title = "My Task A!";
            var request = service.Tasks.Update(myTask, list.Id, myTask.Id);

            request.ETagAction = behaviour;

            try
            {
                request.Execute();
                Console.WriteLine("\tResult: Success!");
            }
            catch (GoogleApiException ex)
            {
                Console.WriteLine("\tResult: Failure! The error message is: " + ex.Message);
            }
            finally
            {
                // Delete the created list.
                service.Tasklists.Delete(list.Id).Execute();
                Console.WriteLine();
            }
        }
Exemple #4
0
        /// <summary>
        /// Adds the right ETag action (e.g. If-Match) header to the given HTTP request if the body contains ETag.
        /// </summary>
        private void AddETag(HttpRequestMessage request)
        {
            IDirectResponseSchema body = GetBody() as IDirectResponseSchema;

            if (body != null && !string.IsNullOrEmpty(body.ETag))
            {
                ETagAction action = ETagAction == ETagAction.Default ? GetDefaultETagAction(HttpMethod) : ETagAction;
                switch (action)
                {
                case ETagAction.IfMatch:
                    request.Headers.IfMatch.Add(new EntityTagHeaderValue(body.ETag));
                    break;

                case ETagAction.IfNoneMatch:
                    request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(body.ETag));
                    break;
                }
            }
        }
        private static void RunSample(TasksService service, bool modify, ETagAction behaviour)
        {
            Console.WriteLine("Testing for E-Tag case " + behaviour + " with modified=" + modify + "...");

            // Create a new task list.
            TaskList list = service.Tasklists.Insert(new TaskList() { Title = "E-Tag Collision Test" }).Execute();

            // Add a task.
            Task myTask = service.Tasks.Insert(new Task() { Title = "My Task" }, list.Id).Execute();

            // Retrieve a second instance of this task, modify it and commit it.
            if (modify)
            {
                Task myTaskB = service.Tasks.Get(list.Id, myTask.Id).Execute();
                myTaskB.Title = "My Task B!";
                service.Tasks.Update(myTaskB, list.Id, myTaskB.Id).Execute();
            }

            // Modify the original task, and see what happens.
            myTask.Title = "My Task A!";
            var request = service.Tasks.Update(myTask, list.Id, myTask.Id);
            request.ETagAction = behaviour;

            try
            {
                request.Execute();
                Console.WriteLine("\tResult: Success!");
            }
            catch (GoogleApiException ex)
            {
                Console.WriteLine("\tResult: Failure! The error message is: " + ex.Message);
            }
            finally
            {
                // Delete the created list. 
                service.Tasklists.Delete(list.Id).Execute();
                Console.WriteLine();
            }
        }
Exemple #6
0
 /// <summary>
 /// Sets the ETag-behavior of this request.
 /// </summary>
 public IRequest WithETagAction(ETagAction action)
 {
     ETagAction = action;
     return this;
 }
Exemple #7
0
        internal WebRequest CreateWebRequest(Action <WebRequest> onRequestReady)
        {
            HttpWebRequest request = BuildRequest();

            // Create the request.
            Authenticator.ApplyAuthenticationToRequest(request);

            // Insert the content type and user agent.
            request.ContentType = string.Format(
                "{0}; charset={1}", GetReturnMimeType(ReturnType), ContentCharset.WebName);
            string appName     = FormatForUserAgent(ApplicationName);
            string apiVersion  = FormatForUserAgent(ApiVersion);
            string platform    = FormatForUserAgent(Environment.OSVersion.Platform.ToString());
            string platformVer = FormatForUserAgent(Environment.OSVersion.Version.ToString());

            // The UserAgent header can only be set on a non-Silverlight platform.
            // Silverlight uses the user agent of the browser instead.
#if !SILVERLIGHT
            request.UserAgent = String.Format(UserAgent, appName, apiVersion, platform, platformVer);
#endif

            // Add the E-tag header:
            if (!string.IsNullOrEmpty(ETag))
            {
                ETagAction action = this.ETagAction;
                if (action == ETagAction.Default)
                {
                    action = GetDefaultETagAction(request.Method);
                }

                switch (action)
                {
                case ETagAction.IfMatch:
                    request.Headers[HttpRequestHeader.IfMatch] = ETag;
                    break;

                case ETagAction.IfNoneMatch:
                    request.Headers[HttpRequestHeader.IfNoneMatch] = ETag;
                    break;
                }
            }

            // Check if compression is supported.
#if !SILVERLIGHT
            if (Service.GZipEnabled)
            {
                request.UserAgent += GZipUserAgentSuffix;
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            }
#endif

            // Attach a body if a POST and there is something to attach.
            if (HttpMethodHasBody(request.Method))
            {
                if (!string.IsNullOrEmpty(Body))
                {
                    AttachBody(request, onRequestReady);
                    return(request);
                }
                else
                {
                    // Set the "Content-Length" header, which is required for every http method declaring a body. This
                    // is required as e.g. the google servers will throw a "411 - Length required" error otherwise.
#if !SILVERLIGHT
                    request.ContentLength = 0;
#else
                    // Set by the browser on Silverlight. Cannot be modified by the user.
#endif
                }
            }

            onRequestReady(request);
            return(request);
        }
Exemple #8
0
 /// <summary>
 /// Sets the ETag-behavior of this request.
 /// </summary>
 public IRequest WithETagAction(ETagAction action)
 {
     ETagAction = action;
     return(this);
 }