protected override bool Validate(bool success, Func <byte[]> readData, Func <string> readTextData) { if (base.Model == null) { return(false); } if (!base.Validate(success, readData, readTextData)) { return(false); } try { if (!base.Model.SetApiFieldsFromJson(base.ResponseDictionary, ref responseError)) { return(false); } } catch (Exception ex) { base.Error = "An exception was caught when filling the model: " + ex.Message; return(false); IL_0063 :; } if (!string.IsNullOrEmpty(base.Model.id)) { ApiCache.Save(base.Model.id, base.Model, andClone : true); } return(true); }
public void SaveReleaseStatus(Action <ApiContainer> onSuccess, Action <ApiContainer> onFailure) { if (string.IsNullOrEmpty(base.Endpoint)) { Debug.LogError((object)"Cannot save to null endpoint"); } else if (string.IsNullOrEmpty(base.id)) { Debug.LogError((object)"Cannot save release status with no id"); } else { Dictionary <string, object> dictionary = new Dictionary <string, object>(); dictionary.Add("id", base.id); dictionary.Add("releaseStatus", releaseStatus); Dictionary <string, object> parameters = dictionary; Action <ApiContainer> onSuccess2 = delegate(ApiContainer c) { ApiCache.Save(c.Model.id, c.Model, andClone: true); if (onSuccess != null) { onSuccess(c); } }; Put(onSuccess2, onFailure, parameters); } }
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); }
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; } } } }
public virtual void Get(Action <ApiContainer> onSuccess = null, Action <ApiContainer> onFailure = null, Dictionary <string, object> parameters = null, bool disableCache = false) { if (string.IsNullOrEmpty(id) && string.IsNullOrEmpty(Endpoint)) { if (onFailure != null) { onFailure(new ApiContainer { Error = "Fetch called with null id." }); } } else { string key = MakeRequestEndpoint() + Json.Encode(parameters); if (activeRequests.ContainsKey(key)) { ApiContainer apiContainer = activeRequests[key]; Action <ApiContainer> originalSuccess = apiContainer.OnSuccess; Action <ApiContainer> onSuccess2 = delegate(ApiContainer c) { if (activeRequests.ContainsKey(key)) { activeRequests.Remove(key); } try { if (onSuccess != null) { onSuccess(c); } } catch (Exception ex4) { Debug.LogException(ex4); } if (originalSuccess != null) { originalSuccess(c); } }; Action <ApiContainer> originalError = apiContainer.OnError; Action <ApiContainer> onError = delegate(ApiContainer c) { if (activeRequests.ContainsKey(key)) { activeRequests.Remove(key); } try { if (onFailure != null) { onFailure(c); } } catch (Exception ex3) { Debug.LogException(ex3); } if (originalError != null) { originalError(c); } }; apiContainer.OnSuccess = onSuccess2; apiContainer.OnError = onError; } else { Action <ApiContainer> onSuccess3 = delegate(ApiContainer c) { if (activeRequests.ContainsKey(key)) { activeRequests.Remove(key); } try { if (onSuccess != null) { onSuccess(c); } } catch (Exception ex2) { Debug.LogException(ex2); } ApiCache.Save(c.Model.id, c.Model, andClone: true); }; Action <ApiContainer> onFailure2 = delegate(ApiContainer c) { if (activeRequests.ContainsKey(key)) { activeRequests.Remove(key); } try { if (onFailure != null) { onFailure(c); } } catch (Exception ex) { Debug.LogException(ex); } }; ApiContainer apiContainer2 = MakeModelContainer(onSuccess3, onFailure2); activeRequests.Add(key, apiContainer2); SendGetRequest(apiContainer2, parameters, disableCache); } } }