Rest api with delphi » 履歴 » バージョン 1
Mitsuyoshi Yoshida, 2011/11/13 12:55
1 | 1 | Mitsuyoshi Yoshida | redmine.org の "Rest_api_with_delphi":http://www.redmine.org/projects/redmine/wiki/Rest_api_with_delphi の日本語訳です。 |
---|---|---|---|
2 | |||
3 | h1. Delphi での REST API の使用法 |
||
4 | |||
5 | ここでは Delphi で Redmine REST API をどのように使用するかを示した 2 つの簡単なプロシージャを記述しています。 この例では "ICS component THttpCli":http://www.overbyte.be/frame_index.html?redirTo=/products/ics.html を使用しています。 |
||
6 | |||
7 | <pre><code class="delphi"> |
||
8 | //チケットの作成 |
||
9 | procedure AddIssue; |
||
10 | var |
||
11 | xmlStream : TMemoryStream; |
||
12 | tempArray : array[0..10000] of char; |
||
13 | xmlIssue : String; |
||
14 | apiAccessKey : String; |
||
15 | HttpClient : THttpCli; |
||
16 | begin |
||
17 | apiAccessKey := 'a1234567abcde12121a11a123456a12a12ab123ab'; // サンプル用 API キー |
||
18 | xmlStream := TMemoryStream.Create; // XML stream の作成 |
||
19 | |||
20 | // Http Client コンポーネントの作成とセット |
||
21 | HttpClient := THttpCli.Create(nil); |
||
22 | HttpClient.ContentTypePost := 'text/xml'; |
||
23 | HttpClient.URL:= 'http://localhost:3000/issues.xml?key='+apiAccessKey; |
||
24 | try |
||
25 | xmlIssue := |
||
26 | '<?xml version="1.0"?>' + |
||
27 | '<issue>' + |
||
28 | ' <project_id>1</project_id>'+ |
||
29 | ' <tracker_id>1</tracker_id>'+ |
||
30 | ' <status_id>1</status_id>'+ |
||
31 | ' <priority_id>1</priority_id>'+ |
||
32 | ' <author_id>1</author_id>'+ |
||
33 | ' <start_date>2011-10-09</start_date>'+ |
||
34 | ' <subject>Created in DELPHI</subject>'+ |
||
35 | ' <description>Issue created in DELPHI using Redmine REST API</description>'+ |
||
36 | ' <custom_fields type="array">' + // adding custom fields |
||
37 | ' <custom_field id="1">'+ |
||
38 | ' <value>12121212</value>'+ |
||
39 | ' </custom_field>'+ |
||
40 | ' <custom_field id="2">'+ |
||
41 | ' <value>2010-10-09</value>'+ |
||
42 | ' </custom_field>'+ |
||
43 | ' </custom_fields>'+ |
||
44 | '</issue>'; |
||
45 | |||
46 | // xmlIssue を stream に書き込む |
||
47 | FillChar( temparray, SizeOf( temparray ), #0 ); |
||
48 | strpcopy( temparray, xmlIssue ); |
||
49 | xmlStream.Write( temparray, length(xmlIssue) ); |
||
50 | xmlStream.Seek( 0, soFromBeginning); |
||
51 | |||
52 | // xml stream のセットとデータの送信 |
||
53 | HttpClient.SendStream:=xmlStream; |
||
54 | try |
||
55 | HttpClient.Post; |
||
56 | except |
||
57 | raise; |
||
58 | end; |
||
59 | finally |
||
60 | xmlStream.Free; |
||
61 | HttpClient.Free; |
||
62 | end; |
||
63 | end; |
||
64 | |||
65 | // チケット一覧 |
||
66 | procedure GetIssue(issueId: String); |
||
67 | var |
||
68 | apiAccessKey: String; |
||
69 | HttpClient: THttpCli; |
||
70 | xmlIssue: TXMLDocument; |
||
71 | begin |
||
72 | Memo.Lines.Clear; // 結果を表示するために TMemo を使っている |
||
73 | apiAccessKey := 'a1234567abcde12121a11a123456a12a12ab123ab'; // サンプル用 API キー |
||
74 | |||
75 | xmlIssue := TXMLDocument.Create(Self); //Creating XML document. Self = TForm |
||
76 | |||
77 | // Http Client コンポーネントの作成とセット |
||
78 | HttpClient := THttpCli.Create(nil); |
||
79 | HttpClient.ContentTypePost := 'text/xml'; |
||
80 | HttpClient.URL:= 'http://localhost:3000/issues/'+issueId+'.xml?key='+apiAccessKey; |
||
81 | |||
82 | HttpClient.RcvdStream := TStringStream.Create(''); // Stream の作成 |
||
83 | try |
||
84 | try |
||
85 | // チケットを取得して、 TMemo に格納されたプロパティのいくつかを表示 |
||
86 | HttpClient.Get; |
||
87 | xmlIssue.LoadFromStream(HttpClient.RcvdStream); |
||
88 | xmlIssue.Active := True; |
||
89 | |||
90 | Memo.Lines.Add('Id = ' + xmlIssue.DocumentElement.ChildNodes['id'].NodeValue); |
||
91 | Memo.Lines.Add('Project id = ' + xmlIssue.DocumentElement.ChildNodes['project'].AttributeNodes['id'].NodeValue); |
||
92 | Memo.Lines.Add('Project Name = ' + xmlIssue.DocumentElement.ChildNodes['project'].AttributeNodes['name'].NodeValue); |
||
93 | Memo.Lines.Add('Tracker id = ' + xmlIssue.DocumentElement.ChildNodes['tracker'].AttributeNodes['id'].NodeValue); |
||
94 | Memo.Lines.Add('Tracker name = ' + xmlIssue.DocumentElement.ChildNodes['tracker'].AttributeNodes['name'].NodeValue); |
||
95 | Except |
||
96 | raise; |
||
97 | end; |
||
98 | finally |
||
99 | HttpClient.Free; |
||
100 | xmlIssue.Free; |
||
101 | end; |
||
102 | end; |
||
103 | </pre></code> |