Skip to content

Instantly share code, notes, and snippets.

View MichalGrzegorzak's full-sized avatar

Michal Grzegorzak MichalGrzegorzak

  • UK
View GitHub Profile
@MichalGrzegorzak
MichalGrzegorzak / AzureDurableFunctions.cs
Created November 23, 2023 15:03
Purge history on local
npm install -g azure-functions-core-tools
// Then open a command prompt in the root directory of your Azure Functions project. The Azure Core Tools requires the host.json file from your project to identify your orchestration instances.
func durable purge-history
// to list all possible actions
// func durable
public async Task<byte[]> GenerateZipFileAsync()
{
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
await AddFileToZipAsync(archive, "HzImpAccountsT.csv", file.Accounts);
await AddFileToZipAsync(archive, "HzImpAcctContactsT.csv", file.Account_Contacts);
}
@MichalGrzegorzak
MichalGrzegorzak / WTClient.cs
Created October 18, 2023 09:24
CreateAndInitHttpClient Basic auth
private HttpClient CreateAndInitHttpClient(IHttpClientFactory httpClientFactory, WorktribeClientConfig config)
{
if (httpClientFactory == null)
throw new ArgumentNullException(nameof(httpClientFactory));
if (string.IsNullOrWhiteSpace(config.BaseUrl))
throw new ArgumentNullException("config.BaseUrl");
if (string.IsNullOrWhiteSpace(config.Username))
throw new ArgumentNullException("config.Username");
if (string.IsNullOrWhiteSpace(config.Password))
throw new ArgumentNullException("config.Password");
@MichalGrzegorzak
MichalGrzegorzak / BaseTestWithConfiguration.cs
Created October 16, 2023 12:29
Read configuration from UnitTest .netCore
[TestClass]
public class BaseTestWithConfiguration
{
/// <summary>
/// Reads values from local.settings of the main project
/// </summary>
static void ConfigureEnvironmentVariablesFromLocalSettings()
{
var path = Path.GetDirectoryName(typeof(Startup).Assembly.Location);
var json = File.ReadAllText(Path.Join(path, "local.settings.json"));
@MichalGrzegorzak
MichalGrzegorzak / StartupExtensions.cs
Created September 30, 2023 09:09
AddConfigOptionsFromConfigSection
internal static IServiceCollection AddConfigOptionsFromConfigSection<T>(this IServiceCollection services, string configSectionName) where T : class
{
services.AddOptions<T>().Configure<IConfiguration>((settings, configuration) =>
{
configuration.GetSection(configSectionName).Bind(settings);
});
return services;
}
@MichalGrzegorzak
MichalGrzegorzak / ProjectMapper.cs
Last active September 29, 2023 12:35
Renaming duplicated elements
var groupedLines = lines.GroupBy(x => new { x.TRACType, x.Description })
.Where(z => z.Count() > 1)
.Select(x => x);
foreach (var grp in groupedLines)
{
int idx = 1;
foreach (BudgetLine budgetLine in grp)
{
budgetLine.Description = $"{budgetLine.Description} {idx}";
idx++;
@MichalGrzegorzak
MichalGrzegorzak / ExcelBuilderExtensions.cs
Created July 17, 2023 12:37
Reflection, cast prop to List
IEnumerable items = (IEnumerable)propertyInfo.GetValue(customerObject, null);
object first = items.Cast<object>().FirstOrDefault();
@MichalGrzegorzak
MichalGrzegorzak / ExcelBuilderExtensions.cs
Created July 17, 2023 12:33
Reflection, create specific generic type
var baseGenericType = typeof(BaseItemCollection<>);
var genType = tt.GetGenericArguments()[0];
var specificBaseType = baseGenericType.MakeGenericType(genType);
[FunctionName(nameof(GetIP))]
public async Task<IActionResult> GetIP(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "check/getIp")] HttpRequest req, ILogger log)
{
log.LogInformation("GetIP called");
var client = new HttpClient();
var response = await client.GetAsync(@"https://ifconfig.me");
return new OkObjectResult(await response.Content.ReadAsStringAsync());
}
@MichalGrzegorzak
MichalGrzegorzak / RmsSftpIntegrationTest.cs
Created May 25, 2023 12:09
MsUnit before and after test
[TestInitialize]
public void BeforeEachTest()
{
using var ftp = CreateRmsSftpService(sftpConfig.ToOptions());
copiedFilePath = ftp.CopyFile(ftp.TargetFile, $"CPY_{ftp.TargetFile}");
}
[TestCleanup]
public void AfterEachTest()
{
using var ftp = CreateRmsSftpService(sftpConfig.ToOptions());