Exemplo n.º 1
0
        public static HttpClient GetHttpClient(NVCEndpoint endpoint, List <string> appendString)
        {
            var iPersistantStorage = ServiceLocator.Instance.Resolve <IPersistantStorage>();
            var facebookToken      = iPersistantStorage.GetFacebookToken();

            Client.Timeout = TimeSpan.FromSeconds(Math.Max(endpoint.TimeoutSections, 20));
            Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", facebookToken);

            if (!Client.DefaultRequestHeaders.Accept.Contains(MediaTypeWithQualityHeaderValueJson))
            {
                Client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValueJson);
            }

            StringBuilder stringBuilder = new StringBuilder(NotSensitive.SystemUrls.base_url + endpoint.Controller + endpoint.URL);

            if (appendString != null && appendString.Count > 0)
            {
                stringBuilder.Append("/");

                foreach (String str in appendString)
                {
                    stringBuilder.Append(str);
                    stringBuilder.Append("/");
                }
            }

            Client.BaseAddress = new Uri(stringBuilder.ToString());

            return(Client);
        }
Exemplo n.º 2
0
            public static async Task UpdateUser(double lat, double lon)
            {
                //cache so we dont send teh same one again and again. just keep last one.
                if (lat == LastLat && lon == LastLon)
                {
                    return;
                }

                LastLat = lat;
                LastLon = lon;

                Dictionary <string, object> requestDictionary = new Dictionary <string, object>();

                requestDictionary.Add("lat", lat);
                requestDictionary.Add("lng", lon);

                NVCEndpoint endpoint = new NVCEndpoint();

                endpoint.Method     = NVCEndpoint.EndpointType.Put;
                endpoint.Controller = Mapping;

                await NVCRestHelper.Async(endpoint, requestDictionary, null);

                System.Diagnostics.Debug.WriteLine("User location updated on server: lat: " + lat + " lon: " + lon);
            }
Exemplo n.º 3
0
            public static async Task GetTranactions(List <Card> itemsToIgnore)
            {
                NVCEndpoint endpoint = new NVCEndpoint();

                endpoint.Method     = NVCEndpoint.EndpointType.Get;
                endpoint.Controller = Mapping;

                var data = await NVCRestHelper.Async(endpoint, new Dictionary <string, object>(), null);

                if (!data.Sucessful)
                {
                    return;
                }

                var token = data.ReturnedData;

                if (token == null)
                {
                    return;
                }

                var transactions = token["transactions"] as JArray;

                if (transactions == null)
                {
                    return;
                }

                foreach (var transactionToken in transactions)
                {
                    ParseTransaction(transactionToken, itemsToIgnore);
                }
            }
Exemplo n.º 4
0
            public static async Task <NVCRestResult> CreateUser()
            {
                NVCEndpoint endpoint = new NVCEndpoint();

                endpoint.Method     = NVCEndpoint.EndpointType.PostRaw;
                endpoint.Controller = Mapping;

                return(await NVCRestHelper.Async(endpoint, new Dictionary <string, object>(), null));
            }
Exemplo n.º 5
0
            public static async Task UpdateUser(string oneSignalId)
            {
                Dictionary <string, object> requestDictionary = new Dictionary <string, object>();

                requestDictionary.Add("oneSignalId", oneSignalId);

                NVCEndpoint endpoint = new NVCEndpoint();

                endpoint.Method     = NVCEndpoint.EndpointType.Put;
                endpoint.Controller = Mapping;

                await NVCRestHelper.Async(endpoint, requestDictionary, null);
            }
Exemplo n.º 6
0
            public static async Task <NVCRestResult> SendValidationCode(string email)
            {
                NVCEndpoint endpoint = new NVCEndpoint();

                endpoint.Method     = NVCEndpoint.EndpointType.PostRaw;
                endpoint.Controller = Mapping;

                Dictionary <string, object> requestDictionary = new Dictionary <string, object>();

                requestDictionary.Add("email", email);

                return(await NVCRestHelper.Async(endpoint, requestDictionary, null));
            }
Exemplo n.º 7
0
        public static async Task <HttpResponseMessage> PostRaw(NVCEndpoint endpoint, Dictionary <string, object> Parameters, List <String> appendString)
        {
            var client = GetHttpClient(endpoint, appendString);

            if (Parameters == null)
            {
                return(await client.PostAsync(endpoint.URL, null));
            }

            var content = new StringContent(JsonConvert.SerializeObject(Parameters), Encoding.UTF8, applicationjson);

            return(await client.PostAsync(endpoint.URL, content));
        }
Exemplo n.º 8
0
            public static async Task TerminateTransaction(string transactionId)
            {
                List <string> appendString = new List <string>()
                {
                    transactionId, "terminate"
                };

                NVCEndpoint endpoint = new NVCEndpoint();

                endpoint.Method     = NVCEndpoint.EndpointType.PostRaw;
                endpoint.Controller = Mapping;

                await NVCRestHelper.Async(endpoint, null, appendString);
            }
Exemplo n.º 9
0
        public static async Task <HttpResponseMessage> PostXWWWFormUrlEncoded(NVCEndpoint endpoint, Dictionary <string, object> Parameters, List <String> appendString)
        {
            var client = GetHttpClient(endpoint, appendString);
            Dictionary <String, String> dictToSend = new Dictionary <string, string>();

            foreach (KeyValuePair <string, object> entry in Parameters)
            {
                if (entry.Value != null)
                {
                    dictToSend.Add(entry.Key, entry.Value.ToString());
                }
            }
            return(await client.PostAsync(endpoint.URL, new FormUrlEncodedContent(dictToSend)));
        }
