示例#1
0
 /// <summary>
 /// Pin a topic
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='request'>
 /// Post pin request
 /// </param>
 /// <param name='authorization'>
 /// Format is: "Scheme CredentialsList". Possible values are:
 ///
 /// - Anon AK=AppKey
 ///
 /// - SocialPlus TK=SessionToken
 ///
 /// - Facebook AK=AppKey|TK=AccessToken
 ///
 /// - Google AK=AppKey|TK=AccessToken
 ///
 /// - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken
 ///
 /// - Microsoft AK=AppKey|TK=AccessToken
 ///
 /// - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <object> PostPinAsync(this IMyPins operations, PostPinRequest request, string authorization, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.PostPinWithHttpMessagesAsync(request, authorization, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
        public async Task <IHttpActionResult> PostPin([FromBody] PostPinRequest request)
        {
            string className  = "MyPinsController";
            string methodName = "PostPin";
            string logEntry   = $"PinnedTopicHandle = {request?.TopicHandle}";

            this.LogControllerStart(this.log, className, methodName, logEntry);

            var topicEntity = await this.topicsManager.ReadTopic(request.TopicHandle);

            if (topicEntity == null)
            {
                return(this.NotFound(ResponseStrings.TopicNotFound));
            }

            var pinLookupEntity = await this.pinsManager.ReadPin(this.UserHandle, request.TopicHandle);

            DateTime currentTime = DateTime.UtcNow;

            if (pinLookupEntity != null && pinLookupEntity.LastUpdatedTime > currentTime)
            {
                return(this.Conflict(ResponseStrings.NewerItemExists));
            }

            string pinHandle = this.handleGenerator.GenerateShortHandle();

            await this.pinsManager.UpdatePin(
                ProcessType.Frontend,
                pinHandle,
                this.UserHandle,
                request.TopicHandle,
                true,
                topicEntity.PublisherType,
                topicEntity.UserHandle,
                topicEntity.AppHandle,
                currentTime,
                pinLookupEntity);

            logEntry += $", PinHandle = {pinHandle}";
            this.LogControllerEnd(this.log, className, methodName, logEntry);
            return(this.NoContent());
        }
