Skip to content

Instantly share code, notes, and snippets.

@leandromoh
Created June 12, 2021 21:48
Show Gist options
  • Save leandromoh/c450cd1c0ceca4562f89d268f1110642 to your computer and use it in GitHub Desktop.
Save leandromoh/c450cd1c0ceca4562f89d268f1110642 to your computer and use it in GitHub Desktop.
reads json file with array of objects and post them in batches (example: post 6 requests in parallel, each time)
using MoreLinq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
static class Program
{
private static HttpClient _client = new HttpClient();
private static string internalPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
// API
public const string hostAddress = "http://foo.com.br";
public const string endpoint = "/minha-api/api/v2/clients";
public const string token = "TOKEN";
// FILES
public const string sourceFileName = "file.json";
public const string errorFileName = "errors.txt";
// BATCH
public const int batchSize = 6;
static async Task Main(string[] _)
{
_client.BaseAddress = new Uri(hostAddress);
_client.DefaultRequestHeaders.Add("Authorization", token);
var orders = await GetOrders();
var counter = 0;
var erroStream = new StreamWriter(Path.Combine(internalPath, errorFileName));
foreach (var sendOrderRequest in orders.Batch(batchSize))
{
var tasks = sendOrderRequest.Select(x => Send(x));
var responses = await Task.WhenAll(tasks);
for (var i = 0; i < responses.Length; i++)
{
if (responses[i].StatusCode == HttpStatusCode.Created)
{
continue;
}
var obj = new
{
HttpStatus = responses[i].StatusCode,
Body = await responses[i].Content?.ReadAsStringAsync(),
Request = sendOrderRequest.ElementAt(i),
};
var log = JsonConvert.SerializeObject(obj) + "\n\n\n";
await erroStream.WriteLineAsync(log);
await erroStream.FlushAsync();
}
counter += responses.Length;
Console.WriteLine(counter);
}
Console.WriteLine("Finished");
}
private static async Task<HttpResponseMessage> Send(string sendOrderRequest)
{
var httpRequest = new HttpRequestMessage()
{
Method = HttpMethod.Post,
RequestUri = new Uri(endpoint, UriKind.Relative),
Content = new StringContent(
sendOrderRequest,
Encoding.UTF8,
"application/json")
};
var response = await _client.SendAsync(httpRequest);
return response;
}
private static async Task<IEnumerable<string>> GetOrders()
{
var fullMappingPath = Path.Combine(internalPath, sourceFileName);
using var reader = new StreamReader(fullMappingPath);
var fileContent = await reader.ReadToEndAsync();
JArray array = JArray.Parse(fileContent);
return array.Children<JObject>().Select(x => x.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment