Rest api » History » Version 3

Jean-Philippe Lang, 2010-01-13 21:15

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 2 Jean-Philippe Lang
_At the time of writing, the API is only available in trunk (see r3310)._
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 1 Jean-Philippe Lang
13 1 Jean-Philippe Lang
h2. API Usage
14 1 Jean-Philippe Lang
15 1 Jean-Philippe Lang
h3. Ruby
16 1 Jean-Philippe Lang
17 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.
18 1 Jean-Philippe Lang
19 3 Jean-Philippe Lang
Here is a simple ruby script that demonstrates how to use the Redmine REST API:
20 3 Jean-Philippe Lang
21 1 Jean-Philippe Lang
<pre>
22 1 Jean-Philippe Lang
<code class="ruby">
23 1 Jean-Philippe Lang
require 'rubygems'
24 1 Jean-Philippe Lang
require 'active_resource'
25 1 Jean-Philippe Lang
26 1 Jean-Philippe Lang
# Issue model on the client side
27 1 Jean-Philippe Lang
class Issue < ActiveResource::Base
28 1 Jean-Philippe Lang
  self.site = 'http://redmine.server/'
29 1 Jean-Philippe Lang
  self.user = 'foo'
30 1 Jean-Philippe Lang
  self.password = 'bar'
31 1 Jean-Philippe Lang
end
32 1 Jean-Philippe Lang
33 1 Jean-Philippe Lang
# Retrieving issues
34 1 Jean-Philippe Lang
issues = Issue.find(:all)
35 1 Jean-Philippe Lang
puts issues.first.subject
36 1 Jean-Philippe Lang
37 1 Jean-Philippe Lang
# Retrieving an issue
38 1 Jean-Philippe Lang
issue = Issue.find(1)
39 1 Jean-Philippe Lang
puts issue.description
40 3 Jean-Philippe Lang
puts issue.author.name
41 1 Jean-Philippe Lang
42 1 Jean-Philippe Lang
# Creating an issue
43 1 Jean-Philippe Lang
issue = Issue.new(:subject => 'REST API', :assigned_to_id => 1, :project_id => 1)
44 1 Jean-Philippe Lang
if issue.save
45 1 Jean-Philippe Lang
  puts issue.id
46 1 Jean-Philippe Lang
else
47 1 Jean-Philippe Lang
  puts issue.errors.full_messages
48 1 Jean-Philippe Lang
end
49 1 Jean-Philippe Lang
50 1 Jean-Philippe Lang
# Updating an issue
51 1 Jean-Philippe Lang
issue = Issue.find(1)
52 1 Jean-Philippe Lang
issue.subject = 'REST API'
53 1 Jean-Philippe Lang
issue.save
54 1 Jean-Philippe Lang
55 1 Jean-Philippe Lang
# Deleting an issue
56 1 Jean-Philippe Lang
issue = Issue.find(1)
57 1 Jean-Philippe Lang
issue.destroy
58 1 Jean-Philippe Lang
</code>
59 1 Jean-Philippe Lang
</pre>