Skip to content

Instantly share code, notes, and snippets.

View yanandrey's full-sized avatar
:shipit:

Yan Andrey yanandrey

:shipit:
  • XP Inc.
  • Campinas - SP / Brasil
View GitHub Profile
@yanandrey
yanandrey / SwaggerApi.csproj
Last active February 24, 2022 22:19
Swagger Configuration
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
@yanandrey
yanandrey / AddTokensServices.cs
Created January 15, 2022 00:42
JWT Authentication/Authorization
public static IServiceCollection AddTokensServices(this IServiceCollection services, IConfiguration configuration)
{
var tokenConfig = new TokenConfiguration();
new ConfigureFromConfigurationOptions<TokenConfiguration>(configuration
.GetSection("TokenConfiguration"))
.Configure(tokenConfig);
services.AddSingleton(tokenConfig);
services.AddAuthentication(x =>
{
public static class RandomStringHelper
{
public static string RandomString(int length)
{
const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var res = new StringBuilder();
using (var rng = new RNGCryptoServiceProvider())
{
byte[] uintBuffer = new byte[sizeof(uint)];
public async Task<TResponse> GetAsJsonAsync<TResponse>(string uri)
{
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(uri);
if (!response.IsSuccessStatusCode)
{
throw new Exception();
}
public bool IsPointInPolygon(List<Location> poly, Location point)
{
int i, j;
var c = false;
for (i = 0, j = poly.Count - 1; i < poly.Count; j = i++)
{
if ((poly[i].Lt <= point.Lt && point.Lt < poly[j].Lt
|| poly[j].Lt <= point.Lt && point.Lt < poly[i].Lt)
&& point.Lg < (poly[j].Lg - poly[i].Lg) * (point.Lt - poly[i].Lt)
/ (poly[j].Lt - poly[i].Lt) + poly[i].Lg)
public int GetDistanceStraightLine(double latOrg, double lngOrg, double latDest, double lngDest)
{
const double radius = 6378.137;
var dLat = latDest * Math.PI / 180 - latOrg * Math.PI / 180;
var dLng = lngDest * Math.PI / 180 - lngOrg * Math.PI / 180;
var a = Math.Sin(dLat / 2) *
Math.Sin(dLat / 2) +
Math.Cos(latOrg * Math.PI / 180) *
Math.Cos(latDest * Math.PI / 180) *
public async Task<Location> GetGeocoding(Address address)
{
var apiClient = new HttpClient();
var key = Environment.GetEnvironmentVariable("GOOGLE_KEY");
var addressToUse = BindAddress(address);
var url = "https://maps.googleapis.com/maps/api/geocode/json?" +
$"address={addressToUse}" +
$"&language=pt-BR" +
$"&key={key}";