示例#1
0
        public static T FromCacheOrNew <T>(string id, float maxCacheAge = -1f) where T : ApiModel, ApiCacheObject, new()
        {
            T val = new T();

            val.id = id;
            T target = val;

            if (!ApiCache.Fetch(id, ref target, maxCacheAge))
            {
                ApiCache.Save(id, target);
            }
            return(target);
        }
示例#2
0
        public unsafe static T Fetch <T>(string id, Action <ApiContainer> onSuccess = null, Action <ApiContainer> onFailure = null, bool disableCache = false) where T : ApiModel, ApiCacheObject, new()
        {
            id = id?.Trim();
            T val = new T();

            val.id = id;
            T model = (T)val;

            if (!disableCache && ApiCache.Fetch(id, ref *(T *)(&model)))
            {
                if (onSuccess != null)
                {
                    UpdateDelegator.Dispatch(delegate
                    {
                        onSuccess(new ApiModelContainer <T>((T)model));
                    });
                }
                return((T)model);
            }
            model.Fetch(onSuccess, onFailure, null, disableCache);
            return((T)model);
        }
示例#3
0
        protected virtual void PostOrPut(Action <ApiContainer> onSuccess, Action <ApiContainer> onFailure, PostOrPutSelect select, Dictionary <string, object> requestParams = null)
        {
            if (string.IsNullOrEmpty(Endpoint))
            {
                Logger.LogErrorFormat(DebugLevel.API, "Cannot save to null endpoint");
            }
            else
            {
                if (requestParams == null)
                {
                    requestParams = ExtractApiFields();
                }
                if (APIUser.CurrentUser == null || !APIUser.CurrentUser.hasSuperPowers)
                {
                    List <KeyValuePair <string, object> > list = (from kvp in requestParams
                                                                  where IsAdminWritableOnly(FindProperty(kvp.Key))
                                                                  select kvp).ToList();
                    foreach (KeyValuePair <string, object> item in list)
                    {
                        requestParams.Remove(item.Key);
                    }
                }
                List <KeyValuePair <string, object> > list2 = (from kvp in requestParams
                                                               where IsApiWritableOnly(FindProperty(kvp.Key))
                                                               select kvp).ToList();
                foreach (KeyValuePair <string, object> item2 in list2)
                {
                    requestParams.Remove(item2.Key);
                }
                Action <ApiContainer> onSuccess2 = delegate(ApiContainer c)
                {
                    ApiCache.Save(c.Model.id, c.Model, andClone: true);
                    if (onSuccess != null)
                    {
                        onSuccess(c);
                    }
                };
                switch (select)
                {
                case PostOrPutSelect.Auto:
                    if (!string.IsNullOrEmpty(id))
                    {
                        SendPutRequest(new ApiContainer
                        {
                            OnSuccess = onSuccess2,
                            OnError   = onFailure,
                            Model     = this
                        }, requestParams);
                    }
                    else
                    {
                        API.SendPostRequest(Endpoint, MakeModelContainer(onSuccess2, onFailure), requestParams);
                    }
                    break;

                case PostOrPutSelect.Post:
                    API.SendPostRequest(Endpoint, MakeModelContainer(onSuccess2, onFailure), requestParams);
                    break;

                case PostOrPutSelect.Put:
                {
                    ApiModel target = null;
                    if (ApiCache.Fetch(GetType(), id + "_copy", ref target, 3.40282347E+38f))
                    {
                        foreach (KeyValuePair <string, object> item3 in target.ExtractApiFields())
                        {
                            if (requestParams.ContainsKey(item3.Key))
                            {
                                if (typeof(IList).IsAssignableFrom(item3.Value.GetType()) && typeof(IList).IsAssignableFrom(requestParams[item3.Key].GetType()))
                                {
                                    IList a = item3.Value as IList;
                                    IList b = requestParams[item3.Key] as IList;
                                    if (!b.Cast <object>().Any((object bo) => !a.Contains(bo)) && !a.Cast <object>().Any((object ao) => !b.Contains(ao)))
                                    {
                                        requestParams.Remove(item3.Key);
                                    }
                                }
                                else if (item3.Value.Equals(requestParams[item3.Key]))
                                {
                                    requestParams.Remove(item3.Key);
                                }
                            }
                        }
                    }
                    SendPutRequest(new ApiContainer
                        {
                            OnSuccess = onSuccess2,
                            OnError   = onFailure,
                            Model     = this
                        }, requestParams);
                    break;
                }
                }
            }
        }