public void Test_FindMessageFromServiceInstance_RoundTrip()
        {
            this.RunHttp200WithCustomResponseProdEndpoint(
                (exchangeService) =>
            {
                FindItemsResults <Item> results = exchangeService.FindItems(
                    WellKnownFolderName.Inbox,
                    new MessageView(10, false));

                Assert.IsTrue(results.Items.Count == 10);
            },
                () =>
            {
                List <Message> messages = new List <Message>();
                for (int i = 0; i < 10; i++)
                {
                    Message msg = new Message();
                    msg.Subject = $"aa - {i}";
                    msg.IsRead  = false;

                    msg.ResetChangeTracking();
                    messages.Add(msg);
                }

                EntityResponseCollection <Message> entityResponseCollection = new EntityResponseCollection <Message>();
                entityResponseCollection.ODataContext =
                    "https://outlook.office365.com/api/v2.0/$metadata#users(user)/mailfolders('Inbox')/messages";
                entityResponseCollection.Value = messages;
                return(Serializer.Instance.Serialize(entityResponseCollection));
            },
                (httpRequestMessage) => { });
        }
Ejemplo n.º 2
0
        /// <param name='organizationUrl'>
        /// Required. Organization URL
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        public async Task <HttpOperationResponse <object> > GetEntitiesWithOperationResponseAsync(string organizationUrl, CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            // Validate
            if (organizationUrl == null)
            {
                throw new ArgumentNullException("organizationUrl");
            }

            // Tracing
            bool   shouldTrace  = ServiceClientTracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = ServiceClientTracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("organizationUrl", organizationUrl);
                ServiceClientTracing.Enter(invocationId, this, "GetEntitiesAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/crm/exporter/metadata/entities";
            List <string> queryParameters = new List <string>();

            queryParameters.Add("organizationUrl=" + Uri.EscapeDataString(organizationUrl));
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = new HttpRequestMessage();

            httpRequest.Method     = HttpMethod.Get;
            httpRequest.RequestUri = new Uri(url);

            // Set Credentials
            if (this.Client.Credentials != null)
            {
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
            }

            // Send Request
            if (shouldTrace)
            {
                ServiceClientTracing.SendRequest(invocationId, httpRequest);
            }
            cancellationToken.ThrowIfCancellationRequested();
            HttpResponseMessage httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

            if (shouldTrace)
            {
                ServiceClientTracing.ReceiveResponse(invocationId, httpResponse);
            }
            HttpStatusCode statusCode = httpResponse.StatusCode;

            cancellationToken.ThrowIfCancellationRequested();
            string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.BadRequest && statusCode != HttpStatusCode.Unauthorized && statusCode != HttpStatusCode.Forbidden && statusCode != HttpStatusCode.InternalServerError)
            {
                HttpOperationException <object> ex = new HttpOperationException <object>();
                ex.Request  = httpRequest;
                ex.Response = httpResponse;
                ex.Body     = null;
                if (shouldTrace)
                {
                    ServiceClientTracing.Error(invocationId, ex);
                }
                throw ex;
            }

            // Create Result
            HttpOperationResponse <object> result = new HttpOperationResponse <object>();

            result.Request  = httpRequest;
            result.Response = httpResponse;

            // Deserialize Response
            if (statusCode == HttpStatusCode.OK)
            {
                IList <EntityResponse> resultModel = new List <EntityResponse>();
                JToken responseDoc = null;
                if (string.IsNullOrEmpty(responseContent) == false)
                {
                    responseDoc = JToken.Parse(responseContent);
                }
                if (responseDoc != null)
                {
                    resultModel = EntityResponseCollection.DeserializeJson(responseDoc);
                }
                result.Body = resultModel;
            }
            if (statusCode == HttpStatusCode.BadRequest || statusCode == HttpStatusCode.InternalServerError)
            {
                ErrorResponse resultModel = new ErrorResponse();
                JToken        responseDoc = null;
                if (string.IsNullOrEmpty(responseContent) == false)
                {
                    responseDoc = JToken.Parse(responseContent);
                }
                if (responseDoc != null)
                {
                    resultModel.DeserializeJson(responseDoc);
                }
                result.Body = resultModel;
            }

            if (shouldTrace)
            {
                ServiceClientTracing.Exit(invocationId, result);
            }
            return(result);
        }