优化注释
This commit is contained in:
parent
67a7002796
commit
d371cc09b3
@ -1,8 +1,8 @@
|
||||
/*
|
||||
* @FileName: auth.go
|
||||
* @Author: JJXu
|
||||
* @CreateTime: 2022/3/24 上午12:09
|
||||
* @Description:
|
||||
*FileName: auth.go
|
||||
*Author: JJXu
|
||||
*CreateTime: 2022/3/24 上午12:09
|
||||
*Description:
|
||||
*/
|
||||
|
||||
package simpleRequest
|
||||
@ -16,24 +16,20 @@ type Authorization struct {
|
||||
simpleReq *SimpleRequest
|
||||
}
|
||||
|
||||
//
|
||||
// Basic
|
||||
// @Description: 身份验证,使用bearer 令牌bearer 令牌
|
||||
// @receiver s
|
||||
// @param username
|
||||
// @param password
|
||||
//
|
||||
// Basic
|
||||
// Description: 身份验证,使用bearer 令牌bearer 令牌
|
||||
// receiver s
|
||||
// param username
|
||||
// param password
|
||||
func (s *Authorization) Bearer(token string) {
|
||||
s.simpleReq.headers.Set("Authorization", fmt.Sprintf("Bearer %v", token))
|
||||
}
|
||||
|
||||
//
|
||||
// Basic
|
||||
// @Description: 身份验证的基本验证方案
|
||||
// @receiver s
|
||||
// @param username
|
||||
// @param password
|
||||
//
|
||||
// Basic
|
||||
// Description: 身份验证的基本验证方案
|
||||
// receiver s
|
||||
// param username
|
||||
// param password
|
||||
func (s *Authorization) Basic(username, password string) {
|
||||
authStr := fmt.Sprintf("%v:%v", username, password)
|
||||
data := base64.StdEncoding.EncodeToString([]byte(authStr))
|
||||
|
8
body.go
8
body.go
@ -1,8 +1,8 @@
|
||||
/*
|
||||
* @FileName: body.go
|
||||
* @Author: JJXu
|
||||
* @CreateTime: 2022/3/2 上午1:23
|
||||
* @Description:
|
||||
*FileName: body.go
|
||||
*Author: JJXu
|
||||
*CreateTime: 2022/3/2 上午1:23
|
||||
*Description:
|
||||
*/
|
||||
|
||||
package simpleRequest
|
||||
|
@ -1,8 +1,8 @@
|
||||
/*
|
||||
* @FileName: simpleRequest_test.go
|
||||
* @Author: JJXu
|
||||
* @CreateTime: 2022/3/3 下午11:34
|
||||
* @Description:
|
||||
*FileName: simpleRequest_test.go
|
||||
*Author: JJXu
|
||||
*CreateTime: 2022/3/3 下午11:34
|
||||
*Description:
|
||||
*/
|
||||
|
||||
package excample
|
||||
@ -49,7 +49,7 @@ func TestRequest(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
//测试content-type 为 multipart/form-data格式的数据请求
|
||||
// 测试content-type 为 multipart/form-data格式的数据请求
|
||||
func TestAuth_fotmData(t *testing.T) {
|
||||
req := simpleRequest.NewRequest()
|
||||
req.Headers().ConentType_formData()
|
||||
@ -67,7 +67,7 @@ func TestAuth_fotmData(t *testing.T) {
|
||||
t.Log(string(data))
|
||||
}
|
||||
|
||||
//测试令牌验证
|
||||
// 测试令牌验证
|
||||
func TestAuthorization(t *testing.T) {
|
||||
req := simpleRequest.NewRequest()
|
||||
req.Authorization().Bearer("19f0591e-fab1-4447-90c3-1c60aef78fbd")
|
||||
|
25
headers.go
25
headers.go
@ -1,8 +1,8 @@
|
||||
/*
|
||||
* @FileName: header.go
|
||||
* @Author: JJXu
|
||||
* @CreateTime: 2022/3/1 下午9:44
|
||||
* @Description:
|
||||
*FileName: header.go
|
||||
*Author: JJXu
|
||||
*CreateTime: 2022/3/1 下午9:44
|
||||
*Description:
|
||||
*/
|
||||
|
||||
package simpleRequest
|
||||
@ -51,7 +51,7 @@ type HeadersConf struct {
|
||||
//-------------------------------------------------------------
|
||||
// Common key settings
|
||||
|
||||
//batch settings
|
||||
// batch settings
|
||||
func (s *HeadersConf) Sets(headers map[string]string) *HeadersConf {
|
||||
for k, v := range headers {
|
||||
s.simpleReq.headers.Set(k, v)
|
||||
@ -59,7 +59,7 @@ func (s *HeadersConf) Sets(headers map[string]string) *HeadersConf {
|
||||
return s
|
||||
}
|
||||
|
||||
//single setting
|
||||
// single setting
|
||||
func (s *HeadersConf) Set(header, value string) *HeadersConf {
|
||||
s.simpleReq.headers.Set(header, value)
|
||||
return s
|
||||
@ -87,13 +87,13 @@ func (s *HeadersConf) Add(header, value string) *HeadersConf {
|
||||
//-------------------------------------------------------------
|
||||
// base Key settings
|
||||
|
||||
//SetUserAgent
|
||||
// SetUserAgent
|
||||
func (s *HeadersConf) SetUserAgent(value string) *HeadersConf {
|
||||
s.simpleReq.headers.Set(hdrUserAgentKey, value)
|
||||
return s
|
||||
}
|
||||
|
||||
//SetConentType
|
||||
// SetConentType
|
||||
func (s *HeadersConf) SetConentType(value string) *HeadersConf {
|
||||
s.simpleReq.headers.Set(hdrContentTypeKey, value)
|
||||
return s
|
||||
@ -127,7 +127,6 @@ func (s *HeadersConf) ConentType_textPlain() *HeadersConf {
|
||||
return s
|
||||
}
|
||||
|
||||
//
|
||||
func (s *HeadersConf) SetConentLength(value string) *HeadersConf {
|
||||
s.simpleReq.headers.Set(hdrContentLengthKey, value)
|
||||
return s
|
||||
@ -141,22 +140,22 @@ func (s *HeadersConf) SetConentLocation(value string) *HeadersConf {
|
||||
return s
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// -------------------------------------------------------------
|
||||
// Extended settings
|
||||
//随机请求头的User-Agent参数
|
||||
// 随机请求头的User-Agent参数
|
||||
func (s *HeadersConf) getRandomUerAgent() string {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
index := rand.Intn(len(userAgentList))
|
||||
return userAgentList[index]
|
||||
}
|
||||
|
||||
//设置为随机 User-Agent
|
||||
// 设置为随机 User-Agent
|
||||
func (s *HeadersConf) SetRandomUerAgent() *HeadersConf {
|
||||
s.simpleReq.headers.Set(hdrUserAgentKey, s.getRandomUerAgent())
|
||||
return s
|
||||
}
|
||||
|
||||
//set Authorization
|
||||
// set Authorization
|
||||
func (s *HeadersConf) SetAuthorization(value string) *HeadersConf {
|
||||
s.simpleReq.headers.Set("Authorization", value)
|
||||
return s
|
||||
|
10
options.go
10
options.go
@ -1,14 +1,14 @@
|
||||
// Package simpleRequest -----------------------------
|
||||
// @file : options.go
|
||||
// @author : JJXu
|
||||
// @contact : wavingBear@163.com
|
||||
// @time : 2022/12/10 01:45:37
|
||||
// file : options.go
|
||||
// author : JJXu
|
||||
// contact : wavingBear@163.com
|
||||
// time : 2022/12/10 01:45:37
|
||||
// -------------------------------------------
|
||||
package simpleRequest
|
||||
|
||||
type OPTION func(r *SimpleRequest) *SimpleRequest
|
||||
|
||||
//OptionNewBodyEntryParser 新增或覆盖BodyEntryParser
|
||||
// OptionNewBodyEntryParser 新增或覆盖BodyEntryParser
|
||||
func OptionNewBodyEntryParser(contentType string, parser IBodyEntryParser) OPTION {
|
||||
return func(r *SimpleRequest) *SimpleRequest {
|
||||
r.bodyEntryParsers[contentType] = parser
|
||||
|
14
param.go
14
param.go
@ -1,8 +1,8 @@
|
||||
/*
|
||||
* @FileName: param.go
|
||||
* @Author: JJXu
|
||||
* @CreateTime: 2022/3/1 下午9:07
|
||||
* @Description:
|
||||
*FileName: param.go
|
||||
*Author: JJXu
|
||||
*CreateTime: 2022/3/1 下午9:07
|
||||
*Description:
|
||||
*/
|
||||
|
||||
package simpleRequest
|
||||
@ -16,7 +16,7 @@ type QueryParams struct {
|
||||
simpleReq *SimpleRequest
|
||||
}
|
||||
|
||||
//batch settings
|
||||
// batch settings
|
||||
func (s *QueryParams) Sets(data map[string]any) *QueryParams {
|
||||
for k, v := range data {
|
||||
s.simpleReq.queryParams.Set(k, fmt.Sprintf("%v", v))
|
||||
@ -24,13 +24,13 @@ func (s *QueryParams) Sets(data map[string]any) *QueryParams {
|
||||
return s
|
||||
}
|
||||
|
||||
//single settings
|
||||
// single settings
|
||||
func (s *QueryParams) Set(key string, value any) *QueryParams {
|
||||
s.simpleReq.queryParams.Set(key, fmt.Sprintf("%v", value))
|
||||
return s
|
||||
}
|
||||
|
||||
//get all queryParams
|
||||
// get all queryParams
|
||||
func (s *QueryParams) Gets() *url.Values {
|
||||
return &s.simpleReq.queryParams
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
// Package simpleRequest -----------------------------
|
||||
// @file : parser.go
|
||||
// @author : JJXu
|
||||
// @contact : wavingBear@163.com
|
||||
// @time : 2022/12/10 00:48:45
|
||||
// file : parser.go
|
||||
// author : JJXu
|
||||
// contact : wavingBear@163.com
|
||||
// time : 2022/12/10 00:48:45
|
||||
// -------------------------------------------
|
||||
package simpleRequest
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
/*
|
||||
* @FileName: simpleRequest.go
|
||||
* @Author: JJXu
|
||||
* @CreateTime: 2022/3/2 上午12:33
|
||||
* @Description:
|
||||
*FileName: simpleRequest.go
|
||||
*Author: JJXu
|
||||
*CreateTime: 2022/3/2 上午12:33
|
||||
*Description:
|
||||
*/
|
||||
|
||||
package simpleRequest
|
||||
@ -84,35 +84,35 @@ type SimpleRequest struct {
|
||||
//
|
||||
// 数据准备
|
||||
|
||||
//Authorization 添加令牌的方法集合
|
||||
// Authorization 添加令牌的方法集合
|
||||
func (s *SimpleRequest) Authorization() *Authorization {
|
||||
return &Authorization{
|
||||
simpleReq: s,
|
||||
}
|
||||
}
|
||||
|
||||
//Headers 添加请求头
|
||||
// Headers 添加请求头
|
||||
func (s *SimpleRequest) Headers() *HeadersConf {
|
||||
return &HeadersConf{
|
||||
simpleReq: s,
|
||||
}
|
||||
}
|
||||
|
||||
//Body 添加请求体
|
||||
// Body 添加请求体
|
||||
func (s *SimpleRequest) Body() *BodyConf {
|
||||
return &BodyConf{
|
||||
simpleReq: s,
|
||||
}
|
||||
}
|
||||
|
||||
//QueryParams 添加url后面的参数
|
||||
// QueryParams 添加url后面的参数
|
||||
func (s *SimpleRequest) QueryParams() *QueryParams {
|
||||
return &QueryParams{
|
||||
simpleReq: s,
|
||||
}
|
||||
}
|
||||
|
||||
//跳过证书验证
|
||||
// 跳过证书验证
|
||||
func (s *SimpleRequest) SkipCertVerify() *SimpleRequest {
|
||||
|
||||
s.transport = &http.Transport{
|
||||
@ -121,17 +121,17 @@ func (s *SimpleRequest) SkipCertVerify() *SimpleRequest {
|
||||
return s
|
||||
}
|
||||
|
||||
//设置超时时间
|
||||
// 设置超时时间
|
||||
func (s *SimpleRequest) TimeOut(t time.Duration) *SimpleRequest {
|
||||
s.timeout = t
|
||||
return s
|
||||
}
|
||||
|
||||
//------------------------------------------------------
|
||||
// ------------------------------------------------------
|
||||
//
|
||||
// 发送请求
|
||||
// 发送请求
|
||||
//
|
||||
//发送postt请求
|
||||
// 发送postt请求
|
||||
func (s *SimpleRequest) do(request *http.Request) (body []byte, err error) {
|
||||
//3. 建立http客户端
|
||||
client := &http.Client{
|
||||
@ -202,7 +202,7 @@ func (s *SimpleRequest) GET(urls string) (body []byte, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
//通用的请求方法
|
||||
// 通用的请求方法
|
||||
func (s *SimpleRequest) LaunchTo(urls, method string) (body []byte, err error) {
|
||||
// body
|
||||
s.initBody()
|
||||
@ -257,10 +257,9 @@ func (s *SimpleRequest) TRACE(url string) (body []byte, err error) {
|
||||
return s.LaunchTo(url, http.MethodTrace)
|
||||
}
|
||||
|
||||
//------------------------------------------------------
|
||||
//
|
||||
// 这里数据
|
||||
// ------------------------------------------------------
|
||||
//
|
||||
// 这里数据
|
||||
func (s *SimpleRequest) initBody() {
|
||||
contentTypeData := s.headers.Get(hdrContentTypeKey)
|
||||
switch {
|
||||
|
@ -1,8 +1,8 @@
|
||||
// Package simpleRequest -----------------------------
|
||||
// @file : simpleRequest_test.go
|
||||
// @author : JJXu
|
||||
// @contact : wavingBear@163.com
|
||||
// @time : 2022/12/9 20:34:52
|
||||
// file : simpleRequest_test.go
|
||||
// author : JJXu
|
||||
// contact : wavingBear@163.com
|
||||
// time : 2022/12/9 20:34:52
|
||||
// -------------------------------------------
|
||||
package simpleRequest
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user