-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
87 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,76 @@ | ||
package misc | ||
|
||
import "github.com/go-resty/resty/v2" | ||
import ( | ||
"encoding/json" | ||
"github.com/go-resty/resty/v2" | ||
"log" | ||
"os" | ||
) | ||
|
||
func GetLc(key) { | ||
type LcResponseBody struct { | ||
Key string `json:"key"` | ||
Value string `json:"value"` | ||
CreatedAt string `json:"createdAt"` | ||
UpdatedAt string `json:"updatedAt"` | ||
ObjectId string `json:"objectId"` | ||
} | ||
|
||
func GetUrl(key string) string { | ||
return "https://8alqie7r.lc-cn-n1-shared.com/1.1/classes/options/" + key | ||
} | ||
|
||
func LcGet(optionKey string) LcResponseBody { | ||
url := GetUrl(optionKey) | ||
client := resty.New() | ||
// set header Lc_key, lc_id from os.get env | ||
lcId := os.Getenv("LC_ID") | ||
lcKey := os.Getenv("LC_KEY") | ||
client.SetHeaders(map[string]string{"X-LC-Id": lcId, "X-LC-Key": lcKey}) | ||
resp, err := client.R().Get(url) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
var lcResponseBody LcResponseBody | ||
json.Unmarshal(resp.Body(), &lcResponseBody) | ||
return lcResponseBody | ||
} | ||
|
||
// create item by key/value | ||
func LcPost(optionKey string, key, value string) LcResponseBody { | ||
url := GetUrl(optionKey) | ||
client := resty.New() | ||
resp, err := client.R().SetBody(map[string]string{ | ||
"key": key, | ||
"value": value, | ||
}).Post(url) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
var lcResponseBody LcResponseBody | ||
json.Unmarshal(resp.Body(), &lcResponseBody) | ||
return lcResponseBody | ||
} | ||
|
||
func LcPut(optionKey string, key, value string) LcResponseBody { | ||
url := GetUrl(optionKey) | ||
client := resty.New() | ||
resp, err := client.R().SetBody(map[string]string{ | ||
"key": key, | ||
"value": value, | ||
}).Put(url) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
var lcResponseBody LcResponseBody | ||
json.Unmarshal(resp.Body(), &lcResponseBody) | ||
return lcResponseBody | ||
} | ||
|
||
func LcDelete(optionKey string) { | ||
url := GetUrl(optionKey) | ||
client := resty.New() | ||
// format url | ||
// url := "https://8alqie7r.lc-cn-n1-shared.com/1.1/classes/options/" + key | ||
|
||
_, err := client.R().Delete(url) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
} |