|
| 1 | +unit OpenApiIndy; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | + SysUtils, Classes, OpenApiRest, IdHTTP; |
| 7 | + |
| 8 | +type |
| 9 | + TIndyHTTP = class(TIdHTTP) |
| 10 | + end; |
| 11 | + |
| 12 | + TClientCreatedEvent = procedure(Client: TIdHttp) of object; |
| 13 | + |
| 14 | + TIndyRestRequest = class(TRestRequest) |
| 15 | + strict private |
| 16 | + FOnClientCreated: TClientCreatedEvent; |
| 17 | + public |
| 18 | + constructor Create(AOnClientCreated: TClientCreatedEvent); |
| 19 | + function Execute: IRestResponse; override; |
| 20 | + end; |
| 21 | + |
| 22 | + TIndyRestResponse = class(TInterfacedObject, IRestResponse) |
| 23 | + strict private |
| 24 | + FClient: TIndyHTTP; |
| 25 | + FResponseBody: TBytes; |
| 26 | + public |
| 27 | + constructor Create(Client: TIndyHTTP; const ResponseBody: TBytes); |
| 28 | + destructor Destroy; override; |
| 29 | + function StatusCode: Integer; |
| 30 | + function ContentAsString: string; |
| 31 | + function ContentAsBytes: TBytes; |
| 32 | + function GetHeader(const Name: string): string; |
| 33 | + end; |
| 34 | + |
| 35 | + TIndyRestRequestFactory = class(TInterfacedObject, IRestRequestFactory) |
| 36 | + private |
| 37 | + FOnClientCreated: TClientCreatedEvent; |
| 38 | + public |
| 39 | + function CreateRequest: IRestRequest; |
| 40 | + property OnClientCreated: TClientCreatedEvent read FOnClientCreated write FOnClientCreated; |
| 41 | + end; |
| 42 | + |
| 43 | +implementation |
| 44 | + |
| 45 | +{ TIndyRestRequestFactory } |
| 46 | + |
| 47 | +function TIndyRestRequestFactory.CreateRequest: IRestRequest; |
| 48 | +begin |
| 49 | + Result := TIndyRestRequest.Create(FOnClientCreated); |
| 50 | +end; |
| 51 | + |
| 52 | +{ TIndyRestRequest } |
| 53 | + |
| 54 | +constructor TIndyRestRequest.Create(AOnClientCreated: TClientCreatedEvent); |
| 55 | +begin |
| 56 | + inherited Create; |
| 57 | + FOnClientCreated := AOnClientCreated; |
| 58 | +end; |
| 59 | + |
| 60 | +function TIndyRestRequest.Execute: IRestResponse; |
| 61 | +var |
| 62 | + Client: TIndyHTTP; |
| 63 | + I: Integer; |
| 64 | + RequestBody: TStringStream; |
| 65 | + ResponseBody: TBytesStream; |
| 66 | +begin |
| 67 | + Client := TIndyHTTP.Create; |
| 68 | + try |
| 69 | + if Assigned(FOnClientCreated) then |
| 70 | + FOnClientCreated(Client); |
| 71 | + |
| 72 | + Client.HTTPOptions := Client.HTTPOptions + [hoNoProtocolErrorException, hoWantProtocolErrorContent]; |
| 73 | + RequestBody := nil; |
| 74 | + if Body <> '' then |
| 75 | + RequestBody := TStringStream.Create(Body, TEncoding.UTF8, False); |
| 76 | + try |
| 77 | + ResponseBody := TBytesStream.Create; |
| 78 | + try |
| 79 | + Client.Request.Accept := ''; |
| 80 | + for I := 0 to Headers.Count - 1 do |
| 81 | + Client.Request.CustomHeaders.AddValue(Headers.Names[I], Headers.ValueFromIndex[I]); |
| 82 | + Client.DoRequest(Self.Method, BuildUrl, RequestBody, ResponseBody, []); |
| 83 | + Result := TIndyRestResponse.Create(Client, Copy(ResponseBody.Bytes, 0, ResponseBody.Size)); |
| 84 | + Client := nil; |
| 85 | + finally |
| 86 | + ResponseBody.Free; |
| 87 | + end; |
| 88 | + finally |
| 89 | + RequestBody.Free; |
| 90 | + end; |
| 91 | + finally |
| 92 | + Client.Free; |
| 93 | + end; |
| 94 | +end; |
| 95 | + |
| 96 | +{ TIndyRestResponse } |
| 97 | + |
| 98 | +constructor TIndyRestResponse.Create(Client: TIndyHTTP; const ResponseBody: TBytes); |
| 99 | +begin |
| 100 | + inherited Create; |
| 101 | + FClient := Client; |
| 102 | + FResponseBody := ResponseBody; |
| 103 | +end; |
| 104 | + |
| 105 | +destructor TIndyRestResponse.Destroy; |
| 106 | +begin |
| 107 | + FClient.Free; |
| 108 | + inherited; |
| 109 | +end; |
| 110 | + |
| 111 | +function TIndyRestResponse.GetHeader(const Name: string): string; |
| 112 | +begin |
| 113 | + Result := FClient.Response.RawHeaders.Values[Name]; |
| 114 | +end; |
| 115 | + |
| 116 | +function TIndyRestResponse.StatusCode: Integer; |
| 117 | +begin |
| 118 | + Result := FClient.ResponseCode; |
| 119 | +end; |
| 120 | + |
| 121 | +function TIndyRestResponse.ContentAsBytes: TBytes; |
| 122 | +begin |
| 123 | + Result := FResponseBody; |
| 124 | +end; |
| 125 | + |
| 126 | +function TIndyRestResponse.ContentAsString: string; |
| 127 | +begin |
| 128 | + Result := TEncoding.UTF8.GetString(ContentAsBytes); |
| 129 | +end; |
| 130 | + |
| 131 | +end. |
0 commit comments