Rest api with delphi » History » Version 1

Rodrigo Carvalho, 2011-10-26 22:24

1 1 Rodrigo Carvalho
h1. Using the REST API with Delphi
2 1 Rodrigo Carvalho
3 1 Rodrigo Carvalho
Here are two simple procedures that demonstrates how to use the Redmine REST API with Delphi. This example uses "ICS component THttpCli":http://www.overbyte.be/frame_index.html?redirTo=/products/ics.html:
4 1 Rodrigo Carvalho
5 1 Rodrigo Carvalho
<pre><code class="delphi">
6 1 Rodrigo Carvalho
//Creating an Issue
7 1 Rodrigo Carvalho
procedure AddIssue;
8 1 Rodrigo Carvalho
var
9 1 Rodrigo Carvalho
  xmlStream     : TMemoryStream;
10 1 Rodrigo Carvalho
  tempArray     : array[0..10000] of char;
11 1 Rodrigo Carvalho
  xmlIssue      : String;
12 1 Rodrigo Carvalho
  apiAccessKey  : String;
13 1 Rodrigo Carvalho
  HttpClient    : THttpCli;
14 1 Rodrigo Carvalho
begin
15 1 Rodrigo Carvalho
  apiAccessKey := 'a1234567abcde12121a11a123456a12a12ab123ab'; ///Api user key example
16 1 Rodrigo Carvalho
  xmlStream := TMemoryStream.Create; //Create the XML stream
17 1 Rodrigo Carvalho
18 1 Rodrigo Carvalho
  //Create and set the Http Client component
19 1 Rodrigo Carvalho
  HttpClient := THttpCli.Create(nil);
20 1 Rodrigo Carvalho
  HttpClient.ContentTypePost := 'text/xml';
21 1 Rodrigo Carvalho
  HttpClient.URL:= 'http://localhost:3000/issues.xml?key='+apiAccessKey;
22 1 Rodrigo Carvalho
  try
23 1 Rodrigo Carvalho
    xmlIssue :=
24 1 Rodrigo Carvalho
        '<?xml version="1.0"?>' +
25 1 Rodrigo Carvalho
        '<issue>' +
26 1 Rodrigo Carvalho
        '  <project_id>1</project_id>'+
27 1 Rodrigo Carvalho
        '  <tracker_id>1</tracker_id>'+
28 1 Rodrigo Carvalho
        '  <status_id>1</status_id>'+
29 1 Rodrigo Carvalho
        '  <priority_id>1</priority_id>'+
30 1 Rodrigo Carvalho
        '  <author_id>1</author_id>'+
31 1 Rodrigo Carvalho
        '  <start_date>2011-10-09</start_date>'+
32 1 Rodrigo Carvalho
        '  <subject>Created in DELPHI</subject>'+
33 1 Rodrigo Carvalho
        '  <description>Issue created in DELPHI using Redmine REST API</description>'+
34 1 Rodrigo Carvalho
        '  <custom_fields type="array">' + // adding custom fields
35 1 Rodrigo Carvalho
        '       <custom_field id="1">'+
36 1 Rodrigo Carvalho
        '           <value>12121212</value>'+
37 1 Rodrigo Carvalho
        '       </custom_field>'+
38 1 Rodrigo Carvalho
        '       <custom_field id="2">'+
39 1 Rodrigo Carvalho
        '           <value>2010-10-09</value>'+
40 1 Rodrigo Carvalho
        '       </custom_field>'+
41 1 Rodrigo Carvalho
        '  </custom_fields>'+
42 1 Rodrigo Carvalho
        '</issue>';
43 1 Rodrigo Carvalho
44 1 Rodrigo Carvalho
    // Fill the stream with the xmlIssue
