コード例 #1
0
        /// <summary> Update object by ID </summary>
        protected Response <T> _UpdateObject <T>(String userApiToken, T existingObj) where T : ICropioObject
        {
            Response <T> asdf = new Response <T> {
                Data = existingObj
            };

            string json = JsonConvert.SerializeObject(asdf, Formatting.Indented);

            String       requestAddress = String.Format(@"{0}/{1}", CropioDataModel.Name <T>(), existingObj.Id);
            Response <T> obj;

            using (HttpClient httpClient = HttpClientHelper.ConstructHttpClient(userApiToken, BaseAddress))
            {
                using (StringContent content = new StringContent(json, Encoding.UTF8, "application/json"))
                {
                    using (HttpResponseMessage response = httpClient.PutAsync(requestAddress, content).Result)
                    {
                        if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                        {
                            throw new CropioAuthorizationException("Авторизация не выполнена");
                        }
                        String responseData = response.Content.ReadAsStringAsync().Result.RemoveBackSlashFromJson();
#if DEBUG
                        responseData = JToken.Parse(responseData).ToString(Formatting.Indented);
#endif
                        obj = JsonConvert.DeserializeObject <Response <T> >(responseData);
                        obj.CropioResponse = new CropioResponse(response);
                    }
                }
            }
            return(obj);
        }
コード例 #2
0
        /// <summary> Get objects changed in specified period </summary>
        /// <remarks> Параметр cropioServerTimeDelta можно получить используя функцию GetServerDeltaTime </remarks>
        protected MassResponse_ChangedObjects <T> _GetChangedObjects <T>(String userApiToken, Int32 limit, DateTime fromTime, TimeSpan cropioServerTimeDelta) where T : ICropioObject
        {
            DateTime timeFrom       = fromTime + cropioServerTimeDelta;
            String   requestAddress = String.Format(@"{0}/changes?{1}from_time={2}", CropioDataModel.Name <T>(), limit, timeFrom);
            MassResponse_ChangedObjects <T> identifiersSet;

            using (HttpClient httpClient = HttpClientHelper.ConstructHttpClient(userApiToken, BaseAddress))
            {
                using (HttpResponseMessage response = httpClient.GetAsync(requestAddress).Result)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        throw new CropioAuthorizationException("Авторизация не выполнена");
                    }
                    String responseData = response.Content.ReadAsStringAsync().Result.RemoveBackSlashFromJson();
#if DEBUG
                    responseData = JToken.Parse(responseData).ToString(Formatting.Indented);
#endif
                    identifiersSet = JsonConvert.DeserializeObject <MassResponse_ChangedObjects <T> >(responseData);
                    identifiersSet.CropioResponse = new CropioResponse(response);
                }
            }

            return(identifiersSet);
        }
コード例 #3
0
        /// <summary> Get the big number of objects by posting the list of ids </summary>
        protected MassResponse <T> _GetObjects <T>(String userApiToken, IEnumerable <Int32> objIds) where T : ICropioObject
        {
            String           requestAddress = String.Format(CropioDataModel.Name <T>() + "/mass_request");
            String           requestContent = String.Format(@"{{ ""data"": [{0}] }}", String.Join(", ", objIds));
            MassResponse <T> obj;

            using (HttpClient httpClient = HttpClientHelper.ConstructHttpClient(userApiToken, BaseAddress))
            {
                using (StringContent content = new StringContent(requestContent, Encoding.UTF8, "application/json"))
                {
                    using (HttpResponseMessage response = httpClient.PostAsync(requestAddress, content).Result)
                    {
                        if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                        {
                            throw new CropioAuthorizationException("Авторизация не выполнена");
                        }
                        String responseJson = response.Content.ReadAsStringAsync().Result.RemoveBackSlashFromJson();
#if DEBUG
                        responseJson = JToken.Parse(responseJson).ToString(Formatting.Indented);
#endif
                        obj = JsonConvert.DeserializeObject <MassResponse <T> >(responseJson);
                        obj.CropioResponse = new CropioResponse(response);
                    }
                }
            }
            return(obj);
        }
