Go语言resty http包调用jenkins api实例

作者:爱吃红薯粉 时间:2024-05-21 10:27:27 

前言

在前两篇文章中都使用HttpRequest这个http包来做api的请求

Go语言resty http包调用jenkins api实例

然后github上面还有一个更有名,星星更多,社区也更活跃的http包Resty

最近11月3号又发布了一个新版本,项目参与者多达75个,标星有5.2k

Resty特色

  • 支持GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS等方法

  • 设置和请求的方法简单

  • 请求体支持多种数据类型(string,[]byte,struct,map,slice,io.Reader)

  • 返回支持[]byte和string

  • 对json和xml内容自动编码和解码

  • 支持上传一个多个文件和下载指定路径或打包

  • 请求参数支持QueryParams,QueryString,FormData

  • 支持重试,代理,证书

  • 支持简单认证和认证token

Resty官网: https://github.com/go-resty/resty

演示例子

演示部分例子get,post等例子,其它put,delete,patch都差不多

简单get请求

该示例显示请求状态和响应耗时

func simpleGet() {
  client := resty.New()
  resp, _ := client.R().EnableTrace().Get("https://httpbin.org/get")
  fmt.Println("状态码:", resp.StatusCode())
  fmt.Println("总耗时:", resp.Time())
}

增强get请求

该示例支持map类型和路径参数,支持设置请求头和认证token

func enhancedGet() {
  client := resty.New()
  resp, _ := client.R().
     SetQueryParams(map[string]string{
        "page_no": "1",
        "limit":   "20",
        "sort":    "name",
        "order":   "asc",
        "random":  strconv.FormatInt(time.Now().Unix(), 10),
     }).
     //SetQueryString("productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more").
     SetHeader("Accept", "application/json").
     SetAuthToken("xxxxxxxxxxxxxxxxxxxxxxx").
     Get("/search_result")
  fmt.Println(resp)
}

灵活post请求

该示例支持设置body内容为map支持简单认证和token认证

func variousPost() {
  client := resty.New()
  resp, err := client.R().
     SetBody(map[string]interface{}{"username": "testuser", "password": "testpass"}).
     SetAuthToken("<your-auth-token>").
     Post("https://myapp.com/login")
  fmt.Println(resp, err)
}

多文件上传

该示例支持多文件上传,支持定义本地文件路径,支持设置FormData数据

func multipartFileUpload() {
  profileImgBytes, _ := ioutil.ReadFile("/Users/jeeva/test-img.png")
  notesBytes, _ := ioutil.ReadFile("/Users/jeeva/text-file.txt")
  client := resty.New()
  resp, _ := client.R().
     SetFileReader("profile_img", "test-img.png", bytes.NewReader(profileImgBytes)).
     SetFileReader("notes", "text-file.txt", bytes.NewReader(notesBytes)).
     SetFormData(map[string]string{
        "first_name": "dddd",
        "last_name": "cccc",
     }).
     Post("http://myapp.com/upload")
  fmt.Println(resp)
}

文件下载

定义下载保存路径,直接下载

func downFile() {
  client := resty.New()
  _, _ = client.R().
     SetOutput("plugin/test-v5.1-beta.zip").
     Get("http://my.app/test")
}

实战例子

通过上面的演示例子,可以看到resty这个包功能非常强大

接下来我们使用该包来封装jenkins api,来进行二次开发

构造一个jenkins客户端

编写一个方法创建jenkins客户端,后续所有动作只需调用既可

该客户端集成了重试,json头,以及简单认证

func jenkinsClient() *resty.Request {
  c := resty.New()
  a := c.SetRetryCount(3).SetRetryWaitTime(5 * time.Second).
     SetRetryMaxWaitTime(20 * time.Second).
     R().SetHeader("Accept", "application/json").
     SetBasicAuth("username", "password")
  return a
}
var (
  url = "http://ip:port"
)

获取jenkins job信息

创建好jobinfo的结构体,方便接下来的数据接收

