Skip to content

Commit

Permalink
User can opt by SSL on CalcPrecoPrazo call
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobraghim committed Mar 8, 2021
1 parent 214f802 commit 3e9af03
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
8 changes: 7 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"name": "Flutter",
"request": "launch",
"type": "dart"
}
},
{
"name": "Dart: Run all Tests",
"request": "launch",
"type": "dart",
"program": "./test/"
}
]
}
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# 1.0.8

* Usuário pode escolher se quer usar HTTP (default) ou HTTPS
na requisição a CalcPrecoPrazo. Para isso é necessário definir
na instanciação da classe `Sigepweb` o atributo `useSSL`

# 1.0.7

* CORS policy para requisição via web

# 1.0.6

* Regras de acordo com package pedantic
Expand Down
19 changes: 13 additions & 6 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
charcode:
dependency: transitive
description:
Expand All @@ -14,7 +21,7 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.12"
version: "1.15.0"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -54,7 +61,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.8"
version: "1.3.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -82,7 +89,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.5"
version: "1.0.6"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -115,14 +122,14 @@ packages:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
version: "2.1.0"
xml:
dependency: transitive
description:
Expand All @@ -138,4 +145,4 @@ packages:
source: hosted
version: "4.3.0"
sdks:
dart: ">=2.7.0 <3.0.0"
dart: ">=2.12.0-0 <3.0.0"
30 changes: 13 additions & 17 deletions lib/src/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ import 'models/contrato.dart';
class Sigepweb {
///
/// Endpoint para caso de ambiente de testes
final _homEndpoint =
'https://apphom.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl';
final _homEndpoint = 'https://apphom.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl';

///
/// Endpoint para caso de ambiente de producao.
///
/// Este endpoint será usado quando [isDebug] for falso e nesse caso é necessário
/// informar o [contrato]
final _prodEndpoint =
'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl';
final _prodEndpoint = 'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl';

final bool useSSL;
final bool isDebug;
final dio = Dio();

Expand All @@ -42,10 +41,10 @@ class Sigepweb {
Sigepweb({
this.contrato,
this.isDebug = false,
this.useSSL = false,
}) {
if (contrato == null && !isDebug) {
throw SigepwebRuntimeError(
'Obrigatório informar o contrato ou estar em modo debug');
throw SigepwebRuntimeError('Obrigatório informar o contrato ou estar em modo debug');
}

if (isDebug) {
Expand Down Expand Up @@ -80,11 +79,13 @@ class Sigepweb {
// do you know a better way to do that? Tell us...
final isWeb = !Platform.isAndroid && !Platform.isIOS;

// Vai definir se a base do endpoint deverá usar HTTPS ou apenas HTTP
final baseEndpoint = 'http${useSSL ? 's' : ''}://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx';

//
// O Endpoint para calculo de preco e prazo eh o unico diferente
// (ate agora)
final endpoint =
'${isWeb ? "https://cors-anywhere.herokuapp.com/" : ""}http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx';
final endpoint = '${isWeb ? "https://cors-anywhere.herokuapp.com/" : ""}$baseEndpoint';

var result = <CalcPrecoPrazoItemModel>[];

Expand All @@ -95,8 +96,7 @@ class Sigepweb {

// Efetiva a consulta
var resp = await dio.get('$endpoint/CalcPrecoPrazo', queryParameters: {
'nCdEmpresa':
contrato.codAdmin.isEmpty ? '08082650' : contrato.codAdmin,
'nCdEmpresa': contrato.codAdmin.isEmpty ? '08082650' : contrato.codAdmin,
'sDsSenha': contrato.senha.isEmpty ? '564321' : contrato.senha,
'nCdServico': servicosList.join(','),
'sCepOrigem': SgUtils.formataCEP(cepOrigem),
Expand Down Expand Up @@ -131,17 +131,14 @@ class Sigepweb {
if (apiResult['cResultado'] == null ||
apiResult['cResultado']['Servicos'] == null ||
apiResult['cResultado']['Servicos']['cServico'] == null) {
throw SigepwebRuntimeError(
'Xml result format isn\'t with expected format');
throw SigepwebRuntimeError('Xml result format isn\'t with expected format');
}

// Guarda o retorno em uma variavel para facilitar
var cServico = apiResult['cResultado']['Servicos']['cServico'];

// Verifica se houve retorno com erro
if (cServico is Map &&
cServico['Erro'] != null &&
cServico['Erro'] != '0') {
if (cServico is Map && cServico['Erro'] != null && cServico['Erro'] != '0') {
throw SigepwebRuntimeError(cServico['MsgErro']);
}

Expand Down Expand Up @@ -216,8 +213,7 @@ class Sigepweb {
xml2json.parse(response.body);
Map<String, dynamic> apiResult = json.decode(xml2json.toGData());

var soapBodyEnvelope =
apiResult['soap\$Envelope']['soap\$Body']['ns2\$consultaCEPResponse'];
var soapBodyEnvelope = apiResult['soap\$Envelope']['soap\$Body']['ns2\$consultaCEPResponse'];

if (soapBodyEnvelope['return'] == null) {
return ConsultaCepModel();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sigepweb
description: Utilize serviços dos Correios no seu App como buscar CEP ou calcular preços e prazos de encomendas
version: 1.0.6
version: 1.0.8
repository: https://github.com/marcobraghim/sigepweb
issue_tracker: https://github.com/marcobraghim/sigepweb/issues

Expand Down

0 comments on commit 3e9af03

Please sign in to comment.