Rest api » History » Version 4

Jean-Philippe Lang, 2010-01-14 20:55
Projects API added

1 1 Jean-Philippe Lang
h1. Redmine API
2 1 Jean-Philippe Lang
3 1 Jean-Philippe Lang
Redmine exposes some of its data through a REST API. This API provides access and basic CRUD operations (create, update, delete) for the resources described below.
4 1 Jean-Philippe Lang
5 1 Jean-Philippe Lang
Most of the time, the API requires authentication. This is done via HTTP Basic authentication using the regular Redmine accounts. To enable this API-style authentication, check *Enable REST API* in Administration -> Settings -> Authentication.
6 1 Jean-Philippe Lang
7 4 Jean-Philippe Lang
_At the time of writing, the API is only available in trunk (see r3310, r3313)._
8 2 Jean-Philippe Lang
9 1 Jean-Philippe Lang
h2. API Description
10 1 Jean-Philippe Lang
11 1 Jean-Philippe Lang
* [[Rest_Issues|Issues]]
12 4 Jean-Philippe Lang
* [[Rest_Projects|Projects]]
13 1 Jean-Philippe Lang
14 1 Jean-Philippe Lang
h2. API Usage
15 1 Jean-Philippe Lang
16 1 Jean-Philippe Lang
h3. Ruby
17 1 Jean-Philippe Lang
18 1 Jean-Philippe Lang
Redmine REST API follows the Rails's RESTful conventions, so using it with "ActiveResource":http://api.rubyonrails.org/classes/ActiveResource/Base.html is pretty straightforward.
19 1 Jean-Philippe Lang
20 3 Jean-Philippe Lang
Here is a simple ruby script that demonstrates how to use the Redmine REST API:
21 3 Jean-Philippe Lang
22 1 Jean-Philippe Lang
<pre>
23 1 Jean-Philippe Lang
<code class="ruby">
24 1 Jean-Philippe Lang
require 'rubygems'
25 1 Jean-Philippe Lang
require 'active_resource'
26 1 Jean-Philippe Lang
27 1 Jean-Philippe Lang
# Issue model on the client side
28 1 Jean-Philippe Lang
class Issue < ActiveResource::Base
29 1 Jean-Philippe Lang
  self.site = 'http://redmine.server/'
30 1 Jean-Philippe Lang
  self.user = 'foo'
31 1 Jean-Philippe Lang
  self.password = 'bar'
32 1 Jean-Philippe Lang
end
33 1 Jean-Philippe Lang
34 1 Jean-Philippe Lang
# Retrieving issues
35 1 Jean-Philippe Lang
issues = Issue.find(:all)
36 1 Jean-Philippe Lang
puts issues.first.subject
37 1 Jean-Philippe Lang
38 1 Jean-Philippe Lang
# Retrieving an issue
39 1 Jean-Philippe Lang
issue = Issue.find(1)
40 1 Jean-Philippe Lang
puts issue.description
41 3 Jean-Philippe Lang
puts issue.author.name
42 1 Jean-Philippe Lang
43 1 Jean-Philippe Lang
# Creating an issue
44 1 Jean-Philippe Lang
issue = Issue.new(:subject => 'REST API', :assigned_to_id => 1, :project_id => 1)
45 1 Jean-Philippe Lang
if issue.save
46 1 Jean-Philippe Lang
  puts issue.id
47 1 Jean-Philippe Lang
else
48 1 Jean-Philippe Lang
  puts issue.errors.full_messages
49 1 Jean-Philippe Lang
end
50 1 Jean-Philippe Lang
51 1 Jean-Philippe Lang
# Updating an issue
52 1 Jean-Philippe Lang
issue = Issue.find(1)
53 1 Jean-Philippe Lang
issue.subject = 'REST API'
54 1 Jean-Philippe Lang
issue.save
55 1 Jean-Philippe Lang
56 1 Jean-Philippe Lang
# Deleting an issue
57 1 Jean-Philippe Lang
issue = Issue.find(1)
58 1 Jean-Philippe Lang
issue.destroy
59 1 Jean-Philippe Lang
</code>
60 1 Jean-Philippe Lang
</pre>