Esempio n. 1
0
        public IAsyncResult BeginPost(string classname, object pobject, ProtoAPIPostCallback callback)
        {
            if (string.IsNullOrEmpty(classname) || null == pobject)
            {
                throw new ProtoAPIException("Invalid parameters: AsyncGet cannot be completed");
            }

            CheckUserAndAppKey();

            ResponseStatus <string> response_status = new ResponseStatus <string>();
            ManualResetEvent        finished_event  = new ManualResetEvent(false);
            RESTAsyncResult         async_result    = new RESTAsyncResult(finished_event, response_status);

            string payload = null;

            try{
                payload = JsonConvert.SerializeObject(pobject, new IsoDateTimeConverter());
            } catch (Exception e) {
                throw new ProtoAPIException("Unable to serialize object to json: " + e.Message);
            }
            if (string.IsNullOrEmpty(payload))
            {
                throw new ProtoAPIException("Resulting json string is null or empty");
            }

            RESTClient client = new RESTClient(m_ServiceUri);

            client.SetBasicAuthorizationCredential(m_username, m_appkey);

            client.AsyncPost("objects/" + classname, null, payload, delegate(int status, string response){
                response_status.statusCode = status;

                if (status == 201)
                {
                    JsonResponseObject <string> data_wrapper;
                    data_wrapper = JsonConvert.DeserializeObject <JsonResponseObject <string> >(response, new IsoDateTimeConverter());

                    response_status.responseData = data_wrapper.data;

                    if (null != callback)
                    {
                        callback(response_status, data_wrapper.data as string);
                    }
                }
                else
                {
                    response_status.responseData = response;
                    response_status.errorMessage = response;

                    if (null != callback)
                    {
                        callback(response_status, null);
                    }
                }

                async_result.SetCompleted();
            });

            return(async_result);
        }