示例#3
0
        /// <summary>
        /// Helper routine that performs the main actions of the test.
        /// Create a topic.  Get the initial topic pin status, check that it is unpinned.  Pin it.  Check the pin status is true.
        /// Unpin it. Then check pin status is back to false.
        /// </summary>
        /// <param name="appPublished">flag to indicate if topic is app published</param>
        /// <param name="appHandle">app handle</param>
        /// <returns>Fail if an exception is hit</returns>
        public async Task PinUnPinDeletePinTestHelper(bool appPublished, string appHandle)
        {
            // Set up initial login etc
            SocialPlusClient client = new SocialPlusClient(TestConstants.ServerApiBaseUrl);

            string           firstName        = "J.J.";
            string           lastName         = "Z";
            string           bio              = string.Empty;
            PostUserResponse postUserResponse = await TestUtilities.DoLogin(client, firstName, lastName, bio);

            string auth       = AuthHelper.CreateSocialPlusAuth(postUserResponse.SessionToken);
            string userHandle = postUserResponse.UserHandle;

            if (appPublished)
            {
                // add user as admin
                bool added = ManageAppsUtils.AddAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                if (!added)
                {
                    // delete the user and fail the test
                    await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                    Assert.Fail("Failed to set user as administrator");
                }
            }

            string   topicTitle   = "Rarest coin";
            string   topicText    = "Egyptian coin";
            BlobType blobType     = BlobType.Image;
            string   blobHandle   = "http://myBlobHandle/";
            string   language     = "en-US";
            string   deepLink     = "coins:abcdef";
            string   categories   = "photo, ncurrency";
            string   friendlyName = "abcde";
            string   group        = "mygroup";
            string   topicHandle  = string.Empty;

            // step 1, create a topic
            var postTopicRequest = new PostTopicRequest()
            {
                Text = topicText, Title = topicTitle, BlobType = blobType, BlobHandle = blobHandle, Categories = categories, Language = language, DeepLink = deepLink, FriendlyName = friendlyName, Group = group
            };

            if (appPublished)
            {
                postTopicRequest.PublisherType = PublisherType.App;
            }
            else
            {
                postTopicRequest.PublisherType = PublisherType.User;
            }

            var postTopicOperationResponse = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest, authorization : auth);

            if (postTopicOperationResponse.Response.IsSuccessStatusCode)
            {
                topicHandle = postTopicOperationResponse.Body.TopicHandle;
            }
            else
            {
                // cleanup: delete the user
                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to post topic.");
            }

            bool?initialPinValue = null;
            bool?secondPinValue  = null;
            bool?finalPinValue   = null;

            // step 2, get the topic (and its pin status)
            var getTopicOperationResponse = await client.Topics.GetTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

            if (getTopicOperationResponse.Response.IsSuccessStatusCode)
            {
                initialPinValue = getTopicOperationResponse.Body.Pinned;
            }
            else
            {
                // cleanup: delete the topic and the user
                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to get topic.");
            }

            // step 3, pin topic
            PostPinRequest postPinRequest = new PostPinRequest()
            {
                TopicHandle = topicHandle
            };
            var postPinOperationResponse = await client.MyPins.PostPinWithHttpMessagesAsync(request : postPinRequest, authorization : auth);

            if (!postPinOperationResponse.Response.IsSuccessStatusCode)
            {
                // cleanup: delete the topic and the user
                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to pin topic.");
            }

            // step 4, get topic and its pin status again
            getTopicOperationResponse = await client.Topics.GetTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

            if (getTopicOperationResponse.Response.IsSuccessStatusCode)
            {
                secondPinValue = getTopicOperationResponse.Body.Pinned;
            }
            else
            {
                // cleanup: delete the topic and the user
                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to get topic.");
            }

            // step 5, Delete Pin
            var deletePinOperationResponse = await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

            if (!deletePinOperationResponse.Response.IsSuccessStatusCode)
            {
                // cleanup: delete the topic and the user
                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to delete pin.");
            }

            // step 6, get topic yet again, and check pin status to see that it is now back to false
            getTopicOperationResponse = await client.Topics.GetTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

            if (getTopicOperationResponse.Response.IsSuccessStatusCode)
            {
                finalPinValue = getTopicOperationResponse.Body.Pinned;
            }
            else
            {
                // cleanup: delete the topic and the user
                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to get pin.");
            }

            // cleanup: delete the topic and the user
            await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

            bool?adminDeleted = null;

            if (appPublished)
            {
                adminDeleted = ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
            }

            await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

            if (appPublished)
            {
                Assert.IsTrue(adminDeleted.HasValue);
                Assert.IsTrue(adminDeleted.Value);
            }

            Assert.IsTrue(initialPinValue.HasValue);
            Assert.IsFalse(initialPinValue.Value);
            Assert.IsTrue(secondPinValue.HasValue);
            Assert.IsTrue(secondPinValue.Value);
            Assert.IsTrue(finalPinValue.HasValue);
            Assert.IsFalse(finalPinValue.Value);
        }
