补充http 协议
This commit is contained in:
parent
d2a4c831a4
commit
79431dc899
6
day10/README.md
Normal file
6
day10/README.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# HTTP 协议 与 Web框架
|
||||||
|
|
||||||
|
## 作业
|
||||||
|
+ Agent Log API
|
||||||
|
+ TCP/UDP 编程的Echo Server
|
||||||
|
+ Agent SDK(基于 http client)
|
||||||
172
devops/agent/client/README.md
Normal file
172
devops/agent/client/README.md
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
# 使用HTTP客户端 调用API Server
|
||||||
|
|
||||||
|
## 基于Curl 命令的调试方式
|
||||||
|
|
||||||
|
+ 任务执行
|
||||||
|
```sh
|
||||||
|
curl --location 'http://127.0.0.1:8848/api/devops_agent/v1/tasks' \
|
||||||
|
--header 'Content-Type: application/json' \
|
||||||
|
--data '{
|
||||||
|
"name": "task_debug",
|
||||||
|
"input_params": {}
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
+ 查询任务执行结果
|
||||||
|
```sh
|
||||||
|
curl --location 'http://127.0.0.1:8848/api/devops_agent/v1/tasks/6a75c2d0-7bdb-4f35-895a-fde49f8f7e93/result'
|
||||||
|
```
|
||||||
|
|
||||||
|
+ 查询任务执行日志
|
||||||
|
```sh
|
||||||
|
curl --location 'http://127.0.0.1:8848/api/devops_agent/v1/tasks/6a75c2d0-7bdb-4f35-895a-fde49f8f7e93/log?offset=20&limit=10'
|
||||||
|
```
|
||||||
|
|
||||||
|
## 基于Go代码来发起HTTP请求
|
||||||
|
|
||||||
|
+ 任务执行
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"net/http"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
url := "http://127.0.0.1:8848/api/devops_agent/v1/tasks"
|
||||||
|
method := "POST"
|
||||||
|
|
||||||
|
payload := strings.NewReader(`{
|
||||||
|
"name": "task_debug",
|
||||||
|
"input_params": {}
|
||||||
|
}`)
|
||||||
|
|
||||||
|
|
||||||
|
// 初始化一个http client对象
|
||||||
|
client := &http.Client {
|
||||||
|
}
|
||||||
|
|
||||||
|
// 准备 HTTP请求参数
|
||||||
|
req, err := http.NewRequest(method, url, payload)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
|
||||||
|
// 基于这个Req,发起HTTP请求
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
// 读取返回的结果
|
||||||
|
body, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(string(body))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
+ 查询任务执行结果
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
url := "http://127.0.0.1:8848/api/devops_agent/v1/tasks/6a75c2d0-7bdb-4f35-895a-fde49f8f7e93/result"
|
||||||
|
method := "GET"
|
||||||
|
|
||||||
|
client := &http.Client {
|
||||||
|
}
|
||||||
|
|
||||||
|
// 准备HTTP 请求
|
||||||
|
req, err := http.NewRequest(method, url, nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发起HTTP请求
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
// 读取结果
|
||||||
|
body, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(string(body))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
+ 查询任务执行日志
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
url := "http://127.0.0.1:8848/api/devops_agent/v1/tasks/6a75c2d0-7bdb-4f35-895a-fde49f8f7e93/log?offset=20&limit=10"
|
||||||
|
method := "GET"
|
||||||
|
|
||||||
|
client := &http.Client {
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest(method, url, nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(string(body))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 基于 http.Client 封装一个 SDK(Agent Client 对象)
|
||||||
|
|
||||||
|
+ 任务执行
|
||||||
|
+ 查询任务执行结果
|
||||||
|
+ 查询任务执行日志
|
||||||
Loading…
x
Reference in New Issue
Block a user