Skip to content

Commit

Permalink
Null safety version 2.0.0-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobraghim committed Mar 12, 2021
1 parent 3e9af03 commit 71b4593
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 185 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 2.0.0-beta

* Flutter 2 compatibility - `We are Null-safety`!
* Apesar do usuário poder escolher SSL, todos os testes sem ele estão retornando OK
* Ao rodar o projeto por linha de comando use o attr `isDebug` para evitar o fix
de cors feito para funcionar com versões web.

# 1.0.8

* Usuário pode escolher se quer usar HTTP (default) ou HTTPS
Expand Down
2 changes: 1 addition & 1 deletion example/calcPrecoPrazo.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:sigepweb/sigepweb.dart';

void main() async {
var sigep = Sigepweb(contrato: SigepContrato.semContrato());
var sigep = Sigepweb(contrato: SigepContrato.semContrato(), isDebug: true);

var calcPrecoPrazo = await sigep.calcPrecoPrazo(
cepOrigem: '70002900',
Expand Down
35 changes: 14 additions & 21 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,21 @@ packages:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.3"
version: "1.2.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
dio:
dependency: transitive
description:
name: dio
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.9"
version: "4.0.0-beta7"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -47,14 +40,14 @@ packages:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.2"
version: "0.13.0"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.4"
version: "4.0.0"
meta:
dependency: transitive
description:
Expand All @@ -68,28 +61,28 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.0"
version: "1.11.0"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.4"
version: "4.0.2"
sigepweb:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "1.0.6"
version: "1.0.8"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -101,21 +94,21 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.1"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
typed_data:
dependency: transitive
description:
Expand All @@ -136,13 +129,13 @@ packages:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.0"
version: "5.0.2"
xml2json:
dependency: transitive
description:
name: xml2json
url: "https://pub.dartlang.org"
source: hosted
version: "4.3.0"
version: "5.0.0"
sdks:
dart: ">=2.12.0-0 <3.0.0"
dart: ">=2.12.0 <3.0.0"
3 changes: 2 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: example
description: A new Flutter project.
version: 1.0.0+1
publish_to: none

environment:
sdk: ">=2.0.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
Expand Down
21 changes: 11 additions & 10 deletions lib/src/core.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library sigepweb;

import 'dart:convert';
import 'dart:io' show Platform;
import 'dart:io' show Platform, HttpStatus;

import 'package:dio/dio.dart';
import 'package:http/http.dart' as http;
Expand Down Expand Up @@ -30,7 +30,7 @@ class Sigepweb {
final bool isDebug;
final dio = Dio();

SigepContrato contrato;
SigepContrato? contrato;

/// Construtor padrao.
/// Voce pode iniciar esta classe passando um [contrato] ou informando [isDebug]
Expand Down Expand Up @@ -62,8 +62,8 @@ class Sigepweb {
ServicosPostagem.sedexAVista_04014,
ServicosPostagem.pacAVista_04510,
],
String cepOrigem,
String cepDestino,
required String cepOrigem,
required String cepDestino,
double valorPeso = .5,
FormatoEncomenda formatoEncomenda = FormatoEncomenda.caixa,
int comprimento = 20,
Expand All @@ -80,12 +80,13 @@ class Sigepweb {
final isWeb = !Platform.isAndroid && !Platform.isIOS;

// Vai definir se a base do endpoint deverá usar HTTPS ou apenas HTTP
// final baseEndpoint = 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx';
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/" : ""}$baseEndpoint';
final endpoint = '${(isWeb && !isDebug) ? "https://cors-anywhere.herokuapp.com/" : ""}$baseEndpoint';

var result = <CalcPrecoPrazoItemModel>[];

Expand All @@ -96,8 +97,8 @@ class Sigepweb {

// Efetiva a consulta
var resp = await dio.get('$endpoint/CalcPrecoPrazo', queryParameters: {
'nCdEmpresa': contrato.codAdmin.isEmpty ? '08082650' : contrato.codAdmin,
'sDsSenha': contrato.senha.isEmpty ? '564321' : contrato.senha,
'nCdEmpresa': contrato!.codAdmin!.isEmpty ? '08082650' : contrato!.codAdmin,
'sDsSenha': contrato!.senha!.isEmpty ? '564321' : contrato!.senha,
'nCdServico': servicosList.join(','),
'sCepOrigem': SgUtils.formataCEP(cepOrigem),
'sCepDestino': SgUtils.formataCEP(cepDestino),
Expand All @@ -115,7 +116,7 @@ class Sigepweb {
});

// Valida resposta
if (resp.statusCode != 200 || resp.data.isEmpty) {
if (resp.statusCode != HttpStatus.ok || resp.data.isEmpty) {
throw SigepwebRuntimeError();
}

Expand Down Expand Up @@ -197,13 +198,13 @@ class Sigepweb {

// Efetiva a consulta
var response = await http.post(
endpoint,
Uri.parse(endpoint),
headers: {'Content-Type': 'text/xml; encoding=iso-8859-1'},
body: envelope,
);

// valida resposta
if (response.statusCode != 200 || response.body.isEmpty) {
if (response.statusCode != HttpStatus.ok || response.body.isEmpty) {
throw SigepwebRuntimeError();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/exceptions/sigepweb_runtime_error.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Identifica que um erro foi capturado ou gerado dentro dessa biblioteca
class SigepwebRuntimeError implements Error {
final String msg;
final String? msg;

const SigepwebRuntimeError([this.msg]);

Expand Down
28 changes: 14 additions & 14 deletions lib/src/models/calc_preco_prazo_item_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import 'package:sigepweb/src/constants.dart';

/// Model para retorno do servico calcPrecoPrazo
class CalcPrecoPrazoItemModel {
String nome;
String codigo;
double valor;
int prazoEntrega;
double valorMaoPropria;
double valorAvisoRecebimento;
double valorDeclarado;
bool entregaDomiciliar;
bool entregaSabado;
String codErro;
Map msgErro;
double valorSemAdicionais;
Map obsFim;
String? nome;
String? codigo;
double? valor;
int? prazoEntrega;
double? valorMaoPropria;
double? valorAvisoRecebimento;
double? valorDeclarado;
bool? entregaDomiciliar;
bool? entregaSabado;
String? codErro;
Map? msgErro;
double? valorSemAdicionais;
Map? obsFim;

/// Construtor padrao
CalcPrecoPrazoItemModel({
Expand Down Expand Up @@ -52,7 +52,7 @@ class CalcPrecoPrazoItemModel {
obsFim = json['obsFim'] {
//
// with the code we set the name of the service
nome = ServicosPostagem.nomeServico[codigo] ?? 'Correios';
nome = ServicosPostagem.nomeServico[codigo!] ?? 'Correios';
}

/// Traduz esta model para json
Expand Down
12 changes: 6 additions & 6 deletions lib/src/models/consulta_cep_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import 'dart:convert';

/// Model para retorno do servico consultaCEP
class ConsultaCepModel {
String cep;
String logradouro;
String complemento;
String bairro;
String cidade;
String estado;
String? cep;
String? logradouro;
String? complemento;
String? bairro;
String? cidade;
String? estado;

/// Construtor padrao
ConsultaCepModel({
Expand Down
14 changes: 7 additions & 7 deletions lib/src/models/contrato.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/// Determina um contrato para ser usado nos servicos
class SigepContrato {
String usuario;
String senha;
String codAdmin;
String numContrato;
String numCartao;
String cnpj;
String? usuario;
String? senha;
String? codAdmin;
String? numContrato;
String? numCartao;
String? cnpj;

/// Construtor padrão.
/// DEVE ser usado quando o cliente de fato tem um contrato
Expand All @@ -32,7 +32,7 @@ class SigepContrato {
SigepContrato.homolog() {
usuario = 'sigep';
senha = 'n5f9t8';
codAdmin = '17000190';
codAdmin = '08082650';
numContrato = '9992157880';
numCartao = '0067599079';
cnpj = '34028316000103';
Expand Down
9 changes: 3 additions & 6 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class SgUtils {
cep = cep.replaceAll(RegExp(r'[^0-9]'), '');

if (cep.length < 8) {
throw SigepwebRuntimeError(
'Código do CEP inválido, deve numérico com 8 posições');
throw SigepwebRuntimeError('Código do CEP inválido, deve numérico com 8 posições');
}
return cep.substring(0, 8);
}
Expand All @@ -35,10 +34,8 @@ class SgUtils {
/// de encomendas de caixa, rolo e envelope os valores respectivamente 1, 2 e
/// 3
static String codFormato(FormatoEncomenda formato) {
String result;
if (formato == FormatoEncomenda.caixa) {
result = '1';
} else if (formato == FormatoEncomenda.rolo) {
var result = '1';
if (formato == FormatoEncomenda.rolo) {
result = '2';
} else if (formato == FormatoEncomenda.envelope) {
result = '3';
Expand Down
Loading

0 comments on commit 71b4593

Please sign in to comment.