cleanup go util
This commit is contained in:
34
README.md
34
README.md
@ -53,11 +53,10 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PasteResponse represents the response from the paste API
|
|
||||||
type PasteResponse struct {
|
type PasteResponse struct {
|
||||||
Key string `json:"key"`
|
Key string `json:"key"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
@ -65,59 +64,40 @@ type PasteResponse struct {
|
|||||||
|
|
||||||
func uploadPaste(content string) (*PasteResponse, error) {
|
func uploadPaste(content string) (*PasteResponse, error) {
|
||||||
url := "https://paste.fascinated.cc/api/upload"
|
url := "https://paste.fascinated.cc/api/upload"
|
||||||
|
|
||||||
// Create a new POST request with the content as the body
|
|
||||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(content)))
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(content)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the Content-Type header
|
|
||||||
req.Header.Set("Content-Type", "text/plain")
|
req.Header.Set("Content-Type", "text/plain")
|
||||||
|
|
||||||
// Perform the request
|
resp, err := (&http.Client{}).Do(req)
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Read the response body
|
body, err := io.ReadAll(resp.Body)
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the request was successful
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
var errResponse map[string]interface{}
|
|
||||||
if err := json.Unmarshal(body, &errResponse); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("error: %v", errResponse["message"])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse the JSON response
|
|
||||||
var pasteResponse PasteResponse
|
var pasteResponse PasteResponse
|
||||||
if err := json.Unmarshal(body, &pasteResponse); err != nil {
|
if err := json.Unmarshal(body, &pasteResponse); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
pasteResponse.URL = "https://paste.fascinated.cc/" + pasteResponse.Key
|
||||||
// Add the URL field to the response
|
|
||||||
pasteResponse.URL = fmt.Sprintf("https://paste.fascinated.cc/%s", pasteResponse.Key)
|
|
||||||
|
|
||||||
return &pasteResponse, nil
|
return &pasteResponse, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
content := "Hello, World!"
|
content := "Hello, World!"
|
||||||
response, err := uploadPaste(content)
|
if response, err := uploadPaste(content); err != nil {
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error:", err)
|
fmt.Println("Error:", err)
|
||||||
return
|
} else {
|
||||||
|
fmt.Println("URL:", response.URL)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("URL: %s\n", response.URL)
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user