Project

General

Profile

Rest api ẅith python » History » Version 11

Mischa The Evil, 2013-03-07 03:42
Fixed missing close code tag, improved code source markup & wrapped PyActiveResource example in code tags

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