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()); } }
public void Assigning() { while (true) { while (queue.GetQueue().Count > 0) { Console.WriteLine($"Active Connections:{queue.GetQueue().Count}"); HttpListenerContext httpListenerContext = queue.Dequeue(); Dipatcher dipatcher = new Dipatcher(httpListenerContext); dipatcher.ParseUrl(); if (dipatcher.GetFilename() == "favicon.ico") { continue; } Response response = new Response(httpListenerContext); var domainDictionary = domainPathStorage.GetDomainsAndPaths(); if (httpListenerContext.Request.HttpMethod == "POST") { PostHandler postHandler = new PostHandler(httpListenerContext); if (domainDictionary[dipatcher.GetDomain()] == dipatcher.GetFilename()) { ApiFactory apiFactory = new ApiFactory(dipatcher.GetFilename()); IApiFunction api = apiFactory.ApiFunctionFactory(); var answer = api.Compute(postHandler.jObject); response.SendReponse(postHandler.ConvertJsonObject(answer)); } else { httpListenerContext.Response.StatusCode = 404; JObject answer = new JObject(); answer["API"] = "Notfound"; response.SendReponse(postHandler.ConvertJsonObject(answer)); } } else { FileHandler fileHandler = new FileHandler(domainDictionary[dipatcher.GetDomain()], dipatcher.GetFilename()); response.SendReponse(fileHandler.ConvertFileDataBytes()); } } } }
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); }
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 }