Project

General

Profile

Rest api ẅith python » History » Version 10

Alex Lourie, 2013-03-04 12:10

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