Rest api with ruby » History » Version 15
James Patrick, 2017-06-19 03:12
Added Documenation for using ActiveResource header based API key for redmine.
1 | 1 | Jean-Philippe Lang | h1. Using the REST API with Ruby |
---|---|---|---|
2 | |||
3 | 12 | David Lukas Müller | {{>toc}} |
4 | |||
5 | 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. |
6 | |||
7 | 12 | David Lukas Müller | h2. ActiveResource (Rails) |
8 | 4 | Eric Davis | |
9 | 10 | Toshi MARUYAMA | On Redmine 3.x (Rails 4.2), you need to add 'activeresource' gem. |
10 | For example, at Gemfile.local: |
||
11 | <pre><code class="ruby"> |
||
12 | gem 'activeresource' |
||
13 | </code></pre> |
||
14 | |||
15 | 1 | Jean-Philippe Lang | Here is a simple ruby script that demonstrates how to use the Redmine REST API: |
16 | |||
17 | <pre> |
||
18 | <code class="ruby"> |
||
19 | require 'rubygems' |
||
20 | require 'active_resource' |
||
21 | |||
22 | # Issue model on the client side |
||
23 | class Issue < ActiveResource::Base |
||
24 | self.site = 'http://redmine.server/' |
||
25 | self.user = 'foo' |
||
26 | self.password = 'bar' |
||
27 | 15 | James Patrick | # Or you can use the Redmine-API key |
28 | # self.headers['X-Redmine-API-Key'] = 'baz' |
||
29 | 1 | Jean-Philippe Lang | end |
30 | |||
31 | 11 | Toshi MARUYAMA | if false |
32 | # Retrieving issues |
||
33 | issues = Issue.find(:all) |
||
34 | puts issues.first.subject |
||
35 | end |
||
36 | 1 | Jean-Philippe Lang | |
37 | # Retrieving an issue |
||
38 | issue = Issue.find(1) |
||
39 | puts issue.description |
||
40 | puts issue.author.name |
||
41 | |||
42 | # Creating an issue |
||
43 | 2 | Jean-Philippe Lang | issue = Issue.new( |
44 | :subject => 'REST API', |
||
45 | :assigned_to_id => 1, |
||
46 | 7 | Denis Savitskiy | :project_id => 1 |
47 | 9 | Marcin Garski | # custom field with id=2 exist in database |
48 | :custom_fields => [{id: 2, value: "IT"}] |
||
49 | 7 | Denis Savitskiy | ) |
50 | 1 | Jean-Philippe Lang | if issue.save |
51 | puts issue.id |
||
52 | else |
||
53 | puts issue.errors.full_messages |
||
54 | end |
||
55 | 9 | Marcin Garski | |
56 | 1 | Jean-Philippe Lang | |
57 | # Updating an issue |
||
58 | issue = Issue.find(1) |
||
59 | issue.subject = 'REST API' |
||
60 | issue.save |
||
61 | |||
62 | # Deleting an issue |
||
63 | issue = Issue.find(1) |
||
64 | 8 | Toshi MARUYAMA | #issue.destroy |
65 | 1 | Jean-Philippe Lang | </code> |
66 | </pre> |
||
67 | 6 | Geoffroy Planquart | |
68 | 1 | Jean-Philippe Lang | _You may need to set @include_root_in_json = true@ in your ActiveResource class_ |
69 | 12 | David Lukas Müller | |
70 | |||
71 | |||
72 | 14 | James Patrick | h2. Pure Ruby (Using only Ruby Core) |
73 | 12 | David Lukas Müller | |
74 | 13 | Mischa The Evil | Here is an example to set the status of Issue !#9599 to internal status_id 1 with a @net/http@ PUT-request: |
75 | 12 | David Lukas Müller | |
76 | <pre><code class="ruby"> |
||
77 | require 'net/https' |
||
78 | require 'uri' |
||
79 | require 'json' |
||
80 | |||
81 | def update_issue_status issue_id, new_status_id, change_note |
||
82 | base_url = "https://your.redmine.example.com" |
||
83 | api_token = "xxxxxxxxxxxxxxx" |
||
84 | |||
85 | payload = { |
||
86 | issue: { |
||
87 | notes: change_note, |
||
88 | status_id: new_status_id |
||
89 | } |
||
90 | } |
||
91 | |||
92 | url = "#{base_url}/issues/#{issue_id}.json" |
||
93 | uri = URI.parse(url) |
||
94 | req = Net::HTTP::Put.new(uri.request_uri) |
||
95 | |||
96 | req["Content-Type"] = "application/json" |
||
97 | req['X-Redmine-API-Key'] = api_token |
||
98 | req.body = payload.to_json |
||
99 | |||
100 | http = Net::HTTP.new(uri.host, uri.port) |
||
101 | http.use_ssl = true |
||
102 | response = http.request(req) |
||
103 | return response |
||
104 | end |
||
105 | |||
106 | # Set Status of issue #9599 to internal status_id = 1 |
||
107 | response = update_issue_status 9599, 1, "Changed Issue Status via REST-API" |
||
108 | </code></pre> |