Exemplo n.º 1
0
        private async Task <string> PostDataAsyncAsJson(IApiFunction data = null)
        {
            using (HttpClient client = new HttpClient())
            {
                var response = await client.PostAsync(
                    CreateUriFromMethod(data.FunctionName),
                    new StringContent(
                        data.GetJsonString(),
                        Encoding.UTF8,
                        "application/json"));

                return(await response.Content.ReadAsStringAsync());
            }
        }
Exemplo n.º 2
0
        public async Task <string> PostDataAsync(IApiFunction data = null)
        {
#if UseSendAsync
            var request = new HttpRequestMessage(HttpMethod.Post, CreateUriFromMethod(data.FunctionName, false));
            if (data != null)
            {
                var apiParams = new List <KeyValuePair <string, string> >();
                apiParams.Add(new KeyValuePair <string, string>("access_token", ConfigurationManager.AccessToken));
                apiParams.Add(new KeyValuePair <string, string>("arg", data.GetJsonString()));
                request.Content = new FormUrlEncodedContent(apiParams);
            }
            return(await SendAsync(request));
#else
#if UseSendAsJson
            return(await PostDataAsyncAsJson(data));
#else
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(ConfigurationManager.ParticleApiUrl);
                client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue()
                {
                    NoCache = true
                };

                var apiParams = new List <KeyValuePair <string, string> >();
                apiParams.Add(new KeyValuePair <string, string>("access_token", ConfigurationManager.AccessToken));
                apiParams.Add(new KeyValuePair <string, string>("arg", data.GetJsonString()));
                var content = new FormUrlEncodedContent(apiParams);

                var response = await client.PostAsync("v1/devices/" + ConfigurationManager.DeviceId + "/" + data.FunctionName, content);

                return(await response.Content.ReadAsStringAsync());
            }
#endif
#endif
        }
Exemplo n.º 3
0
        private static string Execute(PhotonClient client, IApiFunction func, bool waitForKey = true)
        {
            Console.WriteLine("".PadRight(75, '-'));
            Console.WriteLine($"Executing '{func.FunctionName}' ({func.GetType().Name})...");
            Console.WriteLine($"  Data: {func.GetJsonString()}");
            if (waitForKey)
            {
                Console.WriteLine("ENTER to continue...");
                Console.ReadKey();
                Console.WriteLine();
            }
            var task = client.PostDataAsync(func);

            task.Wait();
            Console.WriteLine($"Finished, result: \r\n  {task.Result}");
            //if (waitForKey)
            //{
            //    Console.WriteLine("ENTER to continue...");
            //    Console.ReadKey();
            //    Console.WriteLine();
            //}
            return(task.Result);
        }