Project

General

Profile

Rest api ẅith python » History » Version 13

Max Tepkeev, 2014-01-17 08:21

1 4 Javier Hernandez
h1. Using the REST API with Python
2
3 13 Max Tepkeev
Here is the list of available libraries for using REST API with Python: 
4 1 Javier Hernandez
5 13 Max Tepkeev
# "PythonRedmine":https://github.com/maxtepkeev/python-redmine
6
# "PyRedmineWS":https://github.com/ianepperson/pyredminews
7 1 Javier Hernandez
# "PyActiveResource":http://code.google.com/p/pyactiveresource/
8 13 Max Tepkeev
# "PyRed":https://github.com/alourie/pyred
9 1 Javier Hernandez
10
11 13 Max Tepkeev
h2. *PythonRedmine*:
12 1 Javier Hernandez
13 13 Max Tepkeev
Python Redmine is a 2014 year library which purpose is to support 100% features of Redmine's REST API. It provides a simple but powerful Pythonic API inspired by a well-known Django ORM, is thoroughly tested and has 100% code coverage. Documentation is available at "read the docs":http://python-redmine.readthedocs.org. Example:
14
15
<pre><code class="python">
16
>>> from redmine import Redmine
17
>>> redmine = Redmine('http://demo.redmine.org')
18
>>> project = redmine.project.get('vacation')
19
>>> project.id
20
30404
21
>>> project.identifier
22
'vacation'
23
>>> project.created_on
24
datetime.datetime(2013, 12, 31, 13, 27, 47)
25
>>> project.issues
26
<redmine.resultsets.ResourceSet object with Issue resources>
27
>>> project.issues[0]
28
<redmine.resources.Issue #34441 "Vacation">
29
>>> dir(project.issues[0])
30
['assigned_to', 'author', 'created_on', 'description', 'done_ratio',
31
'due_date', 'estimated_hours', 'id', 'priority', 'project', 'relations',
32
'start_date', 'status', 'subject', 'time_entries', 'tracker', 'updated_on']
33
>>> project.issues[0].subject
34
'Vacation'
35
>>> project.issues[0].time_entries
36
<redmine.resultsets.ResourceSet object with TimeEntry resources>
37
</code></pre>
38
39
h2. *PyRedmineWS* example:
40
41 8 Ian Epperson
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.
42 1 Javier Hernandez
43 12 Rob van der Linde
The dateutil library contains a handy method called relativedelta for calculating relative dates.
44 1 Javier Hernandez
45 11 Mischa The Evil
<pre><code class="python">
46 9 Ian Epperson
# Import the Redmine class
47 1 Javier Hernandez
from redmine import Redmine
48
from dateutil.relativedelta import relativedelta
49
50
server = Redmine('http://my-server.com', username='Me', password='seakrit')
51
project = server.projects['parrot']
52
53
# Find Eric in the user data
54
for u in server.users:
55
    if u.firstname == 'Eric' and u.lastname == 'Idle':
56
       user = u
57
       break
58
else:
59
    raise Exception("Didn't find Eric Idle in the user dateabase")
60
61
# Extend issues in project assigned to user by two weeks
62
for issue in project.issues(assigned_to_id=user.id):
63
    if issue.due_date is not None:
64
       issue.due_date += relativedelta(weeks=+2)
65 9 Ian Epperson
       issue.save('Giving Eric more time to complete - he was out ill')
66 11 Mischa The Evil
67 1 Javier Hernandez
</code></pre>
68 9 Ian Epperson
69
h2. *PyActiveResource* example:
70 11 Mischa The Evil
71 9 Ian Epperson
<pre><code class="python">
72
# Importing pyactiveresource
73
from pyactiveresource.activeresource import ActiveResource
74
75
class Issue(ActiveResource):
76
    _site = 'http://redmine.foo.org'
77
    _user = 'username'
78
    _password = 'password'
79
80
# Get issues
81
issues = Issue.find()
82
83
# Get a specific issue, from its id
84
issue = Issue.find(1345)
85 1 Javier Hernandez
86 9 Ian Epperson
# Issue attributes
87 1 Javier Hernandez
88 9 Ian Epperson
# Updating an attribute
89 1 Javier Hernandez
90 11 Mischa The Evil
</code></pre>
91 10 Alex Lourie
92 13 Max Tepkeev
h2. *PyRed* example: TBD