Project

General

Profile

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