Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
guibranco committed Feb 15, 2019
2 parents 0cf9726 + 4845ab9 commit de45d7f
Show file tree
Hide file tree
Showing 10 changed files with 396 additions and 1 deletion.
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
# ViaCEP
The ViaCEP web service client for .NET projects
The [ViaCEP](https://viacep.com.br) web service client for .NET projects

[![Build status](https://ci.appveyor.com/api/projects/status/9jnsy1e08jhyxl7j?svg=true)](https://ci.appveyor.com/project/guibranco/viacep)
[![LatLongNet NuGet Version](https://img.shields.io/nuget/v/ViaCEP.svg?style=flat)](https://www.nuget.org/packages/ViaCEP/)
[![LatLongNet NuGet Downloads](https://img.shields.io/nuget/dt/ViaCEP.svg?style=flat)](https://www.nuget.org/packages/ViaCEP/)
[![Github All Releases](https://img.shields.io/github/downloads/guibranco/ViaCEP/total.svg?style=flat)](https://github.com/guibranco/ViaCEP)
![Last release](https://img.shields.io/github/release-date/guibranco/viacep.svg?style=flat)

<img src="https://raw.githubusercontent.com/guibranco/ViaCEP/master/logo.png" alt="ViaCEP" width="287" height="72">


The ViaCEP service is exclusive for Brazil! No zipcode data available for other countries (Feb/2019)!

---
NuGet package: https://www.nuget.org/packages/ViaCEP

```
Install-Package ViaCEP
```

---
## Usage

The package has two classes:
- ViaCEPClient - The main class (methods)
- ViaCEPResult - The result class (data)

## Querying by zip code / postal code (single result)

```cs
var result = ViaCEPClient.Search("01234567"); //searches for the postal code 01234-567
var address = result.Address;
var neighborhood = result.Neighborhood
//do what you need with 'result' instance of ViaCEPResult.
```


## Querying by addres (list result)

```cs
var results = ViaCEPClient.Search("SP", "São Paulo", "Avenida Paulista"); //search for the Avenida Paulista in São Paulo / SP
foreach(var result in results){
var address = result.Address;
var neighborhood = result.Neighborhood;
var zipCode = result.ZipCode;
//do what you need with 'resul' instance of ViaCEPResult.
}
```
28 changes: 28 additions & 0 deletions ViaCEP.Tests/AddressTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace ViaCEP.Tests
{
using System.Linq;
using Xunit;

/// <summary>
/// The address tests class.
/// </summary>
public class AddressTests
{
/// <summary>
/// Validates the search by full address.
/// </summary>
[Fact]
public void ValidateSearchByFullAddress()
{
var result = ViaCEPClient.Search("SP", "São Paulo", "Avenida Paulista");
Assert.NotNull(result);
var list = result.ToList();
Assert.True(list.Any());
var first = list.First();
Assert.Equal("SP", first.StateInitials);
Assert.Equal("São Paulo", first.City);
Assert.Equal("01310-905", first.ZipCode);

}
}
}
19 changes: 19 additions & 0 deletions ViaCEP.Tests/ViaCEP.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ViaCEP\ViaCEP.csproj" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions ViaCEP.Tests/ZipCodeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace ViaCEP.Tests
{
using Xunit;

/// <summary>
/// The zip code tests class.
/// </summary>
public class ZipCodeTests
{
/// <summary>
/// Validates the search by zip code.
/// </summary>
[Fact]
public void ValidateSearchByZipCode()
{
var result = ViaCEPClient.Search("03177010");
Assert.NotNull(result);
Assert.Equal("Rua Doutor João Batista de Lacerda", result.Street);
Assert.Equal("Quarta Parada", result.Neighborhood);
Assert.Equal("São Paulo", result.City);
Assert.Equal("SP", result.StateInitials);
}
}
}
31 changes: 31 additions & 0 deletions ViaCEP.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.421
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ViaCEP", "ViaCEP\ViaCEP.csproj", "{A251D320-410E-48F1-889D-9F59EB8CFEB9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ViaCEP.Tests", "ViaCEP.Tests\ViaCEP.Tests.csproj", "{D9FD0AA7-A4C5-43A2-820F-827BD503B52B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A251D320-410E-48F1-889D-9F59EB8CFEB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A251D320-410E-48F1-889D-9F59EB8CFEB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A251D320-410E-48F1-889D-9F59EB8CFEB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A251D320-410E-48F1-889D-9F59EB8CFEB9}.Release|Any CPU.Build.0 = Release|Any CPU
{D9FD0AA7-A4C5-43A2-820F-827BD503B52B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D9FD0AA7-A4C5-43A2-820F-827BD503B52B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9FD0AA7-A4C5-43A2-820F-827BD503B52B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9FD0AA7-A4C5-43A2-820F-827BD503B52B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AC1987AC-4E78-4CCD-84BC-543F3D7426B4}
EndGlobalSection
EndGlobal
92 changes: 92 additions & 0 deletions ViaCEP/VIaCEPResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
namespace ViaCEP
{
using Newtonsoft.Json;
using System;

/// <summary>
/// The Via CEP result class.
/// </summary>
public sealed class ViaCEPResult
{
/// <summary>
/// Gets or sets the zip code.
/// </summary>
/// <value>
/// The zip code.
/// </value>
[JsonProperty("cep")]
public String ZipCode { get; set; }

/// <summary>
/// Gets or sets the street.
/// </summary>
/// <value>
/// The street.
/// </value>
[JsonProperty("logradouro")]
public String Street { get; set; }

/// <summary>
/// Gets or sets the complement.
/// </summary>
/// <value>
/// The complement.
/// </value>
[JsonProperty("complemento")]
public String Complement { get; set; }

/// <summary>
/// Gets or sets the neighborhood.
/// </summary>
/// <value>
/// The neighborhood.
/// </value>
[JsonProperty("bairro")]
public String Neighborhood { get; set; }

/// <summary>
/// Gets or sets the city.
/// </summary>
/// <value>
/// The city.
/// </value>
[JsonProperty("localidade")]
public String City { get; set; }

/// <summary>
/// Gets or sets the state initials.
/// </summary>
/// <value>
/// The state initials.
/// </value>
[JsonProperty("uf")]
public String StateInitials { get; set; }

/// <summary>
/// Gets or sets the unit.
/// </summary>
/// <value>
/// The unit.
/// </value>
[JsonProperty("unidade")]
public String Unit { get; set; }

/// <summary>
/// Gets or sets the ibge code.
/// </summary>
/// <value>
/// The ibge code.
/// </value>
[JsonProperty("ibge")]
public Int32 IBGECode { get; set; }

/// <summary>
/// Gets or sets the gia code.
/// </summary>
/// <value>
/// The gia code.
/// </value>
[JsonProperty("gia")]
public Int32 GIACode { get; set; }
}
}
23 changes: 23 additions & 0 deletions ViaCEP/ViaCEP.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Guilherme Branco Stracini</Authors>
<Description>The ViaCEP WebService client for .NET projects</Description>
<Copyright>© 2019 Guilherme Branco Stracini. All rights reserved.</Copyright>
<PackageLicenseUrl>https://github.com/guibranco/ViaCEP/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/guibranco/ViaCEP/</PackageProjectUrl>
<RepositoryUrl>https://github.com/guibranco/ViaCEP</RepositoryUrl>
<RepositoryType>GIT</RepositoryType>
<PackageReleaseNotes>The first release</PackageReleaseNotes>
<PackageTags>cep zipcode postalcode postal zip address location api webservice brasil brazil correios ibge gia mf fazenda</PackageTags>
<PackageIconUrl>https://github.com/guibranco/ViaCEP/blob/master/logo.png</PackageIconUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>

</Project>
90 changes: 90 additions & 0 deletions ViaCEP/ViaCEPClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
namespace ViaCEP
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// The Via CEP client class.
/// </summary>
public static class ViaCEPClient
{
#region Private fields

/// <summary>
/// The base URL
/// </summary>
private const String BaseUrl = "https://viacep.com.br/ws/";

#endregion


#region Public methods

/// <summary>
/// Searches the specified zip code.
/// </summary>
/// <param name="zipCode">The zip code.</param>
/// <returns></returns>
public static ViaCEPResult Search(String zipCode)
{
return SearchAsync(zipCode, CancellationToken.None).Result;
}

/// <summary>
/// Searches the asynchronous.
/// </summary>
/// <param name="zipCode">The zip code.</param>
/// <param name="token">The token.</param>
/// <returns></returns>
public static async Task<ViaCEPResult> SearchAsync(String zipCode, CancellationToken token)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(BaseUrl);
var response = await client.GetAsync($"{zipCode}/json", token).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsAsync<ViaCEPResult>(token).ConfigureAwait(false);
}
}

/// <summary>
/// Searches the specified state initials.
/// </summary>
/// <param name="stateInitials">The state initials.</param>
/// <param name="city">The city.</param>
/// <param name="address">The address.</param>
/// <returns></returns>
public static IEnumerable<ViaCEPResult> Search(String stateInitials, String city, String address)
{
return SearchAsync(stateInitials, city, address, CancellationToken.None).Result;
}

/// <summary>
/// Searches the asynchronous.
/// </summary>
/// <param name="stateInitials">The state initials.</param>
/// <param name="city">The city.</param>
/// <param name="address">The address.</param>
/// <param name="token">The token.</param>
/// <returns></returns>
public static async Task<IEnumerable<ViaCEPResult>> SearchAsync(
String stateInitials,
String city,
String address,
CancellationToken token)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(BaseUrl);
var response = await client.GetAsync($"{stateInitials}/{city}/{address}/json", token).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsAsync<List<ViaCEPResult>>(token).ConfigureAwait(false);
}
}

#endregion
}
}
Loading

0 comments on commit de45d7f

Please sign in to comment.