Project

General

Profile

Rest api ẅith python » History » Version 8

Ian Epperson, 2013-02-07 20:46
Updated link for the Python library. Added an example for it too.

1 4 Javier Hernandez
h1. Using the REST API with Python
2
3 6 Javier Hernandez
Here is the two well-known options for using REST API with python. 
4 4 Javier Hernandez
5 5 Javier Hernandez
 # "PyActiveResource":http://code.google.com/p/pyactiveresource/
6 8 Ian Epperson
 # "Python library":https://github.com/ianepperson/pyredminews
7 6 Javier Hernandez
8
9
h2. *PyActiveResource* example:
10
11 7 Javier Hernandez
<pre>
12
<code class="python">
13 6 Javier Hernandez
# Importing pyactiveresource
14
from pyactiveresource.activeresource import ActiveResource
15
16
class Issue(ActiveResource):
17
    _site = 'http://redmine.foo.org'
18
    _user = 'username'
19
    _password = 'password'
20
21
# Get issues
22
issues = Issue.find()
23
24
# Get a specific issue, from its id
25
issue = Issue.find(1345)
26
27
# Issue attributes
28
29
# Updating an attribute
30 1 Javier Hernandez
31 8 Ian Epperson
</pre>
32
33
h2. *Python library* example:
34
35
Suppose Eric fell ill and was out for several days. You need to crawl through the project called Parrot and move any due date for issues assigned to Eric out by two more weeks.
36
37
The dateutil library contains a handy method called reativedelta for calculating relative dates.
38
39
<pre>
40
<code class="python">
41
from redmine import Redmine
42
from dateutil.relativedelta import relativedelta
43
44
server = Redmine('http://my-server.com', username='Me', password='seakrit')
45
project = server.projects['parrot']
46
47
# Find Eric in the user data
48
for u in server.users:
49
    if u.firstname == 'Eric' and u.lastname == 'Idle':
50
       user = u
51
       break
52
else:
53
    raise Exception("Didn't find Eric Idle in the user dateabase")
54
55
# Extend issues in project assigned to user by two weeks
56
for issue in project.issues(assigned_to_id=user.id):
57
    if issue.due_date is not None:
58
       issue.due_date += relativedelta(weeks=+2)
59
       issue.save('Giving Eric more time to complete - he was out ill')
60 6 Javier Hernandez
</pre>