Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using GitHub API from Indy in Delphi

I'm working on a project in Delphi 10.1 Berlin which integrates with GitHub. This project intends to download repositories via ZIP files. However, I'm facing some issues.

Originally, I chose (as always) to use Indy to integrate with the GitHub API. I've always used Indy for all web API consumption. However, I'm not having any success using it with GitHub.

The API requires HTTPS. I have obtained the latest OpenSSL DLLs for use with Indy, and am using the Indy library with Delphi 10.1 Berlin.

I have setup a TIdHTTP component with a TIdSSLIOHandlerSocketOpenSSL attached. I've set that IO Handler to all the Method options available, and not one gives me a valid response. I get one of two different responses...

  1. When using sslvSSLv3, I get: error:14094410:SSL routines: SSL3_READ_BYTES:sslv3 alert handshake failure.
  2. When using sslvTLSv1_2, I get: error:1409442E:SSL routines:SSL_READ_BYTES:tlsv1 alert protocol version

I had to resort to TRESTClient for now just to be able to work with the API. But it doesn't handle binary properly.

The test is as simple as https://api.github.com. I can call that in Chrome and Postman, and get a response. Just not via Indy.

How can I accomplish a connection with GitHub's API via Delphi's Indy library?

like image 792
Jerry Dodge Avatar asked Jun 04 '19 02:06

Jerry Dodge


1 Answers

This Indy HTTP subclass works with the GitHub API.

type
  TIndyHttpTransport = class(TIdCustomHTTP)
  public
    constructor Create;
  end;

implementation

uses
  IdSSLOpenSSL;

{ TIndyHttpTransport }

constructor TIndyHttpTransport.Create;
var
  SSLIO: TIdSSLIOHandlerSocketOpenSSL;
begin
  inherited Create;

  HTTPOptions := HTTPOptions + [hoNoProtocolErrorException, hoWantProtocolErrorContent];

  SSLIO := TIdSSLIOHandlerSocketOpenSSL.Create(Self);
  SSLIO.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
  SSLIO.SSLOptions.Mode        := sslmClient;
  SSLIO.SSLOptions.VerifyMode  := [];
  SSLIO.SSLOptions.VerifyDepth := 0;

  Self.IOHandler := SSLIO;

  // Request.UserAgent := 'my useragent string';

end;

Maybe your code also needs to modify the user agent string (because I use this code with different service providers, not only GitHub. Some of them require a modified user agent string instead of the default).

Source: https://github.com/michaelJustin/daraja-framework/blob/master/demo/common/IndyHttpTransport.pas

like image 109
mjn Avatar answered Nov 15 '22 08:11

mjn