Redmine之RestApi集成方式

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

Redmine

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

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

事前准备

docker方式启动

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

文章 链接
Ticket管理工具:Redmine http://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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5

启动Rest Web Service

需要在Redmine中做如下设定

详细可参照如下文章:

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

Rest api

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

Resource Status Availability
Issues Stable 1
Projects Stable 1
Project Memberships Alpha 1.4
Users Stable 1.1
Time Entries Stable 1.1
News Prototype 1.1
Issue Relations Alpha 1.3
Versions Alpha 1.3
Wiki Pages Alpha 2.2
Queries Alpha 1.3
Attachments Beta 1.3
Issue Statuses Alpha 1.3
Trackers Alpha 1.3
Enumerations Alpha 2.2
Issue Categories Alpha 1.3
Roles Alpha 1.4
Groups Alpha 2.1
Custom Fields Alpha 2.4
Search Alpha 3.3
Files Alpha 3.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 ~]# 
  • 1
  • 2
  • 3

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

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

[[email protected] ~]# 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":"[email protected]","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:06:08Z"}],"total_count":1,"offset":0,"limit":25}[[email protected] ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

[[email protected] ~]# 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":"[email protected]","created_on":"2017-12-05T13:28:53Z","api_key":"4a075c22d3a89bd9ca7c2c0999538a86768ecae3","status":1}}[[email protected] ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

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

[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 ~]#
  • 1
  • 2

另外使用刚刚创建的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 ~]# 
  • 1
  • 2
  • 3

修改对象内容

修改内容为firstname

[root@mail ~]# cat liumiaocn.json
{
    "user": {
        "login": "liumiaocn",
        "firstname": "miaocn"
    }
}
[root@mail ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

执行更新

[[email protected] ~]# 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
[[email protected] ~]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

结果确认

[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 ~]# 
  • 1
  • 2
  • 3

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 ~]# 
  • 1
  • 2
  • 3

修改对象内容

修改内容为firstname

[root@mail ~]# cat liumiaocn.json
{
    "user": {
        "login": "liumiaocn",
        "firstname": "miaocn"
    }
}
[root@mail ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

执行删除

[[email protected] ~]# 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
[[email protected] ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

结果确认

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

总结

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

参考文章

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

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

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

原文地址:https://www.cnblogs.com/firsttry/p/10294311.html

时间: 2024-10-08 15:55:04

Redmine之RestApi集成方式的相关文章

Confluence集成实践 3 RestAPI集成方式

上篇文章介绍了Confluence提供的标准的RestAPI主要涵括在那些功能领域.在这篇文章中将会继续使用实际的例子来介绍如何使用这些RestAPI. Rest Api Confluence的相关RestAPI在上篇文章中进行了仔细的介绍,具体参看 项目 详细信息 Confluence RestAPI介绍 http://blog.csdn.net/liumiaocn/article/details/77814943 创建空间 Confluence的使用,一般项目从创建一个空间开始,所以第一步,

对第三方库集成方式的分析

前一阵子做了iOS集成新浪微博SDK并实现部分功能的工作,这里正好将事后反思的内容总结一下,欢迎喷冷水. 假定需求是这样:在已经使用微博登陆的前提下,查找一个人的所有好友,筛选出其中年龄.性别等属性符合一定规则的人,然后查找这些人所有的微博,将这些微博按照一定的条件组织并且展示. 首先说明一下,查找好友和查找微博的做法,都是通过网络请求向新浪API请求数据,而登陆则可以通过SDK来简单实现. 那么,从上述描述当中,我们看到了两个大的用例,一个是登陆,一个是后面描述的一系列操作. 首先说登陆这件事

Reveal的LLDB集成方式

在上一篇文章中,我介绍了用framework的方式去配置Reveal,但是这种方式很繁琐,因为每分析一个工程你都得去添加一次Reveal的framework 并且配置一次Other Linker Flags 而且等你打包测试或者打包上架的时候你还要把Reveal的framework给剔出来.这里给大家分享一种新的方式来玩转Reveal集成->LLDB方式,这种方式就可以避免上面的问题.LLDB集成听上去挺 Heigher-Bigger的,其实是很简单的两步:配置路径.设置命令. 一.配置路径 先

Others-接口集成方式

1. 异步通信方式可分为不互锁.半互锁和全互锁三种类型: a.不互锁方式 主模块发出请求信号后,不等待接到从模块的回答信号,而是经过一段时间.确认从模块已收到请求信号后,便撤消其请求信号:从设备接到请求信号后,在条件允许时发出回答信号,并且经过一段时间,确认主设备已收到回答信号后,自动撤消回答信号.可见通信双方并无互锁关系. b.半互锁方式 主模块发出请求信号,待接到从模块的回答信号后再撤消其请求信号,存在着简单的互锁关系:而从模块发出回答信号后,不等待主模块回答,在一段时间后便撤消其回答信号,

SonarQube静态代码分析maven集成方式

1.在maven的setting.xml增加如下配置 <profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url>jdbc:mysql://10.10.13.7:3306/sonar?useUnicode=true&am

AEAI Portal-虚拟菜单方式配置SSO界面集成

1.前言 一般情况下虚拟菜单集成方式:先创建虚拟菜单节点,然后配置虚拟菜单的数据URL,同时指定显示页面,在显示页面中添加虚拟IframePortlet.而且:通常情况虚拟菜单是要依赖CAS认证的. 在本文中虚拟菜单集成方式,不依赖CAS认证,而是依赖于表单认证模式.主要区别是在于,显示页面配置的不再是虚拟IframePortlet,而是配置SSO界面集成Portlet (SSORedirectPortlet),具体如下. 2.虚拟菜单配置 2.1  流程机制说明 虚拟菜单配置包括虚拟菜单目录及

Spring 集成Hibernate的三种方式

首先把hibernate的配置文件hibernate.cfg.xml放入spring的src目录下,并且为了便于测试导入了一个实体类Student.java以及它的Student.hbm.xml文件 第一种集成方式:首先定义一个MySessionFactory的类 package com.tz.core; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.springfr

Centos6.5下redmine的安装配置

首先引用百度介绍下redmine: Redmine是用Ruby开发的基于web的项目管理软件,是用ROR框架开发的一套跨平台项目管理系统,据说是源于Basecamp的ror版而来,支持多种数据库,有不少自己独特的功能,例如提供wiki.新闻台等,还可以集成其他版本管理系统和BUG跟踪系统,例如Perforce.SVN.CVS.TD等等.这种 Web 形式的项目管理系统通过"项目(Project)"的形式把成员.任务(问题).文档.讨论以及各种形式的资源组织在一起,大家参与更新任务.文档

微服务集成——《微服务设计》读书笔记

一.理想的集成应该是什么样的? 1.避免破坏性修改 如果在一个微服务的响应中添加一个字段,服务的消费方不应该受到影响. 2.保证API的技术无关性 微服务之间的通信应该是与技术无关的. 3.使服务的消费方易于使用 如果消费方使用该服务比登天还难,那么无论该微服务多漂亮都没用任何意义.但同时,易于使用的服务可能内部封装了很多细节,这会增加耦合. 4.隐藏内部实现细节 消费方与服务方的内部细节应该是分开的,如果与细节绑定,则意味着改变服务内部的一些变化,消费方也要跟着修改,这会增加修改的成本. 二.