using System.Collections.Generic;
using System.Configuration;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using CompCare.EndpointMgr;
public class RemoteClient
public DeliveryMethod DeliveryMethod
public ContentNegotiation ContentNegotiation
private String EndpointAddress
String sectionName = ClientEndpointSettings.SectionName,
assemblyLocation = Assembly.GetEntryAssembly().Location;
var configuration = ((ClientEndpointSettings)
ConfigurationManager.OpenExeConfiguration(assemblyLocation)
.GetSection(sectionName));
ProfileList profileList = configuration.ProfileList;
Service endpoint = profileList.ClientProfile.Service;
return String.Format("http://{0}:{1}/EnrollMemberInterface", endpoint.Address, endpoint.PortNum);
private SessionWebClient _csrfWebClientInstanceAccessor = default(SessionWebClient);
private SessionWebClient CsrfWebClientInstanceAccessor
get { return _csrfWebClientInstanceAccessor; }
set { _csrfWebClientInstanceAccessor = value; }
private SessionWebClient CsrfWebClientInstance
string csrfTokenEndpointAddress = String.Format("{0}/GetRequestToken", EndpointAddress);
var cookieJar = new CookieContainer();
var uri = new Uri(csrfTokenEndpointAddress, UriKind.Absolute);
var csrfTokenReq = new HttpClient();
csrfTokenReq.DefaultRequestHeaders
.Add("Content-Type", "text/html");
string csrfRqstToken = string.Empty;
Task request = csrfTokenReq.GetAsync(uri)
.ContinueWith(response =>
HttpResponseMessage message = response.Result;
HttpResponseHeaders responseHeaders = message.Headers;
var csrfRackRequestTkn = (List<string>)
responseHeaders.GetValues("Request-Token");
csrfRqstToken = csrfRackRequestTkn[0];
if (CsrfWebClientInstanceAccessor == null)
CsrfWebClientInstanceAccessor = new SessionWebClient();
CsrfWebClientInstanceAccessor.Headers.Add("X_CSRF_TOKEN", csrfRqstToken);
CsrfWebClientInstanceAccessor.BaseAddress = EndpointAddress;
CsrfWebClientInstanceAccessor.Headers["ContentType"] = "application/x-www-form-urlencoded";
return CsrfWebClientInstanceAccessor;
CsrfWebClientInstanceAccessor = value;
static T CastHelper<T>(object obj, T type)
private SessionWebClient WebClientInit<T>(SessionWebClient instance, params T[] httpHeaders)
var item = CastHelper(o, new { Key = "", Value = "" });
instance.Headers.Add(item.Key, item.Value);
public Expected<Byte[]> FetchPdfCard(String subscriberNumber, String memberIdNumber, ContentNegotiation contentNegotiationMethod)
return Expected<Byte[]>.From(new Byte[]{ })
.ThenExecute(delegate(Byte[] array)
WebClient httpWebRequest = CsrfWebClientInstance;
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("Messaging",
new XAttribute(XNamespace.Xml + "remote", "FetchMemberCardRequest"),
new XElement("Configuration",
new XAttribute(XNamespace.Xml + "id", "CardOptions"),
new XAttribute("name", "DeliveryMethod"),
DeliveryMethod.Download),
new XAttribute("name", "Filename"),
new XAttribute("ContentNegotiation", contentNegotiationMethod))),
new XElement("Container",
new XAttribute(XNamespace.Xml + "id", "CardIdentity"),
new XAttribute("name", "SubscriberIdentity"),
new XAttribute("name", "SubscriberIDNumber"),
StringBuilder builder = new StringBuilder()
var writer = new StringWriter(builder);
xmlFetchRequestDoc.Save(writer, SaveOptions.None);
Byte[] document = ASCIIEncoding.ASCII.GetBytes(
builder.ToString(0, builder.Length));
return httpWebRequest.UploadData(
EndpointAddress, "POST", document);
public Expected<String> NewSubscriber(Hospital hospital, OpenLink record)
StreamReader reader = default(StreamReader);
StringWriter writer = default(StringWriter);
String methodName = MethodBase.GetCurrentMethod().Name;
SessionWebClient httpWebRequest = WebClientInit(CsrfWebClientInstance,
Key = "SnacRequestMethod",
Value = "SyncMemberRequest"
return Expected<String>.From("")
.ThenExecute(delegate(string s)
String idNumber = String.Empty;
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("Messaging",
new XAttribute(XNamespace.Xml + "remote", "SyncMemberRequest"),
new XAttribute(XNamespace.Xml + "unique_id", record["Medical Records No"]),
new XAttribute(XNamespace.Xml + "location", hospital.Location),
new XElement("MessagePart",
new XAttribute(XNamespace.Xml + "id", "MemberName"),
new XAttribute("name", "First"),
new XAttribute("name", "Middle"),
new XAttribute("name", "Last"),
new XElement("MessagePart",
new XAttribute(XNamespace.Xml + "id", "AddressContainer"),
new XAttribute("name", "Address"),
new XAttribute("name", "City"),
new XAttribute("name", "State"),
new XAttribute("name", "Zipcode"),
(record["ZipCd"].Trim().Length == 0 ? "0000000" : record["ZipCd"]))
new XElement("MessagePart",
new XAttribute(XNamespace.Xml + "id", "DateContainer"),
new XAttribute("name", "Birthdate"),
new XAttribute("name", "LastUpdate"),
new XElement("MessagePart",
new XAttribute(XNamespace.Xml + "id", "MiscContainer"),
new XAttribute("name", "Gender"),
StringBuilder builder = new StringBuilder()
writer = new StringWriter(builder);
doc.Save(writer, SaveOptions.None);
String response = httpWebRequest.UploadString(EndpointAddress,
"POST", builder.ToString(0, builder.Length));
reader = new StreamReader(response);
XDocument document = XDocument.Parse(reader.ReadToEnd());
foreach (XElement element in document.XPathSelectElements("/Messaging/Response"))
idNumber = element.Attribute(XNamespace.Xml + "record_id").Value;