Beispiel #1
0
 public void EnsureValid()
 {
     if (ContainsKey("error"))
     {
         Debug.WriteLine(ToString());
         if (ContainsKey("message"))
         {
             throw new Exception(s("message"));
         }
         else
         {
             throw new Exception(s("error"));
         }
     }
     else if (ContainsKey("errors"))
     {
         DictModel errors  = d("errors");
         string    subject = null;
         foreach (var s in errors.Keys)
         {
             subject = s;
             break;
         }
         var    compaints      = errors.l(subject);
         string firstComplaint = compaints [0].ToString();
         string error          = subject [0].ToString().ToUpper() + ((subject.Length > 1) ? subject.Substring(1, subject.Length - 1) : "") + " " + firstComplaint + ".";
         throw new Exception(error);
     }
     else if (ContainsKey("status_code") && !b("status_code"))
     {
         Debug.WriteLine(ToString());
         throw new Exception("Something went wrong.");
     }
     Remove("status_code");
 }
Beispiel #2
0
        private async Task <HttpResponseMessage> PutAsync(string requestUri, DictModel parameters)
        {
            parameters = parameters ?? new DictModel();
            var content = parameters.ToContent();

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            return(await client.PutAsync(requestUri, content));
        }
Beispiel #3
0
        /*---------- REST ----------*/
        private async Task <HttpResponseMessage> GetAsync(string requestUri, DictModel parameters)
        {
            parameters = parameters ?? new DictModel();

            requestUri += "?";

            foreach (var entry in parameters)
            {
                requestUri += String.Format("&{0}={1}", entry.Key, entry.Value.ToString());
            }
            Debug.WriteLine(requestUri);
            return(await client.GetAsync(requestUri));
        }
Beispiel #4
0
        /*---------- BOTTLENECK ----------*/
        protected async Task <DictModel> MakeRequestAsync(string requestUri, DictModel parameters, RequestMethod method)
        {
            int ticket = nextTicket++;

            while (ticket != currentlyServing)
            {
                await Task.Yield();

                Debug.WriteLine("Ticket: " + ticket + ", nextTicket: " + nextTicket + ", currentlyServing: " + currentlyServing + ", request: " + requestUri);
            }
            Debug.WriteLine("Running ticket: " + ticket + ", request: " + method + " " + requestUri);

            HttpResponseMessage response = null;

            try {
                switch (method)
                {
                case RequestMethod.GET:         response = await GetAsync(requestUri, parameters);             break;

                case RequestMethod.POST:        response = await PostAsync(requestUri, parameters);    break;

                case RequestMethod.PUT:         response = await PutAsync(requestUri, parameters);             break;

                case RequestMethod.DELETE:      response = await DeleteAsync(requestUri);                              break;
                }
            } catch (Exception ex) {
                currentlyServing++;
                throw ex;
            }

            currentlyServing++;
            DictModel dict = null;

            try {
                dict = await response.Content.ReadAsStringAsync();
            } catch {
                dict = new DictModel();
            }
            dict.Add("status_code", response.IsSuccessStatusCode);
            return(dict);
        }
Beispiel #5
0
 public DictModel(string rootName)
 {
     root = new DictModel();
     base.Add(rootName, root);
 }