private async Task <IShipmentServerResponse> Request(string command, string data = "", string options = "", string context = "")
        {
            var client = new HttpClient();
            var values = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("options", options),
                new KeyValuePair <string, string>("data", data),
                new KeyValuePair <string, string>("actor", ActorID.ToString()),
                new KeyValuePair <string, string>("key", Key),
                new KeyValuePair <string, string>("command", command),
                new KeyValuePair <string, string>("context", context)
            };
            var content        = new FormUrlEncodedContent(values);
            var serverresponse = await client.PostAsync(Url, content);

            var response = await serverresponse.Content.ReadAsStringAsync();

            var result = new ShipmentServerResponse
            {
                Request = new
                {
                    options = options.ToObject(),
                    data    = data.ToObject(),
                    actor   = ActorID,
                    key     = Key,
                    command = command,
                    context = context.ToObject()
                    ,
                }.ToJson(),
                Response = response.ToObject().ToJson(),
                Info     = response.ToObject <ShipmentServerMessages>(),
            };

            return(result);
        }
        private async Task <IShipmentServerResponse <T> > Request <T>(string command, string data = "", string options = "", string context = "")
        {
            var response = await Request(command, data, options, context);

            var result = new ShipmentServerResponse <T>(response as ShipmentServerResponse);

            if (!string.IsNullOrWhiteSpace(result.Response) && result.Info.ErrorMessages == null && result.Info.Messages == null)
            {
                try
                {
                    result.Result = result.Response.ToObject <T>();
                }
                catch (Exception)
                {
                }
            }
            return(result);
        }
        private async Task <IShipmentServerResponse <TResult> > RequestAnonymous <T, TResult>(T template, Func <T, TResult> selector, string command, string data = "", string options = "", string context = "")
        {
            var response = await Request(command, data, options, context);

            var result = new ShipmentServerResponse <TResult>(response as ShipmentServerResponse);

            if (!string.IsNullOrWhiteSpace(result.Response) && result.Info.ErrorMessages == null && result.Info.Messages == null)
            {
                try
                {
                    T obj = JsonConvert.DeserializeAnonymousType(result.Response, template);
                    result.Result = selector(obj);
                }
                catch (Exception)
                {
                }
            }
            return(result);
        }
        private async Task <IShipmentServerResponse <byte[]> > RequestBinary(string command, string data = "", string options = "", string context = "")
        {
            var client = new HttpClient();
            var values = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("options", options),
                new KeyValuePair <string, string>("data", data),
                new KeyValuePair <string, string>("actor", ActorID.ToString()),
                new KeyValuePair <string, string>("key", Key),
                new KeyValuePair <string, string>("command", command),
                new KeyValuePair <string, string>("context", context)
            };
            var content        = new FormUrlEncodedContent(values);
            var serverresponse = await client.PostAsync(Url, content);

            var bytes = serverresponse.IsSuccessStatusCode ? await serverresponse.Content.ReadAsByteArrayAsync() : null;

            var info = serverresponse.IsSuccessStatusCode ? string.Empty : await serverresponse.Content.ReadAsStringAsync();

            var result = new ShipmentServerResponse <byte[]>
            {
                Request = new
                {
                    options = options.ToObject(),
                    data    = data.ToObject(),
                    actor   = ActorID,
                    key     = Key,
                    command = command,
                    context = context.ToObject()
                    ,
                }.ToJson(),
                Response = serverresponse.Content.Headers.ToJson(),
                Info     = info.ToObject <ShipmentServerMessages>(),
                Result   = bytes,
            };

            return(result);
        }
Exemple #5
0
 public ShipmentServerResponse(ShipmentServerResponse instance)
 {
     this.Info     = instance.Info;
     this.Request  = instance.Request;
     this.Response = instance.Response;
 }