forked from MosaicoSolutions/ViaCep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViaCepService.cs
141 lines (107 loc) · 5.54 KB
/
ViaCepService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
================================================================
ViaCepService - Um módulo para a consulta de endereços da API ViaCepService
================================================================
ViaCepService - https://viacep.com.br
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2016 Mosaico Solutions.
Authors: Bruno Xavier de Moura - https://github.com/brunoSpeedrun
*/
using System;
using System.Threading.Tasks;
using MosaicoSolutions.ViaCep.Modelos;
using MosaicoSolutions.ViaCep.Net;
using MosaicoSolutions.ViaCep.Util;
namespace MosaicoSolutions.ViaCep
{
/// <inheritdoc />
public sealed partial class ViaCepService : IViaCepService
{
private readonly IViaCepCliente _cliente;
private readonly IEnderecoConvert _enderecoConvert;
private readonly IViaCepRequisicaoPorCepFactory _requisicaoPorCepFactory;
private readonly IViaCepRequisicaoPorEnderecoFactory _requisicaoPorEnderecoFactory;
/// <summary>
/// Devolve um serviço ViaCep padrão.
/// </summary>
/// <returns>Um <see cref="IViaCepService"/> que representa um serviço ViaCep.</returns>
public static IViaCepService Default()
=> new ViaCepService(new ViaCepCliente(),
new EnderecoConvert(),
new ViaCepRequisicaoPorCepFactory(),
new ViaCepRequisicaoPorEnderecoFactory());
/// <inheritdoc />
public ViaCepService(IViaCepCliente viaCepCliente,
IEnderecoConvert enderecoConvert,
IViaCepRequisicaoPorCepFactory requisicaoPorCepFactory,
IViaCepRequisicaoPorEnderecoFactory requisicaoPorEnderecoFactory)
{
_cliente = viaCepCliente ?? throw new ArgumentNullException(nameof(viaCepCliente));
_enderecoConvert = enderecoConvert ?? throw new ArgumentNullException(nameof(enderecoConvert));
_requisicaoPorCepFactory = requisicaoPorCepFactory ?? throw new ArgumentNullException(nameof(requisicaoPorCepFactory));
_requisicaoPorEnderecoFactory = requisicaoPorEnderecoFactory ?? throw new ArgumentNullException(nameof(requisicaoPorEnderecoFactory));
}
#region Helpers
private static ArgumentException ThrowCepVazioErro(string nomeParametro)
=> new ArgumentException("O Cep não pode ser nulo ou vazio. ", nomeParametro);
private static ArgumentException ThrowEnderecoRequisicaoInvalido(string nomeParametro)
=> new ArgumentException("O objeto da requisição por Endereço não é válido.", nomeParametro);
private string ObterEnderecoPorCepComoString(Cep cep, ViaCepFormatoRequisicao formatoRequisicao)
{
IViaCepRequisicaoPor<Cep> requisicao = NovaRequisicaoPorCep(cep, formatoRequisicao);
IViaCepResposta resposta = _cliente.ObterResposta(requisicao.ToUri);
GaranteCodigoDeSucessoOuLancaException(resposta);
IViaCepConteudo conteudo = resposta.ObterConteudo();
GaranteConteudoDaRequisicaoPorCepSemErroOuLancaException(conteudo);
return conteudo.LerComoString();
}
private Task<string> ObterEnderecoPorCepComoStringAsync(Cep cep, ViaCepFormatoRequisicao formatoRequisicao)
=> Task.Run(async () =>
{
IViaCepRequisicaoPor<Cep> requisicao = NovaRequisicaoPorCep(cep, formatoRequisicao);
IViaCepResposta resposta = await _cliente.ObterRespostaAsync(requisicao.ToUri);
GaranteCodigoDeSucessoOuLancaException(resposta);
IViaCepConteudo conteudo = resposta.ObterConteudo();
GaranteConteudoDaRequisicaoPorCepSemErroOuLancaException(conteudo);
return conteudo.LerComoString();
});
private static IViaCepRequisicaoPor<Cep> NovaRequisicaoPorCep(Cep cep, ViaCepFormatoRequisicao formatoRequisicao)
=> new ViaCepRequisicaoPorCep(cep, formatoRequisicao);
private static void GaranteCodigoDeSucessoOuLancaException(IViaCepResposta resposta)
{
if (!resposta.EhCodigoDeSucesso)
throw new ViaCepException(resposta.CodigoDeStatus);
}
private static void GaranteConteudoDaRequisicaoPorCepSemErroOuLancaException(IViaCepConteudo conteudo)
{
if (conteudo.PossuiErro)
throw new CepInexistenteException();
}
#region IDisposable Support
private bool _disposedValue;
private void Dispose(bool disposing)
{
if (_disposedValue) return;
if (disposing)
_cliente.Dispose();
_disposedValue = true;
}
/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#endregion
}
}