45 1 Rodrigo Carvalho
    FillChar( temparray, SizeOf( temparray ), #0 );
46 1 Rodrigo Carvalho
    strpcopy( temparray, xmlIssue );
47 1 Rodrigo Carvalho
    xmlStream.Write( temparray, length(xmlIssue) );
48 1 Rodrigo Carvalho
    xmlStream.Seek( 0, soFromBeginning);
49 1 Rodrigo Carvalho
50 1 Rodrigo Carvalho
    // set the xml stream and post
51 1 Rodrigo Carvalho
    HttpClient.SendStream:=xmlStream;
52 1 Rodrigo Carvalho
    try
53 1 Rodrigo Carvalho
      HttpClient.Post;
54 1 Rodrigo Carvalho
    except
55 1 Rodrigo Carvalho
      raise;
56 1 Rodrigo Carvalho
    end;
57 1 Rodrigo Carvalho
  finally
58 1 Rodrigo Carvalho
    xmlStream.Free;
59 1 Rodrigo Carvalho
    HttpClient.Free;
60 1 Rodrigo Carvalho
  end;
61 1 Rodrigo Carvalho
end;
62 1 Rodrigo Carvalho
63 1 Rodrigo Carvalho
//Listing Issue
64 1 Rodrigo Carvalho
procedure GetIssue(issueId: String);
65 1 Rodrigo Carvalho
var
66 1 Rodrigo Carvalho
  apiAccessKey: String;
67 1 Rodrigo Carvalho
  HttpClient: THttpCli;
68 1 Rodrigo Carvalho
  xmlIssue: TXMLDocument;
69 1 Rodrigo Carvalho
begin
70 1 Rodrigo Carvalho
    Memo.Lines.Clear; //using TMemo to show the result
71 1 Rodrigo Carvalho
    apiAccessKey := 'a1234567abcde12121a11a123456a12a12ab123ab'; //Api user key example
72 1 Rodrigo Carvalho
73 1 Rodrigo Carvalho
    xmlIssue := TXMLDocument.Create(Self); //Creating XML document. Self = TForm
74 1 Rodrigo Carvalho
75 1 Rodrigo Carvalho
    //Create and set the Http Client component
76 1 Rodrigo Carvalho
    HttpClient := THttpCli.Create(nil);
77 1 Rodrigo Carvalho
    HttpClient.ContentTypePost := 'text/xml';
78 1 Rodrigo Carvalho
    HttpClient.URL:= 'http://localhost:3000/issues/'+issueId+'.xml?key='+apiAccessKey;
79 1 Rodrigo Carvalho
80 1 Rodrigo Carvalho
    HttpClient.RcvdStream := TStringStream.Create(''); //Create the Stream
81 1 Rodrigo Carvalho
    try
82 1 Rodrigo Carvalho
      try
83 1 Rodrigo Carvalho
        //Get the Issue and show some properties in TMemo
84 1 Rodrigo Carvalho
        HttpClient.Get;
85 1 Rodrigo Carvalho
        xmlIssue.LoadFromStream(HttpClient.RcvdStream);
86 1 Rodrigo Carvalho
        xmlIssue.Active := True;
87 1 Rodrigo Carvalho
88 1 Rodrigo Carvalho
        Memo.Lines.Add('Id = ' + xmlIssue.DocumentElement.ChildNodes['id'].NodeValue);
89 1 Rodrigo Carvalho
        Memo.Lines.Add('Project id = ' + xmlIssue.DocumentElement.ChildNodes['project'].AttributeNodes['id'].NodeValue);
90 1 Rodrigo Carvalho
        Memo.Lines.Add('Project Name = ' + xmlIssue.DocumentElement.ChildNodes['project'].AttributeNodes['name'].NodeValue);
91 1 Rodrigo Carvalho
        Memo.Lines.Add('Tracker id = ' + xmlIssue.DocumentElement.ChildNodes['tracker'].AttributeNodes['id'].NodeValue);
92 1 Rodrigo Carvalho
        Memo.Lines.Add('Tracker name = ' + xmlIssue.DocumentElement.ChildNodes['tracker'].AttributeNodes['name'].NodeValue);
93 1 Rodrigo Carvalho
      Except
94 1 Rodrigo Carvalho
        raise;
95 1 Rodrigo Carvalho
      end;
96 1 Rodrigo Carvalho
    finally
97 1 Rodrigo Carvalho
      HttpClient.Free;
98 1 Rodrigo Carvalho
      xmlIssue.Free;
99 1 Rodrigo Carvalho
    end;
100 1 Rodrigo Carvalho
end;
101 1 Rodrigo Carvalho
</pre></code>