mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
6 Commits
v2.2.7
...
4b12173f24
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b12173f24 | ||
|
|
31c618c187 | ||
|
|
6497b321b0 | ||
|
|
bda78201f5 | ||
|
|
0753ea2801 | ||
|
|
e25b53712b |
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
## <a href="https://www.golancet.cn/en/" target="_blank"> Website</a> | [简体中文](./README_zh-CN.md)
|
## <a href="https://www.golancet.cn/en/" target="_blank"> Website</a> | [简体中文](./README_zh-CN.md)
|
||||||
|
|
||||||
## Feature
|
## Features
|
||||||
|
|
||||||
- 👏 Comprehensive, efficient and reusable.
|
- 👏 Comprehensive, efficient and reusable.
|
||||||
- 💪 600+ go util functions, support string, slice, datetime, net, crypt...
|
- 💪 600+ go util functions, support string, slice, datetime, net, crypt...
|
||||||
@@ -453,10 +453,14 @@ import "github.com/duke-git/lancet/v2/cryptor"
|
|||||||
[[play](https://go.dev/play/p/uef0q1fz53I)]
|
[[play](https://go.dev/play/p/uef0q1fz53I)]
|
||||||
- **<big>GenerateRsaKeyPair</big>** : creates rsa private and public key.
|
- **<big>GenerateRsaKeyPair</big>** : creates rsa private and public key.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/cryptor.md#GenerateRsaKeyPair)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/cryptor.md#GenerateRsaKeyPair)]
|
||||||
|
[[play](https://go.dev/play/p/sSVmkfENKMz)]
|
||||||
- **<big>RsaEncryptOAEP</big>** : encrypts the given data with RSA-OAEP.
|
- **<big>RsaEncryptOAEP</big>** : encrypts the given data with RSA-OAEP.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/cryptor.md#RsaEncryptOAEP)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/cryptor.md#RsaEncryptOAEP)]
|
||||||
|
[[play](https://go.dev/play/p/sSVmkfENKMz)]
|
||||||
- **<big>RsaDecryptOAEP</big>** : decrypts the data with RSA-OAEP
|
- **<big>RsaDecryptOAEP</big>** : decrypts the data with RSA-OAEP
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/cryptor.md#RsaDecryptOAEP)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/cryptor.md#RsaDecryptOAEP)]
|
||||||
|
[[play](https://go.dev/play/p/sSVmkfENKMz)]
|
||||||
|
|
||||||
|
|
||||||
<h3 id="datetime"> 7. Datetime package supports date and time format and compare. <a href="#index">index</a></h3>
|
<h3 id="datetime"> 7. Datetime package supports date and time format and compare. <a href="#index">index</a></h3>
|
||||||
|
|
||||||
@@ -1553,6 +1557,7 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
[[play](https://go.dev/play/p/6zXRH_c0Qd3)]
|
[[play](https://go.dev/play/p/6zXRH_c0Qd3)]
|
||||||
- **<big>IsNotBlank</big>** : checks if a string is not whitespace or not empty.
|
- **<big>IsNotBlank</big>** : checks if a string is not whitespace or not empty.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#IsNotBlank)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#IsNotBlank)]
|
||||||
|
[[play](https://go.dev/play/p/e_oJW0RAquA)]
|
||||||
- **<big>HasPrefixAny</big>** : checks if a string starts with any of an array of specified strings.
|
- **<big>HasPrefixAny</big>** : checks if a string starts with any of an array of specified strings.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#HasPrefixAny)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#HasPrefixAny)]
|
||||||
[[play](https://go.dev/play/p/8UUTl2C5slo)]
|
[[play](https://go.dev/play/p/8UUTl2C5slo)]
|
||||||
|
|||||||
@@ -453,10 +453,14 @@ import "github.com/duke-git/lancet/v2/cryptor"
|
|||||||
[[play](https://go.dev/play/p/uef0q1fz53I)]
|
[[play](https://go.dev/play/p/uef0q1fz53I)]
|
||||||
- **<big>GenerateRsaKeyPair</big>** : 创建rsa公钥私钥和key。
|
- **<big>GenerateRsaKeyPair</big>** : 创建rsa公钥私钥和key。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/cryptor.md#GenerateRsaKeyPair)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/cryptor.md#GenerateRsaKeyPair)]
|
||||||
|
[[play](https://go.dev/play/p/sSVmkfENKMz)]
|
||||||
- **<big>RsaEncryptOAEP</big>** : rsa OAEP加密。
|
- **<big>RsaEncryptOAEP</big>** : rsa OAEP加密。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/cryptor.md#RsaEncryptOAEP)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/cryptor.md#RsaEncryptOAEP)]
|
||||||
|
[[play](https://go.dev/play/p/sSVmkfENKMz)]
|
||||||
- **<big>RsaDecryptOAEP</big>** : rsa OAEP解密。
|
- **<big>RsaDecryptOAEP</big>** : rsa OAEP解密。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/cryptor.md#RsaDecryptOAEP)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/cryptor.md#RsaDecryptOAEP)]
|
||||||
|
[[play](https://go.dev/play/p/sSVmkfENKMz)]
|
||||||
|
|
||||||
|
|
||||||
<h3 id="datetime"> 7. datetime 日期时间处理包,格式化日期,比较日期。 <a href="#index">回到目录</a></h3>
|
<h3 id="datetime"> 7. datetime 日期时间处理包,格式化日期,比较日期。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
@@ -1560,6 +1564,7 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
[[play](https://go.dev/play/p/6zXRH_c0Qd3)]
|
[[play](https://go.dev/play/p/6zXRH_c0Qd3)]
|
||||||
- **<big>IsNotBlank</big>** : 检查字符串是否不为空。
|
- **<big>IsNotBlank</big>** : 检查字符串是否不为空。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#IsNotBlank)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#IsNotBlank)]
|
||||||
|
[[play](https://go.dev/play/p/e_oJW0RAquA)]
|
||||||
- **<big>HasPrefixAny</big>** : 检查字符串是否以指定字符串数组中的任何一个开头。
|
- **<big>HasPrefixAny</big>** : 检查字符串是否以指定字符串数组中的任何一个开头。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#HasPrefixAny)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#HasPrefixAny)]
|
||||||
[[play](https://go.dev/play/p/8UUTl2C5slo)]
|
[[play](https://go.dev/play/p/8UUTl2C5slo)]
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ func compareRefValue(operator string, leftObj, rightObj any, kind reflect.Kind)
|
|||||||
|
|
||||||
switch operator {
|
switch operator {
|
||||||
case equal:
|
case equal:
|
||||||
if bytes.Compare(bytesObj1, bytesObj2) == 0 {
|
if bytes.Equal(bytesObj1, bytesObj2) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
case lessThan:
|
case lessThan:
|
||||||
|
|||||||
@@ -508,14 +508,14 @@ func RsaDecrypt(data []byte, privateKeyFileName string) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GenerateRsaKeyPair create rsa private and public key.
|
// GenerateRsaKeyPair create rsa private and public key.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/sSVmkfENKMz
|
||||||
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey) {
|
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey) {
|
||||||
privateKey, _ := rsa.GenerateKey(rand.Reader, keySize)
|
privateKey, _ := rsa.GenerateKey(rand.Reader, keySize)
|
||||||
return privateKey, &privateKey.PublicKey
|
return privateKey, &privateKey.PublicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
// RsaEncryptOAEP encrypts the given data with RSA-OAEP.
|
// RsaEncryptOAEP encrypts the given data with RSA-OAEP.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/sSVmkfENKMz
|
||||||
func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error) {
|
func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error) {
|
||||||
encryptedBytes, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, &key, data, label)
|
encryptedBytes, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, &key, data, label)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -526,7 +526,7 @@ func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RsaDecryptOAEP decrypts the data with RSA-OAEP.
|
// RsaDecryptOAEP decrypts the data with RSA-OAEP.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/sSVmkfENKMz
|
||||||
func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error) {
|
func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error) {
|
||||||
decryptedBytes, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, &key, ciphertext, label)
|
decryptedBytes, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, &key, ciphertext, label)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1437,7 +1437,7 @@ func main() {
|
|||||||
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey)
|
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/sSVmkfENKMz)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -1462,7 +1462,7 @@ func main() {
|
|||||||
func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error)
|
func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/sSVmkfENKMz)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -1505,7 +1505,7 @@ func main() {
|
|||||||
func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error)
|
func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/sSVmkfENKMz)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|||||||
@@ -843,7 +843,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="ReadFile">ReadFile</span>
|
### <span id="ReadFile">ReadFile</span>
|
||||||
|
|
||||||
<p>读取文件或者URL</p>
|
<p>读取文件或者URL。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -851,7 +851,7 @@ func main() {
|
|||||||
func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error)
|
func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;"></span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/uNep3Tr8fqF)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -862,19 +862,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
reader, fn, err := ReadFile("https://httpbin.org/robots.txt")
|
reader, fn, err := fileutil.ReadFile("https://httpbin.org/robots.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer fn()
|
defer fn()
|
||||||
|
|
||||||
dat, err := io.ReadAll(reader)
|
dat, err := io.ReadAll(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(string(dat))
|
fmt.Println(string(dat))
|
||||||
// Output:
|
// Output:
|
||||||
// User-agent: *
|
// User-agent: *
|
||||||
// Disallow: /deny
|
// Disallow: /deny
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -1080,13 +1080,13 @@ func main() {
|
|||||||
|
|
||||||
<p>Checks if a string is not whitespace or not empty.</p>
|
<p>Checks if a string is not whitespace or not empty.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func IsNotBlank(str string) bool
|
func IsNotBlank(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/e_oJW0RAquA)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -1095,11 +1095,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := IsNotBlank("")
|
result1 := strutil.IsNotBlank("")
|
||||||
result2 := IsNotBlank(" ")
|
result2 := strutil.IsNotBlank(" ")
|
||||||
result3 := IsNotBlank("\t\v\f\n")
|
result3 := strutil.IsNotBlank("\t\v\f\n")
|
||||||
result4 := IsNotBlank(" 中文")
|
result4 := strutil.IsNotBlank(" 中文")
|
||||||
result5 := IsNotBlank(" world ")
|
result5 := strutil.IsNotBlank(" world ")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|||||||
@@ -1437,7 +1437,7 @@ func main() {
|
|||||||
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey)
|
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/sSVmkfENKMz)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -1462,7 +1462,7 @@ func main() {
|
|||||||
func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error)
|
func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/sSVmkfENKMz)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -1505,7 +1505,7 @@ func main() {
|
|||||||
func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error)
|
func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/sSVmkfENKMz)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|||||||
@@ -843,7 +843,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="ReadFile">ReadFile</span>
|
### <span id="ReadFile">ReadFile</span>
|
||||||
|
|
||||||
<p>Read File/URL</p>
|
<p>Read File or URL.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -851,7 +851,7 @@ func main() {
|
|||||||
func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error)
|
func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;"> </span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/uNep3Tr8fqF)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -862,19 +862,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
reader, fn, err := ReadFile("https://httpbin.org/robots.txt")
|
reader, fn, err := fileutil.ReadFile("https://httpbin.org/robots.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer fn()
|
defer fn()
|
||||||
|
|
||||||
dat, err := io.ReadAll(reader)
|
dat, err := io.ReadAll(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(string(dat))
|
fmt.Println(string(dat))
|
||||||
// Output:
|
// Output:
|
||||||
// User-agent: *
|
// User-agent: *
|
||||||
// Disallow: /deny
|
// Disallow: /deny
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1087,7 +1087,7 @@ func main() {
|
|||||||
func IsNotBlank(str string) bool
|
func IsNotBlank(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/e_oJW0RAquA)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -1096,11 +1096,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := IsNotBlank("")
|
result1 := strutil.IsNotBlank("")
|
||||||
result2 := IsNotBlank(" ")
|
result2 := strutil.IsNotBlank(" ")
|
||||||
result3 := IsNotBlank("\t\v\f\n")
|
result3 := strutil.IsNotBlank("\t\v\f\n")
|
||||||
result4 := IsNotBlank(" 中文")
|
result4 := strutil.IsNotBlank(" 中文")
|
||||||
result5 := IsNotBlank(" world ")
|
result5 := strutil.IsNotBlank(" world ")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|||||||
@@ -627,7 +627,7 @@ func WriteBytesToFile(filepath string, content []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadFile get file reader by a url or a local file
|
// ReadFile get file reader by a url or a local file
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/uNep3Tr8fqF
|
||||||
func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error) {
|
func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error) {
|
||||||
switch {
|
switch {
|
||||||
case validator.IsUrl(path):
|
case validator.IsUrl(path):
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ package netutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -110,22 +111,23 @@ type HttpClientConfig struct {
|
|||||||
Verbose bool
|
Verbose bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultHttpClientConfig defalut client config
|
// defaultHttpClientConfig defalut client config.
|
||||||
var defaultHttpClientConfig = &HttpClientConfig{
|
var defaultHttpClientConfig = &HttpClientConfig{
|
||||||
Compressed: false,
|
Compressed: false,
|
||||||
HandshakeTimeout: 20 * time.Second,
|
HandshakeTimeout: 20 * time.Second,
|
||||||
ResponseTimeout: 40 * time.Second,
|
ResponseTimeout: 40 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
// HttpClient is used for sending http request
|
// HttpClient is used for sending http request.
|
||||||
type HttpClient struct {
|
type HttpClient struct {
|
||||||
*http.Client
|
*http.Client
|
||||||
TLS *tls.Config
|
TLS *tls.Config
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
Config HttpClientConfig
|
Config HttpClientConfig
|
||||||
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHttpClient make a HttpClient instance
|
// NewHttpClient make a HttpClient instance.
|
||||||
func NewHttpClient() *HttpClient {
|
func NewHttpClient() *HttpClient {
|
||||||
client := &HttpClient{
|
client := &HttpClient{
|
||||||
Client: &http.Client{
|
Client: &http.Client{
|
||||||
@@ -141,7 +143,7 @@ func NewHttpClient() *HttpClient {
|
|||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHttpClientWithConfig make a HttpClient instance with pass config
|
// NewHttpClientWithConfig make a HttpClient instance with pass config.
|
||||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient {
|
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient {
|
||||||
if config == nil {
|
if config == nil {
|
||||||
config = defaultHttpClientConfig
|
config = defaultHttpClientConfig
|
||||||
@@ -176,6 +178,11 @@ func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, err
|
|||||||
rawUrl := request.RawURL
|
rawUrl := request.RawURL
|
||||||
|
|
||||||
req, err := http.NewRequest(request.Method, rawUrl, bytes.NewBuffer(request.Body))
|
req, err := http.NewRequest(request.Method, rawUrl, bytes.NewBuffer(request.Body))
|
||||||
|
|
||||||
|
if client.Context != nil {
|
||||||
|
req, err = http.NewRequestWithContext(client.Context, request.Method, rawUrl, bytes.NewBuffer(request.Body))
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -409,6 +409,7 @@ func IsBlank(str string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsNotBlank checks if a string is not whitespace, not empty.
|
// IsNotBlank checks if a string is not whitespace, not empty.
|
||||||
|
// Play: https://go.dev/play/p/e_oJW0RAquA
|
||||||
func IsNotBlank(str string) bool {
|
func IsNotBlank(str string) bool {
|
||||||
return !IsBlank(str)
|
return !IsBlank(str)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,8 +32,10 @@ func splitIntoStrings(s string, upperCase bool) []string {
|
|||||||
|
|
||||||
for i := 0; i < len(runes)-1; i++ {
|
for i := 0; i < len(runes)-1; i++ {
|
||||||
if isUpper(runes[i][0]) && isLower(runes[i+1][0]) {
|
if isUpper(runes[i][0]) && isLower(runes[i+1][0]) {
|
||||||
runes[i+1] = append([]rune{runes[i][len(runes[i])-1]}, runes[i+1]...)
|
length := len(runes[i]) - 1
|
||||||
runes[i] = runes[i][:len(runes[i])-1]
|
temp := runes[i][length]
|
||||||
|
runes[i+1] = append([]rune{temp}, runes[i+1]...)
|
||||||
|
runes[i] = runes[i][:length]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user