iredmail-doc/html/iredadmin-pro.restful.api.p...

132 lines
5.5 KiB
HTML
Raw Normal View History

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>iRedAdmin-Pro RESTful API (interact with Python)</title>
<link rel="stylesheet" type="text/css" href="./css/markdown.css" />
</head>
<body>
<div id="navigation">
<a href="/index.html" target="_blank"><img alt="iRedMail web site" src="images/logo-iredmail.png" style="vertical-align: middle; height: 30px;"/> <span>iRedMail</span></a>
&nbsp;&nbsp;//&nbsp;&nbsp;<a href="./index.html">Document Index</a></div><h1 id="iredadmin-pro-restful-api-interact-with-python">iRedAdmin-Pro RESTful API (interact with Python)</h1>
<div class="toc">
<ul>
<li><a href="#iredadmin-pro-restful-api-interact-with-python">iRedAdmin-Pro RESTful API (interact with Python)</a><ul>
<li><a href="#summary">Summary</a></li>
<li><a href="#requirements">Requirements</a></li>
<li><a href="#sample-code">Sample code</a><ul>
<li><a href="#login">Login</a></li>
<li><a href="#create-new-domain">Create new domain</a></li>
<li><a href="#delete-domain">Delete domain</a></li>
<li><a href="#create-new-user">Create new user</a></li>
<li><a href="#delete-user">Delete user</a></li>
</ul>
</li>
<li><a href="#see-also">See Also</a></li>
</ul>
</li>
</ul>
</div>
<h2 id="summary">Summary</h2>
<p>iRedAdmin-Pro RESTful API will return message in JSON format.</p>
<ul>
<li>If operation succeed, it returns JSON <code>{'success': true}</code>.</li>
<li>If operation failed, it returns JSON <code>{'success': false, 'msg': '&lt;error_reason&gt;'}</code>.</li>
</ul>
<h2 id="requirements">Requirements</h2>
<ul>
<li>At least iRedAdmin-Pro-SQL-2.4.0 or iRedAdmin-Pro-LDAP-2.6.0. Earlier releases
didn't offer RESTful API.</li>
<li>Our sample Python code requires Python module <code>requests</code>. For more details,
please read its <a href="http://docs.python-requests.org/en/master/">official documentation</a>.</li>
</ul>
<h2 id="sample-code">Sample code</h2>
<h3 id="login">Login</h3>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>We need the cookies for further operations.</p>
</div>
<pre><code>import sys
import requests
# Base URL to iRedAdmin-Pro API interface
url = 'https://&lt;server&gt;/iredadmin/api'
# Admin username (email) and password.
admin = 'my_admin@domain.com'
pw = 'my_password'
#
# Login
#
r = requests.post(url + '/login', data={'username': admin, 'password': pw})
# Get returned JSON data (Python dict)
data = r.json()
if not data['success']:
sys.exit('Login failed')
# Get cookies
cookies = r.cookies
</code></pre>
<h3 id="create-new-domain">Create new domain</h3>
<p>Create new domain <code>test.com</code>.</p>
<pre><code>requests.post(url + '/domain/test.com',
cookies=cookies,
data={'defaultQuota': '1024'})
</code></pre>
<p>Optional POST data:</p>
<ul>
<li><code>cn</code>: the short description of this domain name. e.g. company name.</li>
<li><code>quota</code>: a integer number for mailbox quota (for whole domain)</li>
<li><code>preferredLanguage</code>: default preferred language for new user. e.g. <code>en_US</code> for English, <code>de_DE</code> for Deutsch.</li>
<li><code>defaultQuota</code>: default mailbox quota for new user.</li>
<li><code>maxUserQuota</code>: Max mailbox quota of a single mail user</li>
<li><code>numberOfUsers</code>: Max number of mail user accounts</li>
<li><code>numberOfAliases</code>: Max number of mail alias accounts</li>
</ul>
<h3 id="delete-domain">Delete domain</h3>
<p>Delete domain: test.com</p>
<pre><code>requests.delete(url + '/domain/test.com', cookies=cookies)
</code></pre>
<h3 id="create-new-user">Create new user</h3>
<p>Create new user <code>zhb@test.com</code>.</p>
<pre><code>requests.post(url + '/user/zhb@test.com',
cookies=cookies,
data={'cn': 'Zhang Huangbin',
'password': 'password_for_zhb',
'preferredLanguage': 'en_US',
'mailQuota': 2048})
</code></pre>
<p>Required POST data:</p>
<ul>
<li><code>password</code>: password for this user</li>
</ul>
<p>Optional POST data:</p>
<ul>
<li><code>cn</code>: display name</li>
<li><code>preferredLanguage</code>: default preferred language for new user. e.g. <code>en_US</code> for English, <code>de_DE</code> for Deutsch.</li>
<li><code>mailQuota</code>: mailbox quota for this user (in MB). Defaults to per-domain quota setting or unlimited.</li>
</ul>
<h3 id="delete-user">Delete user</h3>
<p>Delete user <code>zhb@test.com</code></p>
<pre><code>requests.delete(url + '/user/zhb@test.com', cookies=cookies)
</code></pre>
<h2 id="see-also">See Also</h2>
<ul>
<li><a href="./iredadmin-pro.restful.api.curl.html">iRedAdmin-Pro RESTful API (interact with <code>curl</code>)</a></li>
</ul><p style="text-align: center; color: grey;">All documents are available in <a href="https://bitbucket.org/zhb/iredmail-docs/src">BitBucket repository</a>, and published under <a href="http://creativecommons.org/licenses/by-nd/3.0/us/" target="_blank">Creative Commons</a> license. If you found something wrong, please do <a href="http://www.iredmail.org/contact.html">contact us</a> to fix it.<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-3293801-21', 'auto');
ga('send', 'pageview');
</script>
</body></html>