From 4845ab9f23aa92c542d799646d2237724052e07e Mon Sep 17 00:00:00 2001 From: Guilherme Branco Stracini Date: Fri, 15 Feb 2019 10:01:49 -0200 Subject: [PATCH] Release of version 1.0.0 --- README.md | 49 +++++++++++++++- ViaCEP.Tests/AddressTests.cs | 28 ++++++++++ ViaCEP.Tests/ViaCEP.Tests.csproj | 19 +++++++ ViaCEP.Tests/ZipCodeTests.cs | 24 ++++++++ ViaCEP.sln | 31 +++++++++++ ViaCEP/VIaCEPResult.cs | 92 +++++++++++++++++++++++++++++++ ViaCEP/ViaCEP.csproj | 23 ++++++++ ViaCEP/ViaCEPClient.cs | 90 ++++++++++++++++++++++++++++++ appveyor.yml | 41 ++++++++++++++ logo.png | Bin 0 -> 5232 bytes 10 files changed, 396 insertions(+), 1 deletion(-) create mode 100644 ViaCEP.Tests/AddressTests.cs create mode 100644 ViaCEP.Tests/ViaCEP.Tests.csproj create mode 100644 ViaCEP.Tests/ZipCodeTests.cs create mode 100644 ViaCEP.sln create mode 100644 ViaCEP/VIaCEPResult.cs create mode 100644 ViaCEP/ViaCEP.csproj create mode 100644 ViaCEP/ViaCEPClient.cs create mode 100644 appveyor.yml create mode 100644 logo.png 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 0000000000000000000000000000000000000000..8b7d709a2d30d810e4ad201da50dbdcd01e29a79 GIT binary patch literal 5232 zcmbVQc{G%L-=Az_$&!5u6S8F+%)~ImSmVwvSxSZ(YmD7sY=a0PTV&0W)I=h?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