Exemplo n.º 10
0
        //POST
        public static async Task <HttpResponseMessage> Post(NVCEndpoint endpoint, Dictionary <string, object> Parameters, List <String> appendString)
        {
            var client = GetHttpClient(endpoint, appendString);

            string      contentString = String.Empty;
            HttpContent contentHttp   = null;

            if (Parameters != null)
            {
                contentString = JsonConvert.SerializeObject(Parameters).ToString();
                contentHttp   = new StringContent(contentString, Encoding.UTF8, applicationjson);
            }

            return(await client.PostAsync(endpoint.URL, contentHttp));
        }
Exemplo n.º 11
0
            public static async Task <string> CreateTransaction(double lat, double lng, Card card, string userName)
            {
                //replace the card name w/ the users name
                var obj = card.ToJObject();

                obj["name"] = userName;
                obj["uuid"] = Guid.NewGuid().ToString();

                Dictionary <string, object> requestDictionary = new Dictionary <string, object>();

                requestDictionary.Add("lat", lat);
                requestDictionary.Add("lng", lng);
                requestDictionary.Add("distance", 1);
                requestDictionary.Add("card", obj);

                NVCEndpoint endpoint = new NVCEndpoint();

                endpoint.Method     = NVCEndpoint.EndpointType.PostRaw;
                endpoint.Controller = Mapping;

                var data = await NVCRestHelper.Async(endpoint, requestDictionary, null);

                if (!data.Sucessful)
                {
                    return(null);
                }

                var token = data.ReturnedData;

                if (token["id"] == null)
                {
                    return(null);
                }


                var transactionId = (string)token["id"];

                return(transactionId);
            }
Exemplo n.º 12
0
        public static async Task <HttpResponseMessage> PostFormData(NVCEndpoint endpoint, Dictionary <string, object> Parameters, List <String> appendString)
        {
            var client = GetHttpClient(endpoint, appendString);
            MultipartFormDataContent content = new MultipartFormDataContent("boundary=---------------------------14737809831466499882746641449");

            foreach (KeyValuePair <String, object> entry in Parameters)
            {
                if (entry.Value is string)
                {
                    string key = entry.Key;
                    string val = entry.Value.ToString();
                    content.Add(new StringContent(val), key);
                }
                else
                {
                    var imageContent = new ByteArrayContent((byte[])entry.Value);
                    imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
                    content.Add(imageContent, "file", "image.jpg");
                }
            }
            return(await client.PostAsync(endpoint.URL, content));
        }
Exemplo n.º 13
0
        //GET
        public static async Task <HttpResponseMessage> Get(NVCEndpoint endpoint, Dictionary <string, object> Parameters, List <String> appendString)
        {
            var client = GetHttpClient(endpoint, appendString);

            if (Parameters != null)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (KeyValuePair <String, object> entry in Parameters)
                {
                    stringBuilder.Append(entry.Key);
                    stringBuilder.Append("=");
                    stringBuilder.Append(entry.Value);
                    stringBuilder.Append("&");
                }

                if (stringBuilder.Length > 0)
                {
                    stringBuilder.Remove(stringBuilder.Length - 1, 1);
                    client.BaseAddress = new Uri(client.BaseAddress + "?" + stringBuilder);
                }
            }

            return(await client.GetAsync(endpoint.URL));
        }
Exemplo n.º 14
0
        public static async Task <NVCRestResult> Async(NVCEndpoint endpoint, Dictionary <string, object> parameters, List <string> appendString)
        {
            HttpResponseMessage contentsTask = null;

            try
            {
                switch (endpoint.Method)
                {
                case NVCEndpoint.EndpointType.Post:
                    contentsTask = await Post(endpoint, parameters, appendString);

                    break;

                case NVCEndpoint.EndpointType.PostXWWWFormUrlEncoded:
                    contentsTask = await PostXWWWFormUrlEncoded(endpoint, parameters, appendString);

                    break;

                case NVCEndpoint.EndpointType.PostRaw:
                    contentsTask = await PostRaw(endpoint, parameters, appendString);

                    break;

                case NVCEndpoint.EndpointType.Get:
                    contentsTask = await Get(endpoint, parameters, appendString);

                    break;

                case NVCEndpoint.EndpointType.Put:
                    contentsTask = await Put(endpoint, parameters, appendString);

                    break;

                default:
                    break;
                }

                if (contentsTask == null)
                {
                    return(new NVCRestResult(false));
                }

                NVCRestResult restResult = new NVCRestResult(contentsTask.IsSuccessStatusCode);
                restResult.StatusCode = contentsTask.StatusCode;

                if (restResult.Sucessful)
                {
                    string result = contentsTask.Content.ReadAsStringAsync().Result;
                    if (!String.IsNullOrEmpty(result))
                    {
                        restResult.ReturnedData = JObject.Parse(contentsTask.Content.ReadAsStringAsync().Result);
                    }
                }

                if (restResult.StatusCode == HttpStatusCode.Unauthorized)
                {
                    ServiceLocator.Instance.Resolve <IBroadcastNotificaion>()?.SendNotificaion(UnauthorizedExceptionThrown);
                }


                return(restResult);
            }
            catch (Exception exception)
            {
                System.Diagnostics.Debug.WriteLine(exception.Message);
                throw exception;
            }
        }