add Global Url

master
李光春 2 years ago
parent 6c3420e9ab
commit 17e61153b0

@ -2,4 +2,4 @@ module github.com/dtapps/gorequest
go 1.18
require github.com/nilorg/sdk v0.0.0-20210429091026-95b6cdc95c84
require github.com/dtapps/gostring v1.0.1

@ -1,2 +1,2 @@
github.com/nilorg/sdk v0.0.0-20210429091026-95b6cdc95c84 h1:Nxk1uViXfb9MHgtHBlQFWzlQCsJbDQuotfTsAFcFP3o=
github.com/nilorg/sdk v0.0.0-20210429091026-95b6cdc95c84/go.mod h1:X1swpPdqguAZaBDoEPyEWHSsJii0YQ1o+3piMv6W3JU=
github.com/dtapps/gostring v1.0.1 h1:J04kndQ08LOqb+H41s8PbMEgNtpheV9iTTl7DSPIZVk=
github.com/dtapps/gostring v1.0.1/go.mod h1:BYYnZHrmwpFXkLpd9rQyV5YcChovVreacfcjyMKEwoU=

@ -22,13 +22,15 @@ type Response struct {
ResponseContentLength int64 //【返回】大小
}
type app struct {
type App struct {
Url string // 全局请求地址没有设置url才会使用
httpUrl string // 请求地址
httpMethod string // 请求方法
httpHeader Headers // 请求头
httpParams Params // 请求参数
httpParamsMode string // 请求参数方式
responseContent Response // 返回内容
Error error // 错误
}
var (
@ -37,25 +39,25 @@ var (
)
// NewHttp 实例化
func NewHttp() *app {
return &app{
func NewHttp() *App {
return &App{
httpHeader: NewHeaders(),
httpParams: NewParams(),
}
}
// SetUrl 设置请求地址
func (app *app) SetUrl(url string) {
func (app *App) SetUrl(url string) {
app.httpUrl = url
}
// SetMethod 设置请求方式地址
func (app *app) SetMethod(method string) {
func (app *App) SetMethod(method string) {
app.httpMethod = method
}
// SetHeader 设置请求头
func (app *app) SetHeader(key, value string) {
func (app *App) SetHeader(key, value string) {
if key == "" {
panic("url is empty")
}
@ -63,19 +65,19 @@ func (app *app) SetHeader(key, value string) {
}
// SetHeaders 批量设置请求头
func (app *app) SetHeaders(headers Headers) {
func (app *App) SetHeaders(headers Headers) {
for key, value := range headers {
app.httpHeader.Set(key, value)
}
}
// SetAuthToken 设置身份验证令牌
func (app *app) SetAuthToken(token string) {
func (app *App) SetAuthToken(token string) {
app.httpHeader.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}
// SetUserAgent 设置用户代理,空字符串就随机设置
func (app *app) SetUserAgent(ua string) {
func (app *App) SetUserAgent(ua string) {
if ua == "" {
ua = GetRandomUserAgent()
}
@ -83,50 +85,50 @@ func (app *app) SetUserAgent(ua string) {
}
// SetContentTypeJson 设置JSON格式
func (app *app) SetContentTypeJson() {
func (app *App) SetContentTypeJson() {
app.httpParamsMode = httpParamsModeJson
app.httpHeader.Set("Content-Type", "application/json")
}
// SetContentTypeForm 设置FORM格式
func (app *app) SetContentTypeForm() {
func (app *App) SetContentTypeForm() {
app.httpParamsMode = httpParamsModeForm
app.httpHeader.Set("Content-Type", "application/x-www-form-urlencoded")
}
// SetParam 设置请求参数
func (app *app) SetParam(key string, value interface{}) {
func (app *App) SetParam(key string, value interface{}) {
app.httpParams.Set(key, value)
}
// SetParams 批量设置请求参数
func (app *app) SetParams(params Params) {
func (app *App) SetParams(params Params) {
for key, value := range params {
app.httpParams.Set(key, value)
}
}
// Get 发起GET请求
func (app *app) Get() (httpResponse Response, err error) {
func (app *App) Get() (httpResponse Response, err error) {
// 设置请求方法
app.httpMethod = http.MethodGet
return request(app)
}
// Post 发起POST请求
func (app *app) Post() (httpResponse Response, err error) {
func (app *App) Post() (httpResponse Response, err error) {
// 设置请求方法
app.httpMethod = http.MethodPost
return request(app)
}
// Request 发起请求
func (app *app) Request() (httpResponse Response, err error) {
func (app *App) Request() (httpResponse Response, err error) {
return request(app)
}
// 请求
func request(app *app) (httpResponse Response, err error) {
func request(app *App) (httpResponse Response, err error) {
// 创建 http 客户端
client := &http.Client{}

@ -6,7 +6,7 @@ import (
"strings"
)
const Version = "1.0.1"
const Version = "1.0.2"
// ClientIp 尽最大努力实现获取客户端 IP 的算法。
// 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理nginx 或 haproxy可以正常工作。

@ -2,7 +2,7 @@ package gorequest
import (
"encoding/json"
"github.com/nilorg/sdk/convert"
"github.com/dtapps/gostring"
"log"
)
@ -30,7 +30,7 @@ func GetParamsString(src interface{}) string {
case int, int8, int32, int64:
case uint8, uint16, uint32, uint64:
case float32, float64:
return convert.ToString(src)
return gostring.ToString(src)
}
data, err := json.Marshal(src)
if err != nil {

@ -0,0 +1,8 @@
.env
.git
.svn
.idea
.vscode
*.log
gitmod.sh
*_test.go

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2018 Nil Org
Copyright (c) 2018 茂名聚合科技有限公司
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

@ -0,0 +1,25 @@
<h1>
<a href="https://www.dtapp.net/">Golang String</a>
</h1>
📦 Golang 字符串组件
[comment]: <> (go)
[![godoc](https://pkg.go.dev/badge/github.com/dtapps/gostring?status.svg)](https://pkg.go.dev/github.com/dtapps/gostring)
[![goproxy.cn](https://goproxy.cn/stats/github.com/dtapps/gostring/badges/download-count.svg)](https://goproxy.cn/stats/github.com/dtapps/gostring)
[![goreportcard.com](https://goreportcard.com/badge/github.com/dtapps/gostring)](https://goreportcard.com/report/github.com/dtapps/gostring)
[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/github.com%2Fdtapps%2Fgostring)
#### 安装使用
```go
go get -v -u github.com/dtapps/gostring
```
#### 导入
```go
import (
"github.com/dtapps/gostring"
)
```

@ -0,0 +1,151 @@
package gostring
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"strconv"
"strings"
"unicode/utf8"
)
const Version = "1.0.1"
// ToString 转换成string
func ToString(value interface{}) string {
if value == nil {
return ""
}
return fmt.Sprint(value)
}
// ToFloat64 string到float64
func ToFloat64(s string) float64 {
i, _ := strconv.ParseFloat(s, 64)
return i
}
// ToInt string到int
func ToInt(s string) int {
i, _ := strconv.Atoi(s)
return i
}
// ToInt64 string到int64
func ToInt64(s string) int64 {
i, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return i
}
return int64(ToFloat64(s))
}
// ToUint string到uint64
func ToUint(s string) uint {
i, err := strconv.ParseUint(s, 10, 64)
if err == nil {
return uint(i)
}
return 0
}
// ToUint64 string到uint64
func ToUint64(s string) uint64 {
i, err := strconv.ParseUint(s, 10, 64)
if err == nil {
return i
}
return 0
}
// Replace 字符串替换
func Replace(str, old, new string) string {
return strings.Replace(str, old, new, -1)
}
func HmacSha256Hex(key, strToSign string) string {
hasHer := hmac.New(sha256.New, []byte(key))
hasHer.Write([]byte(strToSign))
return hex.EncodeToString(hasHer.Sum(nil))
}
// Space 去除空格
func Space(str string) string {
return strings.Replace(str, " ", "", -1)
}
// LineBreak 去除换行符
func LineBreak(str string) string {
return strings.Replace(str, "\n", "", -1)
}
// SpaceAndLineBreak 去除空格和去除换行符
func SpaceAndLineBreak(str string) string {
return LineBreak(Space(str))
}
// TrimLastChar 删除字符串中的最后一个
func TrimLastChar(s string) string {
r, size := utf8.DecodeLastRuneInString(s)
if r == utf8.RuneError && (size == 0 || size == 1) {
size = 0
}
return s[:len(s)-size]
}
// Split 字符串分隔
func Split(s string, sep string) []string {
return strings.Split(s, sep)
}
// Contains 判断字符串是否包含某个字符
func Contains(s, substr string) bool {
return strings.Contains(s, substr)
}
func NumericalToString(value interface{}) (string, bool) {
var val string
switch value.(type) {
default:
return "0", false
case int:
intVal, _ := value.(int)
val = strconv.FormatInt(int64(intVal), 10)
case int8:
intVal, _ := value.(int8)
val = strconv.FormatInt(int64(intVal), 10)
case int16:
intVal, _ := value.(int16)
val = strconv.FormatInt(int64(intVal), 10)
case int32:
intVal, _ := value.(int32)
val = strconv.FormatInt(int64(intVal), 10)
case int64:
intVal, _ := value.(int64)
val = strconv.FormatInt(int64(intVal), 10)
case uint:
intVal, _ := value.(uint)
val = strconv.FormatUint(uint64(intVal), 10)
case uint8:
intVal, _ := value.(uint8)
val = strconv.FormatUint(uint64(intVal), 10)
case uint16:
intVal, _ := value.(uint16)
val = strconv.FormatUint(uint64(intVal), 10)
case uint32:
intVal, _ := value.(uint32)
val = strconv.FormatUint(uint64(intVal), 10)
case uint64:
intVal, _ := value.(uint64)
val = strconv.FormatUint(intVal, 10)
case float32:
floatVal, _ := value.(float32)
val = strconv.FormatFloat(float64(floatVal), 'f', -1, 32)
case float64:
floatVal, _ := value.(float64)
val = strconv.FormatFloat(floatVal, 'f', -1, 64)
}
return val, true
}

@ -1,112 +0,0 @@
package convert
import (
"bytes"
"encoding/binary"
"fmt"
"strconv"
)
// ToString 转换成string
func ToString(value interface{}) string {
if value == nil {
return ""
}
return fmt.Sprint(value)
}
// ToBool 转换成Bool
func ToBool(i interface{}) bool {
switch b := i.(type) {
case bool:
return b
case nil:
return false
case int:
if i.(int) != 0 {
return true
}
return false
case string:
v, err := strconv.ParseBool(ToString(i))
if err != nil {
return false
}
return v
default:
return false
}
}
// ToInt 转换成int
func ToInt(value interface{}) int {
return int(ToInt64(value))
}
// ToInt32 转换成int32
func ToInt32(value interface{}) int32 {
return int32(ToInt64(value))
}
// ToInt64 转换成int64
func ToInt64(value interface{}) int64 {
num, err := strconv.ParseInt(ToString(value), 10, 64)
if err != nil {
return 0
}
return num
}
// ToUint 转换成uint
func ToUint(value interface{}) uint {
return uint(ToUint64(value))
}
// ToUint32 转换成uint32
func ToUint32(value interface{}) uint32 {
return uint32(ToUint64(value))
}
// ToUint64 转换成uint64
func ToUint64(value interface{}) uint64 {
num, err := strconv.ParseUint(ToString(value), 10, 64)
if err != nil {
return 0
}
return num
}
// ToFloat32 转换成float32
func ToFloat32(value interface{}) float32 {
return float32(ToFloat64(value))
}
// ToFloat64 转换成float64
func ToFloat64(value interface{}) float64 {
num, err := strconv.ParseFloat(ToString(value), 64)
if err != nil {
return 0
}
return num
}
// BytesToInt32 字节转Int32
func BytesToInt32(data []byte) int32 {
var num int32
buffer := bytes.NewBuffer(data)
binary.Read(buffer, binary.BigEndian, &num)
return num
}
// BytesToInt 字节转Int
func BytesToInt(data []byte) int {
return int(BytesToInt32(data))
}
// BytesToInt64 字节转Int64
func BytesToInt64(data []byte) int64 {
var num int64
buffer := bytes.NewBuffer(data)
binary.Read(buffer, binary.BigEndian, &num)
return num
}

@ -1,3 +1,3 @@
# github.com/nilorg/sdk v0.0.0-20210429091026-95b6cdc95c84
## explicit; go 1.12
github.com/nilorg/sdk/convert
# github.com/dtapps/gostring v1.0.1
## explicit
github.com/dtapps/gostring

Loading…
Cancel
Save