Redmine之RestApi集成方式

Redmine是一个较为强大的开源Ticket管理工具,这篇文章我们将会介绍一下如何使用Curl和Redmine所提供的RestApi来进行集成,将会选取Redmine中的用户进行简单的增删改查的操作。

Redmine

相关的介绍以前已经做过,在此不再赘述。详细可以参看如下文章:

文章链接
Ticket管理工具:Redminehttp://blog.csdn.net/liumiaocn/article/details/52107410
Bitnami Redmine安装配置指南http://blog.csdn.net/liumiaocn/article/details/53523604

事前准备

docker方式启动

这篇文章,我们使用easypack下已经整理好的镜像直接使用,使用方法参看如下文章:

文章链接
Ticket管理工具:Redminehttp://blog.csdn.net/liumiaocn/article/details/56254955

安装之后缺省的用户名和密码为:admin/admin,登陆需要修改密码,将其修改为admin/admin123

[root@mail ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
750f29e4deae        liumiaocn/redmine   "/docker-entrypoin..."   12 minutes ago      Up 12 minutes       0.0.0.0:3000->3000/tcp   redmine
3230aef19745        liumiaocn/mysql     "docker-entrypoint..."   16 minutes ago      Up 16 minutes       3306/tcp                 mysql
[root@mail ~]#

启动Rest Web Service

需要在Redmine中做如下设定
这里写图片描述
详细可参照如下文章:

文章链接
设定方式http://blog.csdn.net/liumiaocn/article/details/74832762

Rest api

此文中使用的redmine为版本3.3,mysql为5.7, redmine相关的restapi主要如下:

ResourceStatusAvailability
IssuesStable1
ProjectsStable1
Project MembershipsAlpha1.4
UsersStable1.1
Time EntriesStable1.1
NewsPrototype1.1
Issue RelationsAlpha1.3
VersionsAlpha1.3
Wiki PagesAlpha2.2
QueriesAlpha1.3
AttachmentsBeta1.3
Issue StatusesAlpha1.3
TrackersAlpha1.3
EnumerationsAlpha2.2
Issue CategoriesAlpha1.3
RolesAlpha1.4
GroupsAlpha2.1
Custom FieldsAlpha2.4
SearchAlpha3.3
FilesAlpha3.4

这篇文章我们选取user相关的api进行集成,可以看出该api已经是stable版本,虽然看起来还有很多Alpha版本,但是在开源领域来说,Redmine已经算是不错的选择。

Http Get:查询

首先使用Get 方法进行查询:

命令行:curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json
.json用于制定返回结果的格式
-u制定登陆的用户名和密码

[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json
{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:02:36Z"}],"total_count":1,"offset":0,"limit":25}[root@mail ~]# 
[root@mail ~]# 

如果需要确认详细的信息,可以使用如下命令, 类似的信息后文不再一一验证。

curl -v -H “Content-Type: application/json” -X GET -u admin:admin123 http://192.168.163.151:3000/users.json

[root@mail ~]# curl -v -H "Content-Type: application/json" -X GET -u admin:admin123 http://192.168.163.151:3000/users.json
* About to connect() to 192.168.163.151 port 3000 (#0)
*   Trying 192.168.163.151...
* Connected to 192.168.163.151 (192.168.163.151) port 3000 (#0)
* Server auth using Basic with user 'admin'
> GET /users.json HTTP/1.1
> Authorization: Basic YWRtaW46YWRtaW4xMjM=
> User-Agent: curl/7.29.0
> Host: 192.168.163.151:3000
> Accept: */*
> Content-Type: application/json
> 
< HTTP/1.1 200 OK 
< X-Frame-Options: SAMEORIGIN
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Type: application/json; charset=utf-8
< Content-Length: 217
< Etag: W/"1e49c564bd63f382cd5b6c3697d786a3"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: b3f10c08-37bd-48dd-a455-24c5944bea3d
< X-Runtime: 0.187073
< Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21)
< Date: Tue, 05 Dec 2017 13:06:08 GMT
< Connection: Keep-Alive
< Set-Cookie: _redmine_session=WHY1UGNwdHI4VE8wYXk5SjlRTk15YkYyU2RSZXUrZDRPMllnVFBReExoTGxBN0lhRnF6ZVI2WDR4YkI2Z0Y4M3N2Uk1OOURBYVJTNmQ0YVhLV2F1aGdxdjdTcE5MQUhRNFdQRXpLMTdmVU1UK2RaTkY0L0F4WEk0WldaRjAxVW5WTHNOQ2FQNElYeURkNlA0bTYzaGNRPT0tLUVSQ3RNMmtHYXhhdE44TzdxbmM0VWc9PQ%3D%3D--cbb59aa7ef006f47dbefe320b6121e2605e43f66; path=/; HttpOnly
< 
* Connection #0 to host 192.168.163.151 left intact
{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:06:08Z"}],"total_count":1,"offset":0,"limit":25}[root@mail ~]#

Http Post : 新增用户

使用命令:curl -v -H “Content-Type: application/json” -X POST –data-binary “@liumiaocn.json” -u admin:admin123 http://192.168.163.151:3000/users.json

使用json文件方式创建用户,创建用户的json文件信息如下:

[root@mail ~]# cat liumiaocn.json 
{
    "user": {
        "login": "liumiaocn",
        "firstname": "miao",
        "lastname": "liu",
        "mail": "liumiaocn@outlook.com",
        "password": "hello123" 
    }
}
[root@mail ~]#

执行结果如下,建议打开-v,这样能看到更多信息已进行排错。

[root@mail ~]# curl -v -H "Content-Type: application/json" -X POST --data-binary "@liumiaocn.json" -u admin:admin123 http://192.168.163.151:3000/users.json
* About to connect() to 192.168.163.151 port 3000 (#0)
*   Trying 192.168.163.151...
* Connected to 192.168.163.151 (192.168.163.151) port 3000 (#0)
* Server auth using Basic with user 'admin'
> POST /users.json HTTP/1.1
> Authorization: Basic YWRtaW46YWRtaW4xMjM=
> User-Agent: curl/7.29.0
> Host: 192.168.163.151:3000
> Accept: */*
> Content-Type: application/json
> Content-Length: 183
> 
* upload completely sent off: 183 out of 183 bytes
< HTTP/1.1 201 Created 
< X-Frame-Options: SAMEORIGIN
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Location: http://192.168.163.151:3000/users/5
< Content-Type: application/json; charset=utf-8
< Content-Length: 204
< Etag: W/"7cbd19185b3fd02c67cc3e22ec9ab1d7"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: 458e9f6e-e9c3-41ff-95a6-fefeb262ab9d
< X-Runtime: 0.736546
< Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21)
< Date: Tue, 05 Dec 2017 13:28:53 GMT
< Connection: Keep-Alive
< 
* Connection #0 to host 192.168.163.151 left intact
{"user":{"id":5,"login":"liumiaocn","firstname":"miao","lastname":"liu","mail":"liumiaocn@outlook.com","created_on":"2017-12-05T13:28:53Z","api_key":"4a075c22d3a89bd9ca7c2c0999538a86768ecae3","status":1}}[root@mail ~]#

再次确认,信息已经得到保存:

[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json
{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:31:47Z"},{"id":5,"login":"liumiaocn","firstname":"miao","lastname":"liu","mail":"liumiaocn@outlook.com","created_on":"2017-12-05T13:28:53Z"}],"total_count":2,"offset":0,"limit":25}[root@mail ~]#

另外使用刚刚创建的liumiaocn用户也可以进行登陆了
这里写图片描述

Http Put : 用户修改

使用命令:curl -v -H “Content-Type: application/json” -X PUT –data-binary “@liumiaocn.json” -u admin:admin123 http://192.168.163.151:3000/users/5.json
其中5为用户id, 我们接下来会使用Put方法将firstname从miao改成miaocn。

事前确认

[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json 2>/dev/null |grep liumiaocn
{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:39:16Z"},{"id":5,"login":"liumiaocn","firstname":"miao","lastname":"liu","mail":"liumiaocn@outlook.com","created_on":"2017-12-05T13:28:53Z","last_login_on":"2017-12-05T13:33:47Z"}],"total_count":2,"offset":0,"limit":25}
[root@mail ~]# 

修改对象内容

修改内容为firstname

[root@mail ~]# cat liumiaocn.json 
{
    "user": {
        "login": "liumiaocn",
        "firstname": "miaocn"
    }
}
[root@mail ~]#

执行更新

[root@mail ~]# curl -v -H "Content-Type: application/json" -X PUT --data-binary "@liumiaocn.json" -u admin:admin123 http://192.168.163.151:3000/users/5.json
* About to connect() to 192.168.163.151 port 3000 (#0)
*   Trying 192.168.163.151...
* Connected to 192.168.163.151 (192.168.163.151) port 3000 (#0)
* Server auth using Basic with user 'admin'
> PUT /users/5.json HTTP/1.1
> Authorization: Basic YWRtaW46YWRtaW4xMjM=
> User-Agent: curl/7.29.0
> Host: 192.168.163.151:3000
> Accept: */*
> Content-Type: application/json
> Content-Length: 84
> 
* upload completely sent off: 84 out of 84 bytes
< HTTP/1.1 200 OK 
< X-Frame-Options: SAMEORIGIN
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Type: application/json; charset=utf-8
< Content-Length: 0
< Cache-Control: no-cache
< X-Request-Id: 690f8890-5651-4c11-bad3-d854a10c926e
< X-Runtime: 0.175155
< Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21)
< Date: Tue, 05 Dec 2017 13:39:43 GMT
< Connection: Keep-Alive
< 
* Connection #0 to host 192.168.163.151 left intact
[root@mail ~]# 

结果确认

[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json 2>/dev/null |grep liumiaocn
{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:39:56Z"},{"id":5,"login":"liumiaocn","firstname":"miaocn","lastname":"liu","mail":"liumiaocn@outlook.com","created_on":"2017-12-05T13:28:53Z","last_login_on":"2017-12-05T13:33:47Z"}],"total_count":2,"offset":0,"limit":25}
[root@mail ~]# 

Http Delete

使用命令:curl -v -H “Content-Type: application/json” -X DELETE –data-binary “@liumiaocn.json” -u admin:admin123 http://192.168.163.151:3000/users/5.json
其中5为用户id, 我们接下来会删除此用户

事前确认

[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json 2>/dev/null |grep liumiaocn
{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:43:29Z"},{"id":5,"login":"liumiaocn","firstname":"miaocn","lastname":"liu","mail":"liumiaocn@outlook.com","created_on":"2017-12-05T13:28:53Z","last_login_on":"2017-12-05T13:33:47Z"}],"total_count":2,"offset":0,"limit":25}
[root@mail ~]# 

修改对象内容

修改内容为firstname

[root@mail ~]# cat liumiaocn.json 
{
    "user": {
        "login": "liumiaocn",
        "firstname": "miaocn"
    }
}
[root@mail ~]#

执行删除

[root@mail ~]# curl -v -H "Content-Type: application/json" -X DELETE --data-binary "@liumiaocn.json" -u admin:admin123 http://192.168.163.151:3000/users/5.json
* About to connect() to 192.168.163.151 port 3000 (#0)
*   Trying 192.168.163.151...
* Connected to 192.168.163.151 (192.168.163.151) port 3000 (#0)
* Server auth using Basic with user 'admin'
> DELETE /users/5.json HTTP/1.1
> Authorization: Basic YWRtaW46YWRtaW4xMjM=
> User-Agent: curl/7.29.0
> Host: 192.168.163.151:3000
> Accept: */*
> Content-Type: application/json
> Content-Length: 84
> 
* upload completely sent off: 84 out of 84 bytes
< HTTP/1.1 200 OK 
< X-Frame-Options: SAMEORIGIN
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Type: application/json; charset=utf-8
< Content-Length: 0
< Cache-Control: no-cache
< X-Request-Id: 016ec5d3-eef4-4db9-8813-28a21d0aa8c2
< X-Runtime: 0.453139
< Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21)
< Date: Tue, 05 Dec 2017 13:43:49 GMT
< Connection: Keep-Alive
< 
* Connection #0 to host 192.168.163.151 left intact
[root@mail ~]#

结果确认

[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json 2>/dev/null |grep liumiaocn
[root@mail ~]#

总结

这篇文章我们学习了如何使用curl和restapi以命令行的方式进行集成。

参考文章

http://www.redmine.org/projects/redmine/wiki/Rest_Users
http://www.redmine.org/projects/redmine/wiki/Rest_api_with_curl

淼叔 CSDN认证博客专家 神经网络 TensorFlow NLP
资深架构师,PMP、OCP、CSM、HPE University讲师,EXIN DevOps Professional与DevOps Master认证讲师,曾担任HPE GD China DevOps & Agile Leader,帮助企业级客户提供DevOps咨询培训以及实施指导。熟悉通信和金融领域,有超过十年金融外汇行业的架构设计、开发、维护经验,在十几年的IT从业生涯中拥有了软件开发设计领域接近全生命周期的经验和知识积累,著有企业级DevOps技术与工具实战。
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页