示例#4
0
        /// <summary>
        /// Helper routine that performs the main actions of the test.
        /// Create multiple topics.  Pin them.  Then get the pin feed, and check that all the pinned topics show up.
        /// </summary>
        /// <param name="appPublished">flag to indicate if topic is app published</param>
        /// <param name="appHandle">app handle</param>
        /// <returns>Fail if an exception is hit</returns>
        public async Task GetPinFeedTestHelper(bool appPublished, string appHandle)
        {
            // Set up initial login etc
            SocialPlusClient client = new SocialPlusClient(TestConstants.ServerApiBaseUrl);

            string           firstName        = "J.J.";
            string           lastName         = "Z";
            string           bio              = string.Empty;
            PostUserResponse postUserResponse = await TestUtilities.DoLogin(client, firstName, lastName, bio);

            string auth       = AuthHelper.CreateSocialPlusAuth(postUserResponse.SessionToken);
            string userHandle = postUserResponse.UserHandle;

            if (appPublished)
            {
                // add user as admin
                bool added = ManageAppsUtils.AddAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                if (!added)
                {
                    // delete the user and fail the test
                    await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                    Assert.Fail("Failed to set user as administrator");
                }
            }

            // create topic #1
            string topicTitle = "topic number 1";
            string topicText  = "the jukebox needs to take a leak";
            string language   = "en-US";
            string deepLink   = "http://dummy/";
            string categories = "cat1, cat6";
            string group      = "mygroup";

            // step 1, create a topic and pin it
            string topicHandle      = string.Empty;
            var    postTopicRequest = new PostTopicRequest()
            {
                Text = topicText, Title = topicTitle, Categories = categories, Language = language, DeepLink = deepLink, Group = group
            };

            if (appPublished)
            {
                postTopicRequest.PublisherType = PublisherType.App;
            }
            else
            {
                postTopicRequest.PublisherType = PublisherType.User;
            }

            var postTopicOperationResponse = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest, authorization : auth);

            if (postTopicOperationResponse.Response.IsSuccessStatusCode)
            {
                topicHandle = postTopicOperationResponse.Body.TopicHandle;
            }
            else
            {
                // cleanup: delete the user
                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to post topic.");
            }

            var pinRequest = new PostPinRequest()
            {
                TopicHandle = topicHandle
            };
            var postPinOperationResponse = await client.MyPins.PostPinWithHttpMessagesAsync(authorization : auth, request : pinRequest);

            if (!postPinOperationResponse.Response.IsSuccessStatusCode)
            {
                // cleanup: delete topic #1 and delete the user
                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to pin topic #1.");
            }

            // create topic #2 and pin it
            topicTitle = "topic number 2";
            topicText  = "the piano has been drinking";
            language   = "en-US";
            deepLink   = "http://dummy/";
            categories = "cat1, cat6";
            group      = "mygroup";

            string           topicHandle2      = string.Empty;
            PostTopicRequest postTopicRequest2 = new PostTopicRequest()
            {
                Text = topicText, Title = topicTitle, Categories = categories, Language = language, DeepLink = deepLink, Group = group
            };

            if (appPublished)
            {
                postTopicRequest2.PublisherType = PublisherType.App;
            }
            else
            {
                postTopicRequest2.PublisherType = PublisherType.User;
            }

            HttpOperationResponse <PostTopicResponse> postTopicOperationResponse2 = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest2, authorization : auth);

            if (postTopicOperationResponse2.Response.IsSuccessStatusCode)
            {
                topicHandle2 = postTopicOperationResponse2.Body.TopicHandle;
            }
            else
            {
                // cleanup: delete topic #1 and delete the user
                await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to post topic #2.");
            }

            pinRequest = new PostPinRequest()
            {
                TopicHandle = topicHandle2
            };
            postPinOperationResponse = await client.MyPins.PostPinWithHttpMessagesAsync(authorization : auth, request : pinRequest);

            if (!postPinOperationResponse.Response.IsSuccessStatusCode)
            {
                // cleanup: delete topic #1, topic #2, and delete the user
                await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle2, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle2, authorization : auth);

                await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to pin topic #2.");
            }

            // create topic #3
            topicTitle = "topic number 3";
            topicText  = "the carpet needs a haircut";
            language   = "en-US";
            deepLink   = "http://dummy/";
            categories = "cat1, cat6";
            group      = "mygroup";

            string           topicHandle3      = string.Empty;
            PostTopicRequest postTopicRequest3 = new PostTopicRequest()
            {
                Text = topicText, Title = topicTitle, Categories = categories, Language = language, DeepLink = deepLink, Group = group
            };

            if (appPublished)
            {
                postTopicRequest3.PublisherType = PublisherType.App;
            }
            else
            {
                postTopicRequest3.PublisherType = PublisherType.User;
            }

            HttpOperationResponse <PostTopicResponse> postTopicOperationResponse3 = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest3, authorization : auth);

            if (postTopicOperationResponse3.Response.IsSuccessStatusCode)
            {
                topicHandle3 = postTopicOperationResponse3.Body.TopicHandle;
            }
            else
            {
                // cleanup: delete topic #1, topic #2, and delete the user
                await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle2, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle2, authorization : auth);

                await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to post topic #3.");
            }

            pinRequest = new PostPinRequest()
            {
                TopicHandle = topicHandle3
            };
            postPinOperationResponse = await client.MyPins.PostPinWithHttpMessagesAsync(authorization : auth, request : pinRequest);

            if (!postPinOperationResponse.Response.IsSuccessStatusCode)
            {
                // cleanup: delete topic #1, topic #2, topic #3 and delete the user
                await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle3, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle3, authorization : auth);

                await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle2, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle2, authorization : auth);

                await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to pin topic #3.");
            }

            var pinFeedOperationResponse = await client.MyPins.GetPinsWithHttpMessagesAsync(authorization : auth);

            IList <TopicView> pinFeedResponse = null;

            if (pinFeedOperationResponse.Response.IsSuccessStatusCode)
            {
                pinFeedResponse = pinFeedOperationResponse.Body.Data;
            }
            else
            {
                // cleanup: delete topic #1, topic #2, topic #3 and delete the user
                await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle3, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle3, authorization : auth);

                await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle2, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle2, authorization : auth);

                await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                if (appPublished)
                {
                    ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                }

                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to get the pin feed.");
            }

            // after getting the pin feed, clean up
            await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle3, authorization : auth);

            await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle3, authorization : auth);

            await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle2, authorization : auth);

            await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle2, authorization : auth);

            await client.MyPins.DeletePinWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

            await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

            bool?adminDeleted = null;

            if (appPublished)
            {
                adminDeleted = ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
            }

            await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

            if (appPublished)
            {
                Assert.IsTrue(adminDeleted.HasValue);
                Assert.IsTrue(adminDeleted.Value);
            }

            // after clean up, check the content of the pin feed
            Assert.AreEqual(pinFeedResponse.Count, 3);
            Assert.AreEqual(pinFeedResponse[0].Title, "topic number 3");
            Assert.AreEqual(pinFeedResponse[1].Title, "topic number 2");
            Assert.AreEqual(pinFeedResponse[2].Title, "topic number 1");
        }