type JobInfo struct {
  DisplayName string
  FullName string
  Buildable bool
  NextBuildNumber int
  InQueue bool
  Color string //blue成功 red失败
  Url string
}

resty会将返回数据解码,只需要提供结构体接收

func jenkinsJobInfo(job string) {
  a := jenkinsClient()
  jobInfo := &JobInfo{}
  resp, err := a.SetResult(jobInfo).Get(url + "/job/" + job + "/api/json")
  if err != nil {
     fmt.Println(err)
  }
  fmt.Println("resp\n", resp.StatusCode(), resp.Time())
  fmt.Println(jobInfo.Color)
}

无参构建job

简单post请求,调用无参数构建

func jenkinsJobBuild(job string)  {
  a := jenkinsClient()
  resp, _  := a.Post(url+"/job/"+job+"/build")
  if resp.StatusCode() == 201 {
     fmt.Println("build 成功")
  }
}

查看构建日志

查看日志,需要先获取job的最后一次的buildID

所以这里发请2次请求,第1次获取buildID,第2次获取日志内容

func jenkinsJobLog(job string)  {
  a := jenkinsClient()
  resp, _ := a.Get(url + "/job/" + job + "/lastBuild/buildNumber")
  if resp.StatusCode() == 200 {
     lastBuild := resp.String()
     resp2, _ := a.Get(url + "/job/" + job + "/" + lastBuild + "/logText/progressiveText")
     fmt.Println(resp2.String())
  }
}

job开关(启用禁用job)

第一个参数为job名称,第二个参数设定开关值

func jenkinsJobSwich(job string,swi bool) {
  a := jenkinsClient()
  if swi {
     resp, _ := a.Post(url + "/job/" + job + "/enable")
     fmt.Println(resp.Status())
  } else {
     resp, _ := a.Post(url + "/job/" + job + "/disable")
     fmt.Println(resp.Status())
  }
}

小结

通过实战可以发现,在没有第三方sdk的时候,完全是可以自已通过使用http包,来进行http api项目的二次开发或封装

而resty则是Go http包中的王者

来源:https://juejin.cn/post/7035800426293231653

标签:Go,resty,http,jenkins,api调用
0
投稿

猜你喜欢

  • MySQL 处理插入过程中的主键唯一键重复值的解决方法

    2024-01-23 10:33:06
  • Python多线程入门学习

    2021-10-11 00:26:09
  • 详解python中docx库的安装过程

    2023-01-21 18:32:36
  • SQL SERVER 2014 安装图解教程(含SQL SERVER 2014下载)

    2024-01-16 05:16:07
  • 在SQL Server 2005数据库中进行错误捕捉

    2008-12-02 14:39:00
  • 胜过语言的图形符号

    2009-05-06 12:43:00
  • Python快速生成随机密码超简单实现

    2022-08-07 19:26:09
  • python linecache读取行更新的实现

    2021-01-26 01:33:06
  • Python的join函数的用法及实例

    2023-03-08 12:39:28
  • asp简单可逆运算字符串加密解密函数

    2010-05-04 16:42:00
  • python解决pandas处理缺失值为空字符串的问题

    2021-10-21 09:01:38
  • 通过字符串导入 Python 模块的方法详解

    2023-10-15 03:00:56
  • 分析SQL Server中数据库的快照工作原理

    2009-01-19 14:03:00
  • python标准库random模块处理随机数

    2023-11-23 16:22:49
  • JavaScript正则表达式验证中文实例讲解

    2024-04-10 10:55:59
  • SQL Server如何才能访问Sybase中的表

    2009-01-08 13:33:00
  • opencv实现图像缩放效果

    2022-10-24 04:52:28
  • Python字典创建 遍历 添加等实用基础操作技巧

    2021-05-23 22:22:31
  • Python基于递归实现电话号码映射功能示例

    2023-11-15 09:38:39
  • 10个精致的导航菜单欣赏及点评

    2011-09-22 20:33:44
  • asp之家 网络编程 m.aspxhome.com