.NET SDK for Grula Pricing Intelligence Platform API.
$ dotnet add package Grula.PricingIntelligencePlatform.SdkA .NET SDK for the Grula Pricing Intelligence Platform API, providing easy access to pricing policies, drivers, and price calculations.
Install the package via NuGet Package Manager:
dotnet add package Grula.PricingIntelligencePlatform.Sdk
Or via Package Manager Console:
Install-Package Grula.PricingIntelligencePlatform.Sdk
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Net.Http.Headers;
using Grula.PricingIntelligencePlatform.Sdk;
// In Program.cs or Startup.cs
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHttpClient("GrulaApi", client =>
{
client.BaseAddress = new Uri("https://api.grula.net");
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", builder.Configuration["GrulaApi:ApiKey"]);
})
.AddTypedClient((httpClient, serviceProvider) =>
{
var baseUrl = "https://api.grula.net"; // or get from configuration
return new GrulaApiClient(baseUrl, httpClient);
});
var host = builder.Build();
public class PricingService
{
private readonly GrulaApiClient _grulaClient;
public PricingService(GrulaApiClient grulaClient)
{
_grulaClient = grulaClient;
}
public async Task<decimal> GetProductPriceAsync(string product, string region, string currency = "USD")
{
var priceQuery = new GetPriceByPriceDriversQuery
{
EnvironmentId = Guid.Parse("your-environment-id"),
CurrencyThreeLetterCode = currency,
PricingDate = DateTime.UtcNow,
PriceDrivers = new[]
{
new PriceDriver { Name = "Product", Value = product },
new PriceDriver { Name = "Region", Value = region }
}
};
var price = await _grulaClient.GetPriceByPriceDriversAsync(priceQuery);
return (decimal)price.Amount.Amount;
}
public async Task<List<decimal>> GetMultiplePricesAsync(List<(string product, string region, string currency)> requests)
{
var pricesQuery = new GetPricesByPriceDriversQuery
{
EnvironmentId = Guid.Parse("your-environment-id"),
PriceRequests = requests.Select(r => new PriceRequest
{
CurrencyThreeLetterCode = r.currency,
PricingDate = DateTime.UtcNow,
PriceDrivers = new[]
{
new PriceDriver { Name = "Product", Value = r.product },
new PriceDriver { Name = "Region", Value = r.region }
}
}).ToArray()
};
var prices = await _grulaClient.GetPricesByPriceDriversAsync(pricesQuery);
return prices.Select(p => (decimal)p.Amount.Amount).ToList();
}
}
var priceQuery = new GetPriceByPriceDriversQuery
{
EnvironmentId = Guid.Parse("your-environment-id"),
CurrencyThreeLetterCode = "USD",
PricingDate = DateTime.UtcNow,
PriceDrivers = new[]
{
new PriceDriver { Name = "Product", Value = "Premium" },
new PriceDriver { Name = "Region", Value = "US-East" }
}
};
var price = await client.GetPriceByPriceDriversAsync(priceQuery);
Console.WriteLine($"Price: {price.Amount.Amount} {price.Amount.CurrencyThreeLetterCode}");
var pricesQuery = new GetPricesByPriceDriversQuery
{
EnvironmentId = Guid.Parse("your-environment-id"),
PriceRequests = new[]
{
new PriceRequest
{
CurrencyThreeLetterCode = "USD",
PricingDate = DateTime.UtcNow,
PriceDrivers = new[]
{
new PriceDriver { Name = "Product", Value = "Basic" },
new PriceDriver { Name = "Region", Value = "US-West" }
}
},
new PriceRequest
{
CurrencyThreeLetterCode = "EUR",
PricingDate = DateTime.UtcNow,
PriceDrivers = new[]
{
new PriceDriver { Name = "Product", Value = "Premium" },
new PriceDriver { Name = "Region", Value = "EU-Central" }
}
}
}
};
var prices = await client.GetPricesByPriceDriversAsync(pricesQuery);
foreach (var price in prices)
{
Console.WriteLine($"Price: {price.Amount.Amount} {price.Amount.CurrencyThreeLetterCode}");
}
using System.Net.Http.Headers;
using Grula.PricingIntelligencePlatform.Sdk;
var httpClient = new HttpClient
{
BaseAddress = new Uri("https://api.grula.net")
};
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", "your-api-key-here");
var client = new GrulaApiClient("https://api.grula.net", httpClient);
You can configure the HttpClient with additional settings:
var httpClient = new HttpClient
{
BaseAddress = new Uri("https://api.grula.net"),
Timeout = TimeSpan.FromSeconds(30)
};
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", "your-api-key-here");
var client = new GrulaApiClient("https://api.grula.net", httpClient);
Register the client in your DI container:
services.AddHttpClient("GrulaApi", client =>
{
client.BaseAddress = new Uri("https://api.grula.net");
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", configuration["GrulaApi:ApiKey"]);
})
.AddTypedClient((httpClient, serviceProvider) =>
{
var baseUrl = "https://api.grula.net"; // or get from configuration
return new GrulaApiClient(baseUrl, httpClient);
});
The SDK supports logging through the HttpClient:
services.AddHttpClient("GrulaApi", client =>
{
client.BaseAddress = new Uri("https://api.grula.net");
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", configuration["GrulaApi:ApiKey"]);
})
.AddTypedClient((httpClient, serviceProvider) =>
{
var baseUrl = "https://api.grula.net";
return new GrulaApiClient(baseUrl, httpClient);
})
.AddLogger(); // Add logging to the HttpClient
The SDK throws exceptions for HTTP errors. Handle them appropriately:
try
{
var price = await client.GetPriceByPriceDriversAsync(priceQuery);
}
catch (ApiException ex)
{
Console.WriteLine($"API Error: {ex.StatusCode} - {ex.Message}");
}
catch (HttpRequestException ex)
{
Console.WriteLine($"Network Error: {ex.Message}");
}
InvalidOperationException: Unable to resolve service for type 'System.String'
This error occurs when using the generic AddHttpClient<GrulaApiClient>() method. The generated GrulaApiClient requires both a baseUrl string and an HttpClient instance in its constructor.
Solution: Use the factory-based registration pattern shown in the examples above:
services.AddHttpClient("GrulaApi", client =>
{
client.BaseAddress = new Uri("https://api.grula.net");
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", configuration["GrulaApi:ApiKey"]);
})
.AddTypedClient((httpClient, serviceProvider) =>
{
var baseUrl = "https://api.grula.net";
return new GrulaApiClient(baseUrl, httpClient);
});
GetPriceByPriceDriversAsync() - Get a single priceGetPricesByPriceDriversAsync() - Get multiple prices (max 100 requests)This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions: