Beispiel #1
0
        public List <string> GetColors()
        {
            var storedList = storage.Get <List <string> > ("colors");

            if (storedList != null)
            {
                return(storedList);
            }

            List <string> result = new  List <string> ();
            APIResponse <List <Colors> > response = api.CreateRequest <List <Colors> >(
                "colors.get",
                new {}
                );

            if (response != null)
            {
                if (response.error != null)
                {
                    using (var alert = new UIAlertView("Sign Up Error",
                                                       string.Format("Please try again--{0}--Code:{1} ",
                                                                     response.error.message, response.error.code), null, "OK", null))
                    {
                        alert.Show();
                    }
                    //we good TODO
                }
                else if (response.Result != null)
                {
                    result = response.Result.Select(x => x.label).ToList();

                    storage.Put <List <string> > ("colors", result);
                }
            }
            return(result);
        }
Beispiel #2
0
 //process response and cache
 private void processResponse(APIResponse <LoginResult> result)
 {
     AccessToken = api.Token;
     storage.Put("token", api.Token);
 }
Beispiel #3
0
        public APIResponse <T> CreateRequest <T>(string method, object parameters = null)
        {
            APIResponse <T> result = null;

            try{
                Console.WriteLine(method);
                this.request             = (HttpWebRequest)HttpWebRequest.Create(URL);
                this.request.ContentType = "application/json";
                this.request.Timeout     = TimeOutFlag;
                this.request.Method      = "POST";
                this.request.Headers["Authorization-Type"] = "token";

                if (Token != null)
                {
                    this.request.Headers ["Authorization"] = Token;
                }
                var req = new APIRequest {
                    method = method
                };

                ASCIIEncoding encoding   = new ASCIIEncoding();
                string        stringData = req.BuildRequestBody(parameters);
                byte[]        data       = encoding.GetBytes(stringData);

                this.request.ContentLength = data.Length;

                Stream newStream = this.request.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();


                using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
                {
                    if (resp.StatusCode == HttpStatusCode.OK)
                    {
                        CheckToken(resp.Headers[1]);

                        using (StreamReader reader = new StreamReader(resp.GetResponseStream())) {
                            var content = reader.ReadToEnd();
                            if (string.IsNullOrWhiteSpace(content))
                            {
                                Console.Out.WriteLine("Response contained empty body...");

                                return(null);
                            }
                            else
                            {
                                Console.Out.WriteLine("Response Body: \r\n");
                                //Console.Out.WriteLine ("\r\n {0}", content);
                                result = ParseJson <T> (content);
                            }
                        }
                    }
                    else
                    {
                        Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", resp.StatusCode);
                    }
                }
            }catch (Exception exp)
            {
                HandleError <T>(exp);
            }

            return(result);
        }