示例#5
0
        /// <summary>
        /// Pin a topic
        /// </summary>
        /// <param name='request'>
        /// Post pin request
        /// </param>
        /// <param name='authorization'>
        /// Format is: "Scheme CredentialsList". Possible values are:
        ///
        /// - Anon AK=AppKey
        ///
        /// - SocialPlus TK=SessionToken
        ///
        /// - Facebook AK=AppKey|TK=AccessToken
        ///
        /// - Google AK=AppKey|TK=AccessToken
        ///
        /// - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken
        ///
        /// - Microsoft AK=AppKey|TK=AccessToken
        ///
        /// - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken
        /// </param>
        /// <param name='customHeaders'>
        /// Headers that will be added to request.
        /// </param>
        /// <param name='cancellationToken'>
        /// The cancellation token.
        /// </param>
        /// <return>
        /// A response object containing the response body and response headers.
        /// </return>
        public async Task <HttpOperationResponse <object> > PostPinWithHttpMessagesAsync(PostPinRequest request, string authorization, Dictionary <string, List <string> > customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (request == null)
            {
                throw new ValidationException(ValidationRules.CannotBeNull, "request");
            }
            if (request != null)
            {
                request.Validate();
            }
            if (authorization == null)
            {
                throw new ValidationException(ValidationRules.CannotBeNull, "authorization");
            }
            // Tracing
            bool   _shouldTrace  = ServiceClientTracing.IsEnabled;
            string _invocationId = null;

            if (_shouldTrace)
            {
                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("request", request);
                tracingParameters.Add("authorization", authorization);
                tracingParameters.Add("cancellationToken", cancellationToken);
                ServiceClientTracing.Enter(_invocationId, this, "PostPin", tracingParameters);
            }
            // Construct URL
            var _baseUrl = this.Client.BaseUri.AbsoluteUri;
            var _url     = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "v0.7/users/me/pins").ToString();
            // Create HTTP transport objects
            HttpRequestMessage  _httpRequest  = new HttpRequestMessage();
            HttpResponseMessage _httpResponse = null;

            _httpRequest.Method     = new HttpMethod("POST");
            _httpRequest.RequestUri = new Uri(_url);
            // Set Headers
            if (authorization != null)
            {
                if (_httpRequest.Headers.Contains("Authorization"))
                {
                    _httpRequest.Headers.Remove("Authorization");
                }
                _httpRequest.Headers.TryAddWithoutValidation("Authorization", authorization);
            }
            if (customHeaders != null)
            {
                foreach (var _header in customHeaders)
                {
                    if (_httpRequest.Headers.Contains(_header.Key))
                    {
                        _httpRequest.Headers.Remove(_header.Key);
                    }
                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
                }
            }

            // Serialize Request
            string _requestContent = null;

            if (request != null)
            {
                _requestContent      = SafeJsonConvert.SerializeObject(request, this.Client.SerializationSettings);
                _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8);
                _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
            }
            // Send Request
            if (_shouldTrace)
            {
                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
            }
            cancellationToken.ThrowIfCancellationRequested();
            _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);

            if (_shouldTrace)
            {
                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
            }
            HttpStatusCode _statusCode = _httpResponse.StatusCode;

            cancellationToken.ThrowIfCancellationRequested();
            string _responseContent = null;

            if ((int)_statusCode != 204 && (int)_statusCode != 400 && (int)_statusCode != 401 && (int)_statusCode != 404 && (int)_statusCode != 409 && (int)_statusCode != 500)
            {
                var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                ex.Request  = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
                if (_shouldTrace)
                {
                    ServiceClientTracing.Error(_invocationId, ex);
                }
                _httpRequest.Dispose();
                if (_httpResponse != null)
                {
                    _httpResponse.Dispose();
                }
                throw ex;
            }
            // Create Result
            var _result = new HttpOperationResponse <object>();

            _result.Request  = _httpRequest;
            _result.Response = _httpResponse;
            // Deserialize Response
            if ((int)_statusCode == 204)
            {
                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                try
                {
                    _result.Body = SafeJsonConvert.DeserializeObject <object>(_responseContent, this.Client.DeserializationSettings);
                }
                catch (JsonException ex)
                {
                    _httpRequest.Dispose();
                    if (_httpResponse != null)
                    {
                        _httpResponse.Dispose();
                    }
                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
                }
            }
            if (_shouldTrace)
            {
                ServiceClientTracing.Exit(_invocationId, _result);
            }
            return(_result);
        }
示例#6
0
 /// <summary>
 /// Pin a topic
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='request'>
 /// Post pin request
 /// </param>
 /// <param name='authorization'>
 /// Format is: "Scheme CredentialsList". Possible values are:
 ///
 /// - Anon AK=AppKey
 ///
 /// - SocialPlus TK=SessionToken
 ///
 /// - Facebook AK=AppKey|TK=AccessToken
 ///
 /// - Google AK=AppKey|TK=AccessToken
 ///
 /// - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken
 ///
 /// - Microsoft AK=AppKey|TK=AccessToken
 ///
 /// - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken
 /// </param>
 public static object PostPin(this IMyPins operations, PostPinRequest request, string authorization)
 {
     return(Task.Factory.StartNew(s => ((IMyPins)s).PostPinAsync(request, authorization), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }