private void SerializeRequest(TextWriter textWriter, Request request, object context)
 {
     // Delegate error checking to serializer
     using (var xmlWriter = (HealthVaultXmlWriter) HealthVaultXmlWriter.Create(textWriter))
     {
         xmlWriter.AllowRootPrefix = true;
         xmlWriter.Context = context;
         m_requestSerializer.Serialize(xmlWriter, request, m_requestNamespaces);
     }
 }
Exemplo n.º 2
0
 private void NotifyReceived(Request request, string xml)
 {
     if (ReceivedXml != null)
     {
         ReceivedXml(this, request, xml);
     }
 }
Exemplo n.º 3
0
 private void NotifySending(Request request, string xml)
 {
     if (SendingXml != null)
     {
         SendingXml(this, request, xml);
     }
 }
Exemplo n.º 4
0
 private void NotifySending(Request request)
 {
     if (SendingRequest != null)
     {
         SendingRequest(this, request);
     }
 }
Exemplo n.º 5
0
        private Response DeserializeResponseXml(Request request, TextReader reader, Type bodyType)
        {
            var context = new ResponseDeserializationContext {BodyType = bodyType};

            var response = (Response) Serializer.Deserialize(reader, typeof (Response), context);
            response.Request = request;

            NotifyReceived(response);

            return response;
        }
Exemplo n.º 6
0
        private async Task<Response> DeserializeResponseDebug(Request request, HttpContent content, Type bodyType)
        {
            string xml = await content.ReadAsStringAsync();
            NotifyReceived(request, xml);

            using (var reader = new StringReader(xml))
            {
                return DeserializeResponseXml(request, reader, bodyType);
            }
        }
Exemplo n.º 7
0
        public async Task<Response> ExecuteRequestAsync(
            Request request, Type responseBodyType,
            CancellationToken cancelToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            Response response = null;
            int attempt = 1;
            const int maxAttempts = 2;

            while (attempt <= maxAttempts)
            {
                //
                // Ensure we've got a session set up
                //
                SessionCredential credentials = null;
                if (!request.IsAnonymous)
                {
                    credentials = await EnsureCredentialsAsync(cancelToken);
                }
                //
                // Prepare request - adding headers, session & auth information
                //
                PrepareRequestAsync(request, credentials);
                //
                // Notify any subscribers
                //
                NotifySending(request);
                //
                // Call HealthVault
                //
                response = await GetResponseAsync(request, responseBodyType, cancelToken);
                if (
                    response.IsSuccess ||
                        !(response.Status.IsStatusCredentialsExpired || response.Status.IsStatusServerFailure) ||
                        attempt == maxAttempts)
                {
                    break;
                }

                if (response.Status.IsStatusCredentialsExpired)
                {
                    await RefreshSessionTokenAsync(cancelToken);
                }

                ++attempt;
            }

            return response;
        }
Exemplo n.º 8
0
 private void EnsureBodyHash(Request request)
 {
     if (!request.Header.HasBodyHash)
     {
         request.Header.BodyHash = new HashData(request.Body.Hash(m_cryptographer));
     }
 }
Exemplo n.º 9
0
        private void EnsureSession(Request request, SessionCredential credentials)
        {
            if (credentials == null)
            {
                return;
            }

            var session = new AuthSession();
            session.Token = credentials.Token;
            if (request.Record != null)
            {
                session.Person = new OfflinePersonInfo(request.Record.PersonId);
            }

            request.Header.Session = session;
        }
Exemplo n.º 10
0
        private void PrepareAuth(Request request, SessionCredential credentials)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (request.IsAnonymous)
            {
                return;
            }
            if (credentials == null)
            {
                throw new ArgumentNullException("credentials");
            }

            VerifyCredentials();

            string headerXml = request.Header.ToXml();
            Hmac hmac = m_cryptographer.Hmac(credentials.SharedSecret, headerXml);
            request.Auth = new RequestAuth(hmac);
        }
Exemplo n.º 11
0
 private void EnsureStandardHeaders(Request request)
 {
     RequestHeader header = request.Header;
     if (request.Record != null)
     {
         header.RecordId = request.Record.RecordId;
     }
     if (!header.HasLanguage)
     {
         header.Language = m_appInfo.Language;
     }
     if (!header.HasCountry)
     {
         header.Country = m_appInfo.Country;
     }
 }
Exemplo n.º 12
0
        private void PrepareHeader(Request request, SessionCredential credentials)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            EnsureStandardHeaders(request);
            EnsureBodyHash(request);
            EnsureSession(request, credentials);
        }
Exemplo n.º 13
0
        private void PrepareRequestAsync(Request request, SessionCredential credentials)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            PrepareHeader(request, credentials);
            PrepareAuth(request, credentials);

            request.Validate();
        }
Exemplo n.º 14
0
        //----------------------------------------
        //
        // Implementation
        //
        //----------------------------------------

        private async Task<Response> GetResponseAsync(
            Request request, Type responseBodyType,
            CancellationToken cancelToken)
        {
            //
            // Serialize the request
            //
            StringContent content = SerializeRequest(request);
            //
            // Call the server. 
            //
            HttpResponseMessage httpResponse = await m_transport.SendAsync(content, cancelToken);
            using (httpResponse)
            {
                //
                // Deserialize the response
                //
                if (Debug)
                {
                    return await DeserializeResponseDebug(request, httpResponse.Content, responseBodyType);
                }

                return await DeserializeResponse(request, httpResponse.Content, responseBodyType);
            }
        }
 public override Request CreateRequest()
 {
     Request request = new Request("BeginPutBlob", 1);
     request.Record = m_record;
     return request;
 }
Exemplo n.º 16
0
        private StringContent SerializeRequest(Request request)
        {
            string xml;
            using (var writer = new StringWriter())
            {
                Serializer.Serialize(writer, request, null);
                xml = writer.ToString();
            }

            NotifySending(request, xml);

            return new StringContent(xml);
        }
 public override Request CreateRequest()
 {
     Request request = new Request("PutThings", 2, m_body);
     request.Record = m_record;
     return request;
 }
Exemplo n.º 18
0
 private async Task<Response> DeserializeResponse(Request request, HttpContent content, Type bodyType)
 {
     using (Stream contentStream = await content.ReadAsStreamAsync())
     {
         using (var reader = new StreamReader(contentStream, Encoding.UTF8, false, DefaultBufferSize, true))
         {
             return DeserializeResponseXml(request, reader, bodyType);
         }
     }
 }
Exemplo n.º 19
0
 public override Request CreateRequest()
 {
     Request request = new Request("GetVocabulary", 2, m_body);
     request.Header.AppId = this.Client.State.ProvisioningInfo.AppIdInstance;
     return request;
 }
Exemplo n.º 20
0
        //----------------------------------------
        //
        // Methods
        //
        //----------------------------------------

        public async Task<Response> ExecuteRequestAsync(Request request, Type responseBodyType)
        {
            return await ExecuteRequestAsync(request, responseBodyType, CancellationToken.None);
        }