diff --git a/README.md b/README.md index 9cc0152..cf251cd 100644 --- a/README.md +++ b/README.md @@ -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) + +ViaCEP + + +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. +} +``` \ No newline at end of file diff --git a/ViaCEP.Tests/AddressTests.cs b/ViaCEP.Tests/AddressTests.cs new file mode 100644 index 0000000..555c1e6 --- /dev/null +++ b/ViaCEP.Tests/AddressTests.cs @@ -0,0 +1,28 @@ +namespace ViaCEP.Tests +{ + using System.Linq; + using Xunit; + + /// + /// The address tests class. + /// + public class AddressTests + { + /// + /// Validates the search by full address. + /// + [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); + + } + } +} \ No newline at end of file diff --git a/ViaCEP.Tests/ViaCEP.Tests.csproj b/ViaCEP.Tests/ViaCEP.Tests.csproj new file mode 100644 index 0000000..4ca712d --- /dev/null +++ b/ViaCEP.Tests/ViaCEP.Tests.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp2.2 + + false + + + + + + + + + + + + + diff --git a/ViaCEP.Tests/ZipCodeTests.cs b/ViaCEP.Tests/ZipCodeTests.cs new file mode 100644 index 0000000..3da7b37 --- /dev/null +++ b/ViaCEP.Tests/ZipCodeTests.cs @@ -0,0 +1,24 @@ +namespace ViaCEP.Tests +{ + using Xunit; + + /// + /// The zip code tests class. + /// + public class ZipCodeTests + { + /// + /// Validates the search by zip code. + /// + [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); + } + } +} diff --git a/ViaCEP.sln b/ViaCEP.sln new file mode 100644 index 0000000..79ba416 --- /dev/null +++ b/ViaCEP.sln @@ -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 diff --git a/ViaCEP/VIaCEPResult.cs b/ViaCEP/VIaCEPResult.cs new file mode 100644 index 0000000..aaeddee --- /dev/null +++ b/ViaCEP/VIaCEPResult.cs @@ -0,0 +1,92 @@ +namespace ViaCEP +{ + using Newtonsoft.Json; + using System; + + /// + /// The Via CEP result class. + /// + public sealed class ViaCEPResult + { + /// + /// Gets or sets the zip code. + /// + /// + /// The zip code. + /// + [JsonProperty("cep")] + public String ZipCode { get; set; } + + /// + /// Gets or sets the street. + /// + /// + /// The street. + /// + [JsonProperty("logradouro")] + public String Street { get; set; } + + /// + /// Gets or sets the complement. + /// + /// + /// The complement. + /// + [JsonProperty("complemento")] + public String Complement { get; set; } + + /// + /// Gets or sets the neighborhood. + /// + /// + /// The neighborhood. + /// + [JsonProperty("bairro")] + public String Neighborhood { get; set; } + + /// + /// Gets or sets the city. + /// + /// + /// The city. + /// + [JsonProperty("localidade")] + public String City { get; set; } + + /// + /// Gets or sets the state initials. + /// + /// + /// The state initials. + /// + [JsonProperty("uf")] + public String StateInitials { get; set; } + + /// + /// Gets or sets the unit. + /// + /// + /// The unit. + /// + [JsonProperty("unidade")] + public String Unit { get; set; } + + /// + /// Gets or sets the ibge code. + /// + /// + /// The ibge code. + /// + [JsonProperty("ibge")] + public Int32 IBGECode { get; set; } + + /// + /// Gets or sets the gia code. + /// + /// + /// The gia code. + /// + [JsonProperty("gia")] + public Int32 GIACode { get; set; } + } +} diff --git a/ViaCEP/ViaCEP.csproj b/ViaCEP/ViaCEP.csproj new file mode 100644 index 0000000..eb51997 --- /dev/null +++ b/ViaCEP/ViaCEP.csproj @@ -0,0 +1,23 @@ + + + + netstandard2.0 + true + Guilherme Branco Stracini + The ViaCEP WebService client for .NET projects + © 2019 Guilherme Branco Stracini. All rights reserved. + https://github.com/guibranco/ViaCEP/blob/master/LICENSE + https://github.com/guibranco/ViaCEP/ + https://github.com/guibranco/ViaCEP + GIT + The first release + cep zipcode postalcode postal zip address location api webservice brasil brazil correios ibge gia mf fazenda + https://github.com/guibranco/ViaCEP/blob/master/logo.png + + + + + + + + diff --git a/ViaCEP/ViaCEPClient.cs b/ViaCEP/ViaCEPClient.cs new file mode 100644 index 0000000..7e778b7 --- /dev/null +++ b/ViaCEP/ViaCEPClient.cs @@ -0,0 +1,90 @@ +namespace ViaCEP +{ + using System; + using System.Collections.Generic; + using System.Net.Http; + using System.Threading; + using System.Threading.Tasks; + + /// + /// The Via CEP client class. + /// + public static class ViaCEPClient + { + #region Private fields + + /// + /// The base URL + /// + private const String BaseUrl = "https://viacep.com.br/ws/"; + + #endregion + + + #region Public methods + + /// + /// Searches the specified zip code. + /// + /// The zip code. + /// + public static ViaCEPResult Search(String zipCode) + { + return SearchAsync(zipCode, CancellationToken.None).Result; + } + + /// + /// Searches the asynchronous. + /// + /// The zip code. + /// The token. + /// + public static async Task 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(token).ConfigureAwait(false); + } + } + + /// + /// Searches the specified state initials. + /// + /// The state initials. + /// The city. + /// The address. + /// + public static IEnumerable Search(String stateInitials, String city, String address) + { + return SearchAsync(stateInitials, city, address, CancellationToken.None).Result; + } + + /// + /// Searches the asynchronous. + /// + /// The state initials. + /// The city. + /// The address. + /// The token. + /// + public static async Task> 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>(token).ConfigureAwait(false); + } + } + + #endregion + } +} diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..597d75b --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,41 @@ +version: 2.0.{build} +branches: + only: + - master +skip_tags: true +image: Visual Studio 2017 +configuration: Release +skip_commits: + message: /(Created|Update).*\.(png|jpg|jpeg|bmp|gif|md)/ +dotnet_csproj: + patch: true + file: '**\*.csproj' + version: '{version}' + package_version: '{version}' + assembly_version: '{version}' + file_version: '{version}' + informational_version: '{version}' +before_build: +- cmd: nuget restore +build: + publish_nuget: true + include_nuget_references: true + parallel: true + verbosity: normal +after_build: +- xcopy C:\projects\viacep\ViaCEP\bin\Release\netstandard2.0\*.* C:\projects\viacep\src\ +- rd /s /q C:\projects\viacep\ViaCEP\bin\Release\ +- cd C:\projects\viacep\ +- 7z a -tzip -mx9 "ViaCEP.v%APPVEYOR_BUILD_VERSION%.zip" src +artifacts: +- path: ViaCEP.v%APPVEYOR_BUILD_VERSION%.zip + name: ZipFile +deploy: +- provider: Environment + name: NuGet +- provider: GitHub + tag: $(appveyor_build_version) + auth_token: + secure: VgYYJdvNLy/n9/uxxlaF0bMpIIrVxCb+dGr66U9nWfPWSN2ySdfuilO8klAw0uvF + force_update: true + artifact: ZipFile \ No newline at end of file diff --git a/logo.png b/logo.png new file mode 100644 index 0000000..8b7d709 Binary files /dev/null and b/logo.png differ