コード例 #4
0
        /// <summary> Get the big number of objects by posting the list of ids </summary>
        protected void _SaveJsonObjectsToFile <T>(String userApiToken) where T : ICropioObject
        {
            String directoryPath = "../../../__Cropio DB objects (JSON)";

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            String filename             = String.Format("{0}//{1}.json", directoryPath, CropioDataModel.Name <T>());
            MassResponse <Int32> objIds = _GetObjectsIds <T>(userApiToken);
            FileInfo             file   = new FileInfo(filename);

            if (file.Exists)
            {
                file.Delete();
            }
            //
            using (StreamWriter stream = file.CreateText())
            {
                foreach (IEnumerable <Int32> idsPage in objIds.Data.Paginate(200))
                {
                    String requestAddress = String.Format(CropioDataModel.Name <T>() + "/mass_request");
                    String requestContent = String.Format(@"{{ ""data"": [{0}] }}", String.Join(", ", idsPage));

                    using (HttpClient httpClient = HttpClientHelper.ConstructHttpClient(userApiToken, BaseAddress))
                    {
                        using (StringContent content = new StringContent(requestContent, Encoding.UTF8, "application/json"))
                        {
                            using (HttpResponseMessage response = httpClient.PostAsync(requestAddress, content).Result)
                            {
                                if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                                {
                                    throw new CropioAuthorizationException("Авторизация не выполнена");
                                }
                                String responseJson = response.Content.ReadAsStringAsync().Result.RemoveBackSlashFromJson();
                                responseJson = JToken.Parse(responseJson).ToString(Formatting.Indented);
                                stream.Write(responseJson);
                            }
                        }
                    }
                }
            }
            Console.WriteLine("Получение объектов {0} завершено", CropioDataModel.Name <T>());
        }
コード例 #5
0
        /// <summary> Delete object by id </summary>
        protected CropioResponse _DeleteObject <T>(String userApiToken, Int32 objId) where T : ICropioObject
        {
            String         requestAddress = String.Format(@"{0}/{1}", CropioDataModel.Name <T>(), objId);
            CropioResponse cropioResponse;

            using (HttpClient httpClient = HttpClientHelper.ConstructHttpClient(userApiToken, BaseAddress))
            {
                using (HttpResponseMessage response = httpClient.DeleteAsync(requestAddress).Result)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        throw new CropioAuthorizationException("Авторизация не выполнена");
                    }
                    cropioResponse = new CropioResponse(response);
                }
            }
            return(cropioResponse);
        }
コード例 #6
0
        /// <summary> Get list of GetObjectsIds for all objects </summary>
        protected MassResponse <Int32> _GetObjectsIds <T>(String userApiToken) where T : ICropioObject
        {
            String requestAddress = String.Format(@"{0}/ids", CropioDataModel.Name <T>());
            MassResponse <Int32> identifiersSet;

            using (HttpClient httpClient = HttpClientHelper.ConstructHttpClient(userApiToken, BaseAddress))
            {
                using (HttpResponseMessage response = httpClient.GetAsync(requestAddress).Result)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        throw new CropioAuthorizationException("Авторизация не выполнена");
                    }
                    String responseData = response.Content.ReadAsStringAsync().Result.RemoveBackSlashFromJson();
#if DEBUG
                    responseData = JToken.Parse(responseData).ToString(Formatting.Indented);
#endif
                    identifiersSet = JsonConvert.DeserializeObject <MassResponse <Int32> >(responseData);
                    identifiersSet.CropioResponse = new CropioResponse(response);
                }
            }
            return(identifiersSet);
        }
