Пример #1
0
        public virtual Task Trigger(string eventName, EventArgs args)
        {
            InvokeEventSubscriptions(eventName, args);
            IJsonable jsonable = args as IJsonable;

            return(Trigger(eventName, jsonable == null ? string.Empty: jsonable.ToJson()));
        }
Пример #2
0
        public async Task <HttpRequestResponse> Delete(Resource resource, IJsonable body, params String[] urlSegments)
        {
            IHttpSetter hs = Setter(resource, urlSegments);

            hs.SetMethod(HttpMethod.Delete)
            .SetBody(FormatJson(body?.ToJson()));
            return(await RunRequestFromSetter(hs));
        }
Пример #3
0
        }         // CreatePostRequest

        private RestRequest CreateRequest(string sResource = "", IJsonable oData = null)
        {
            Method nMethod   = oData == null ? Method.GET : Method.POST;
            object oJsonData = null;

            var oHeaders = new Dictionary <string, string>();

            oHeaders["Accept"] = "application/xml";

            var oRequest = new RestRequest(sResource, nMethod);

            if (null != oData)
            {
                oHeaders["Content-Type"] = "application/json";
                oHeaders["x-li-format"]  = "json";

                oRequest.RequestFormat = DataFormat.Json;

                oJsonData = oData.ToJson();
                oRequest.AddBody(oJsonData);
            }             // if

            var aryHeaders = new List <string>();

            foreach (KeyValuePair <string, string> h in oHeaders)
            {
                oRequest.AddHeader(h.Key, h.Value);
                aryHeaders.Add(string.Format("{0}: {1}", h.Key, h.Value));
            }             // for each

            Debug(@"
*******************************************
*
* Request details - begin
*
*******************************************

Method: {0}

Resourse: {1}

Headers: {2}

Data: {3}

*******************************************
*
* Request details - end
*
*******************************************
", nMethod.ToString(), sResource, string.Join("\n         ", aryHeaders), oJsonData == null ? "-- no data --" : JsonConvert.SerializeObject(oJsonData));

            return(oRequest);
        }         // CreateRequest
Пример #4
0
        /// <summary>
        /// Send to remote host with user id
        /// </summary>
        /// <param name="query"> A copied <code>QueryObject</code> </param>
        private static async Task <ReceiveObject> SendWithUser(IJsonable query)
        {
            if (Storage.Test)
            {
                return(new ReceiveObject());
            }

            if (!Instance.SocketConnected)
            {
                await Instance.Reconnect();
            }
            query.UserId = Storage.UserId;
            var send = Encoding.UTF8.GetBytes(query.ToJson());

            byte[] receive;
            try
            {
                int sendLen = await Instance.Send(send);

                Debug.WriteLine("Send of \"{0}\" finish, total {1} bytes.", query.Type, sendLen);

                receive = new byte[1 << 14];
                int recvLen = await Instance.Receive(receive);

                Debug.WriteLine("Receive finish, total {0} bytes.", recvLen);

                Instance.Close();
            }
            catch (Exception)
            {
                await Instance.Reconnect();

                return(await SendWithUser(query));                // recurrence
            }
            var json       = Encoding.UTF8.GetString(receive);
            var receiveObj = ReceiveObject.FromJson(json);

            if (receiveObj == null)
            {
                await Instance.Reconnect();

                return(await SendWithUser(query));                // recurrence
            }
            return(receiveObj);
        }
Пример #5
0
        public string ToJson()
        {
            IJsonable args = EventArgs as IJsonable;

            if (args != null)
            {
                return(args.ToJson());
            }
            else
            {
                return(new
                {
                    Name = Name,
                    UserName = UserName,
                    Json = Json
                }.ToJson());
            }
        }
Пример #6
0
        /// <summary>
        /// Send to remote host with user id of admin
        /// </summary>
        /// <param name="query"> A copied <code>QueryObject</code> </param>
        private static async Task <string> SendAdminWithUser(IJsonable query)
        {
            if (Storage.Test)
            {
                return("");
            }

            if (!InstanceAdmin.SocketConnected)
            {
                await InstanceAdmin.Reconnect();
            }
            query.UserId = Storage.UserId;
            var send = Encoding.UTF8.GetBytes(query.ToJson());

            byte[] receive;
            try
            {
                int sendLen = await InstanceAdmin.Send(send);

                Debug.WriteLine("Send of \"{0}\" finish, total {1} bytes.", query.Type, sendLen);

                receive = new byte[1 << 14];
                int recvLen = await InstanceAdmin.Receive(receive);

                Debug.WriteLine("Receive finish, total {0} bytes.", recvLen);

                InstanceAdmin.Close();
            }
            catch (Exception)
            {
                await InstanceAdmin.Reconnect();

                return(await SendAdminWithUser(query));                // recurrence
            }
            var str = Encoding.UTF8.GetString(receive);

            if (str == null || str.Trim().Length == 0)
            {
                await InstanceAdmin.Reconnect();

                return(await SendAdminWithUser(query));                // recurrence
            }
            return(str);
        }