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