コード例 #7
0
        /// <summary> Get one object by id </summary>
        protected Response <T> _GetObject <T>(String userApiToken, Int32 objId) where T : ICropioObject
        {
            String       requestAddress = String.Format(@"{0}/{1}", CropioDataModel.Name <T>(), objId);
            Response <T> obj;

            using (HttpClient httpClient = HttpClientHelper.ConstructHttpClient(userApiToken, BaseAddress))
            {
                using (HttpResponseMessage response = httpClient.GetAsync(requestAddress).Result)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        throw new CropioAuthorizationException("Авторизация не выполнена");
                    }
                    String responseJson = response.Content.ReadAsStringAsync().Result.RemoveBackSlashFromJson();
#if DEBUG
                    responseJson = JToken.Parse(responseJson).ToString(Formatting.Indented);
#endif
                    obj = JsonConvert.DeserializeObject <Response <T> >(responseJson);
                    obj.CropioResponse = new CropioResponse(response);
                }
            }
            return(obj);
        }
コード例 #8
0
        /// <summary> Получить разницу во времени с сервером Cropio </summary>
        protected TimeSpan _GetServerDeltaTime(String userApiToken)
        {
            if (String.IsNullOrEmpty(userApiToken))
            {
                throw new CropioAuthorizationException();
            }
            DateTime time           = DateTime.Now;
            String   requestAddress = String.Format(@"{0}/changes_ids?from_time={1}", CropioDataModel.Name <CO_AlertType>(), time);
            TimeSpan timeDelta;

            using (HttpClient httpClient = HttpClientHelper.ConstructHttpClient(userApiToken, BaseAddress))
            {
                using (HttpResponseMessage response = httpClient.GetAsync(requestAddress).Result)
                {
                    String   responseData = response.Content.ReadAsStringAsync().Result.RemoveBackSlashFromJson();
                    DateTime time2        = (JsonConvert.DeserializeObject <MassResponse_Changes>(responseData)).Meta.Request.FromTime;
                    timeDelta = time2 - time;
                }
            }
            return(timeDelta);
        }
コード例 #9
0
        /// <summary> Get list of objects </summary>
        protected MassResponse <T> _GetObjects <T>(String userApiToken, Int32 limit, Int32 fromId = 0) where T : ICropioObject
        {
            String           requestAddress = String.Format("{0}?limit={1},from_id={2}", CropioDataModel.Name <T>(), limit, fromId);
            MassResponse <T> obj;

            using (HttpClient httpClient = HttpClientHelper.ConstructHttpClient(userApiToken, BaseAddress))
            {
                using (HttpResponseMessage response = httpClient.GetAsync(requestAddress).Result)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        throw new CropioAuthorizationException("Авторизация не выполнена");
                    }
                    String responseJson = response.Content.ReadAsStringAsync().Result.RemoveBackSlashFromJson();
                    obj = JsonConvert.DeserializeObject <MassResponse <T> >(responseJson);
                    obj.CropioResponse = new CropioResponse(response);
                }
            }
            return(obj);
        }
コード例 #10
0
        /// <summary> Get list of GetObjectsIds for all objects </summary>
        protected MassResponse <CO_Document> _GetRelatedDocument <T>(String userApiToken, Int32 objId) where T : ICropioDocumentableObject
        {
            String requestAddress = String.Format(@"protected_documents?documentable_type={0}&documentable_id={1}", CropioDataModel.DocumentableObjectTypeName <T>(), objId);
            MassResponse <CO_Document> result;

            using (HttpClient httpClient = HttpClientHelper.ConstructHttpClient(userApiToken, BaseAddress))
            {
                using (HttpResponseMessage response = httpClient.GetAsync(requestAddress).Result)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        throw new CropioAuthorizationException("Авторизация не выполнена");
                    }
                    String responseData = response.Content.ReadAsStringAsync().Result.RemoveBackSlashFromJson();
#if DEBUG
                    responseData = JToken.Parse(responseData).ToString(Formatting.Indented);
#endif
                    result = JsonConvert.DeserializeObject <MassResponse <CO_Document> >(responseData);
                    result.CropioResponse = new CropioResponse(response);
                }
            }
            return(result);
        }