public void SetRequestHeader(
            HealthVaultMethods method,
            int methodVersion,
            bool isAnonymous,
            Guid?recordId,
            Guid?appId,
            string infoXml,
            Request request)
        {
            request.Header = new RequestHeader
            {
                Method        = method.ToString(),
                MethodVersion = methodVersion
            };

            if (recordId != null)
            {
                request.Header.RecordId = recordId.Value.ToString();
            }

            // in case the method is anonymous - set app id, else set auth session
            if (isAnonymous)
            {
                request.Header.AppId = appId.HasValue
                    ? appId.Value.ToString()
                    : _healthVaultConfiguration.MasterApplicationId.ToString();
            }
            else
            {
                request.Header.AuthSession = _connectionInternal.GetAuthSessionHeader();
            }

            request.Header.MessageTime = SDKHelper.XmlFromNow();
            request.Header.MessageTtl  = (int)_healthVaultConfiguration.RequestTimeToLiveDuration.TotalSeconds;

            request.Header.Version =
                $"{_telemetryInformation.Category}/{_telemetryInformation.FileVersion} {_telemetryInformation.OsInformation}";

            request.Header.InfoHash = new InfoHash
            {
                HashData = _cryptographer.Hash(Encoding.UTF8.GetBytes(infoXml))
            };
        }
Example #2
0
        public string Serialize(RequestHeader requestHeader)
        {
            if (requestHeader == null)
            {
                throw new ArgumentException(nameof(requestHeader));
            }

            string result;

            using (StringWriter headerXml = new StringWriter())
            {
                using (XmlWriter writer = XmlWriter.Create(headerXml, SDKHelper.XmlUtf8WriterSettings))
                {
                    using (new TagWriter(writer, "header"))
                    {
                        // <method>
                        writer.WriteElementString("method", requestHeader.Method);

                        if (requestHeader.MethodVersion > 0)
                        {
                            // <method-version>
                            writer.WriteElementString("method-version", requestHeader.MethodVersion.ToString());
                        }

                        if (!string.IsNullOrEmpty(requestHeader.TargetPersonId))
                        {
                            // <target-person-id>
                            writer.WriteElementString("target-person-id", requestHeader.TargetPersonId);
                        }

                        if (!string.IsNullOrEmpty(requestHeader.RecordId))
                        {
                            // <record-id>
                            writer.WriteElementString("record-id", requestHeader.RecordId);
                        }

                        if (requestHeader.HasAuthSession)
                        {
                            using (new TagWriter(writer, "auth-session"))
                            {
                                writer.WriteElementString("auth-token", requestHeader.AuthSession.AuthToken);

                                if (requestHeader.HasOfflinePersonInfo)
                                {
                                    writer.WriteStartElement("offline-person-info");

                                    // <offline-person-id>
                                    writer.WriteElementString(
                                        "offline-person-id",
                                        requestHeader.AuthSession.Person.OfflinePersonId.ToString());

                                    // </offline-person-info>
                                    writer.WriteEndElement();
                                }
                                else if (requestHeader.HasUserAuthToken)
                                {
                                    writer.WriteElementString("user-auth-token",
                                                              requestHeader.AuthSession.UserAuthToken);
                                }
                            }
                        }

                        // In case auth session is not present, appid will be sent
                        else
                        {
                            writer.WriteElementString("app-id", requestHeader.AppId);
                        }

                        if (!string.IsNullOrEmpty(requestHeader.CultureCode))
                        {
                            writer.WriteElementString("culture-code", requestHeader.CultureCode);
                        }

                        writer.WriteElementString("msg-time", SDKHelper.XmlFromNow());
                        writer.WriteElementString("msg-ttl", requestHeader.MessageTtl.ToString());

                        writer.WriteElementString("version", requestHeader.Version);

                        InfoHash infoHash = requestHeader.InfoHash;

                        if (infoHash != null)
                        {
                            using (new TagWriter(writer, "info-hash"))
                            {
                                using (new TagWriter(writer, "hash-data"))
                                {
                                    writer.WriteAttributeString("algName", infoHash.HashData.Algorithm);
                                    writer.WriteString(infoHash.HashData.Value);
                                }
                            }
                        }
                    }
                }

                result = headerXml.ToString();
            }

            return(result);
        }