Project

General

Profile

Redmine API usage in Delphi

Added by Rodrigo Carvalho over 12 years ago

I have to integrate Redmine with a Delphi project. I hope it can help someone. I use the ICS component THttpCli.

//Creating an Issue
procedure AddIssue;
var
  xmlStream     : TMemoryStream;
  tempArray     : array[0..10000] of char;
  xmlIssue      : String;
  apiAccessKey  : String;
  HttpClient    : THttpCli;
begin
  //*** For more information:  http://www.redmine.org/projects/redmine/wiki/Rest_Issues ****//

  apiAccessKey := 'a1234567abcde12121a11a123456a12a12ab123ab'; ///Api user key example
  xmlStream := TMemoryStream.Create; //Create the XML stream

  //Create and set the Http Client component
  HttpClient := THttpCli.Create(nil);
  HttpClient.ContentTypePost := 'text/xml';
  HttpClient.URL:= 'http://localhost:3000/issues.xml?key='+apiAccessKey;
  try
    xmlIssue :=
        '<?xml version="1.0"?>' +
        '<issue>' +
        '  <project_id>1</project_id>'+
        '  <tracker_id>1</tracker_id>'+
        '  <status_id>1</status_id>'+
        '  <priority_id>1</priority_id>'+
        '  <author_id>1</author_id>'+
        '  <start_date>2011-10-09</start_date>'+
        '  <subject>Created in DELPHI</subject>'+
        '  <description>Issue created in DELPHI using Redmine REST API</description>'+
        '  <custom_fields type="array">' + // adding custom fields
        '       <custom_field id="1">'+
        '           <value>12121212</value>'+
        '       </custom_field>'+
        '       <custom_field id="2">'+
        '           <value>2010-10-09</value>'+
        '       </custom_field>'+
        '  </custom_fields>'+
        '</issue>';

    // Fill the stream with the xmlIssue
    FillChar( temparray, SizeOf( temparray ), #0 );
    strpcopy( temparray, xmlIssue );
    xmlStream.Write( temparray, length(xmlIssue) );
    xmlStream.Seek( 0, soFromBeginning);

    // set the xml stream and post
    HttpClient.SendStream:=xmlStream;
    try
      HttpClient.Post;
    except
      raise;
    end;
  finally
    xmlStream.Free;
    HttpClient.Free;
  end;
end;

//Listing Issue
procedure GetIssue;
var
  apiAccessKey: String;
  issueId: String;
  HttpClient: THttpCli;
  xmlIssue: TXMLDocument;
begin
    Memo.Lines.Clear; //using TMemo to show the result
    apiAccessKey := 'a1234567abcde12121a11a123456a12a12ab123ab'; //Api user key example
    issueId := '8782'; //Set Issue Id

    xmlIssue := TXMLDocument.Create(Self); //Creating XML document. Self = TForm

    //Create and set the Http Client component
    HttpClient := THttpCli.Create(nil);
    HttpClient.ContentTypePost := 'text/xml';
    HttpClient.URL:= 'http://localhost:3000/issues/' +issueId+'.xml?key='+apiAccessKey;

    HttpClient.RcvdStream := TStringStream.Create(''); //Create the Stream
    try
      try
        //Get the Issue and show some properties in TMemo
        HttpClient.Get;
        xmlIssue.LoadFromStream(HttpClient.RcvdStream);
        xmlIssue.Active := True;

        Memo.Lines.Add('Id = ' + xmlIssue.DocumentElement.ChildNodes['id'].NodeValue);
        Memo.Lines.Add('Project id = ' + xmlIssue.DocumentElement.ChildNodes['project'].AttributeNodes['id'].NodeValue);
        Memo.Lines.Add('Project Name = ' + xmlIssue.DocumentElement.ChildNodes['project'].AttributeNodes['name'].NodeValue);
        Memo.Lines.Add('Tracker id = ' + xmlIssue.DocumentElement.ChildNodes['tracker'].AttributeNodes['id'].NodeValue);
        Memo.Lines.Add('Tracker name = ' + xmlIssue.DocumentElement.ChildNodes['tracker'].AttributeNodes['name'].NodeValue);
      Except
        raise;
      end;
    finally
      HttpClient.Free;
      xmlIssue.Free;
    end;
end;

Replies (4)

RE: Redmine API usage in Delphi - Added by Mischa The Evil over 12 years ago

Rodrigo,

Is this a working script? If yes, then it might be useful to add this Delphi example to Rest_api.

RE: Redmine API usage in Delphi - Added by Mischa The Evil over 12 years ago

Thanks for contributing this.

RE: Redmine API usage in Delphi - Added by Rodrigo Carvalho over 12 years ago

Thank you, guys, for doing this amazing software! I'm glad to help.

    (1-4/4)