Skip to content

Commit b44dfb8

Browse files
committed
Opção de definir uma api default
1 parent 64a314f commit b44dfb8

File tree

2 files changed

+86
-15
lines changed

2 files changed

+86
-15
lines changed

samples/main.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ implementation
2626

2727
procedure TForm1.Button1Click(Sender: TObject);
2828
begin
29-
var LCEP := TEvoInCEP.New.Get(Edit1.Text);
29+
var LCEP := TEvoInCEP.New(TAPIDefault.ViaCep).Get(Edit1.Text);
3030

3131
Memo1.Clear;
3232
Memo1.Lines.Add('CEP: ' + LCEP.CEP);

src/EvoInCEP.pas

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,94 @@ interface
55
uses EvoinCEP.Intf;
66

77
type
8+
{$SCOPEDENUMS ON}
9+
TAPIDefault = (All, ViaCep, BrasilAPI, OpenCEP, BrasilAberto);
10+
{$SCOPEDENUMS OFF}
11+
812
TEvoInCEP = class(TInterfacedObject, ICEPRequest)
913
private
14+
FAPIDefault: TAPIDefault;
1015
function Get(const AValue: string): ICEPResponse;
1116
function OnlyNumbers(const AValue: string): string;
17+
function GetAll(const AValue: string): ICEPResponse;
18+
function GetViaCep(const AValue: string): ICEPResponse;
19+
function GetBrasilApi(const AValue: string): ICEPResponse;
20+
function GetOpenCep(const AValue: string): ICEPResponse;
21+
function GetBrasilAberto(const AValue: string): ICEPResponse;
22+
function MakeRequest(const AAPIDefault: TAPIDefault): Boolean;
1223
public
13-
class function New: ICEPRequest;
24+
class function New(const AAPIDefault: TAPIDefault = TAPIDefault.All): ICEPRequest;
25+
constructor Create(const AAPIDefault: TAPIDefault); overload;
1426
end;
1527

1628
implementation
1729

1830
uses System.Threading, System.SysUtils, System.Generics.Collections, EvoInCEP.ViaCEP, EvoinCEP.BrasilAPI, EvoinCEP.OpenCEP,
1931
EvoinCEP.BrasilAberto;
2032

33+
34+
constructor TEvoInCEP.Create(const AAPIDefault: TAPIDefault);
35+
begin
36+
FAPIDefault := AAPIDefault;
37+
end;
38+
2139
function TEvoInCEP.Get(const AValue: string): ICEPResponse;
2240
var
2341
LCEP: string;
24-
LList: TList<ITask>;
2542
begin
2643
LCEP := OnlyNumbers(AValue).PadLeft(8, '0');
44+
case FAPIDefault of
45+
TAPIDefault.ViaCep:
46+
Result := GetViaCep(LCEP);
47+
TAPIDefault.BrasilAPI:
48+
Result := GetBrasilApi(LCEP);
49+
TAPIDefault.OpenCEP:
50+
Result := GetOpenCep(LCEP);
51+
TAPIDefault.BrasilAberto:
52+
Result := GetBrasilAberto(LCEP);
53+
else
54+
Result := GetAll(LCEP);
55+
end;
56+
end;
2757

58+
function TEvoInCEP.GetAll(const AValue: string): ICEPResponse;
59+
var
60+
LList: TList<ITask>;
61+
begin
2862
LList := TList<ITask>.Create;
2963
try
30-
LList.Add(TTask.Future<ICEPResponse>(
31-
function: ICEPResponse
32-
begin
33-
Result := TViaCEP.New.Get(LCEP);
34-
if not Assigned(Result) then
35-
raise Exception.Create('Internal server error.');
36-
end));
64+
if MakeRequest(TAPIDefault.ViaCep) then
65+
LList.Add(TTask.Future<ICEPResponse>(
66+
function: ICEPResponse
67+
begin
68+
Result := TViaCEP.New.Get(AValue);
69+
if not Assigned(Result) then
70+
raise Exception.Create('Internal server error.');
71+
end));
3772

73+
if MakeRequest(TAPIDefault.BrasilAPI) then
3874
LList.Add(TTask.Future<ICEPResponse>(
3975
function: ICEPResponse
4076
begin
41-
Result := TBrasilAPI.New.Get(LCEP);
77+
Result := TBrasilAPI.New.Get(AValue);
4278
if not Assigned(Result) then
4379
raise Exception.Create('Internal server error.');
4480
end));
4581

82+
if MakeRequest(TAPIDefault.BrasilAberto) then
4683
LList.Add(TTask.Future<ICEPResponse>(
4784
function: ICEPResponse
4885
begin
49-
Result := TBrasilAberto.New.Get(LCEP);
86+
Result := TBrasilAberto.New.Get(AValue);
5087
if not Assigned(Result) then
5188
raise Exception.Create('Internal server error.');
5289
end));
5390

91+
if MakeRequest(TAPIDefault.OpenCEP) then
5492
LList.Add(TTask.Future<ICEPResponse>(
5593
function: ICEPResponse
5694
begin
57-
Result := TOpenCEP.New.Get(LCEP);
95+
Result := TOpenCEP.New.Get(AValue);
5896
if not Assigned(Result) then
5997
raise Exception.Create('Internal server error.');
6098
end));
@@ -74,9 +112,42 @@ function TEvoInCEP.Get(const AValue: string): ICEPResponse;
74112
end;
75113
end;
76114

77-
class function TEvoInCEP.New: ICEPRequest;
115+
function TEvoInCEP.GetBrasilAberto(const AValue: string): ICEPResponse;
116+
begin
117+
Result := TBrasilAberto.New.Get(AValue);
118+
if not Assigned(Result) then
119+
Result := GetAll(AValue);
120+
end;
121+
122+
function TEvoInCEP.GetBrasilApi(const AValue: string): ICEPResponse;
123+
begin
124+
Result := TBrasilAPI.New.Get(AValue);
125+
if not Assigned(Result) then
126+
Result := GetAll(AValue);
127+
end;
128+
129+
function TEvoInCEP.GetOpenCep(const AValue: string): ICEPResponse;
130+
begin
131+
Result := TOpenCEP.New.Get(AValue);
132+
if not Assigned(Result) then
133+
Result := GetAll(AValue);
134+
end;
135+
136+
function TEvoInCEP.GetViaCep(const AValue: string): ICEPResponse;
137+
begin
138+
Result := TViaCEP.New.Get(AValue);
139+
if not Assigned(Result) then
140+
Result := GetAll(AValue);
141+
end;
142+
143+
function TEvoInCEP.MakeRequest(const AAPIDefault: TAPIDefault): Boolean;
144+
begin
145+
Result := ((FAPIDefault = TAPIDefault.All) or (FAPIDefault <> AAPIDefault));
146+
end;
147+
148+
class function TEvoInCEP.New(const AAPIDefault: TAPIDefault): ICEPRequest;
78149
begin
79-
Result := Self.Create;
150+
Result := Self.Create(AAPIDefault);
80151
end;
81152

82153
function TEvoInCEP.OnlyNumbers(const AValue: string): string;

0 commit comments

Comments
 (0)