added http handler.
This commit is contained in:
		
							
								
								
									
										16
									
								
								client.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								client.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| package revoltgo | ||||
|  | ||||
| import ( | ||||
| 	"github.com/sacOO7/gowebsocket" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| 	WS_URL  = "wss://ws.revolt.chat" | ||||
| 	API_URL = "https://api.revolt.chat" | ||||
| ) | ||||
|  | ||||
| // Client struct | ||||
| type Client struct { | ||||
| 	Token  string | ||||
| 	Socket gowebsocket.Socket | ||||
| } | ||||
							
								
								
									
										47
									
								
								http.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								http.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| package revoltgo | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"io/ioutil" | ||||
| 	"net/http" | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| // Send http request | ||||
| func (c *Client) Request(method, path, data string) ([]byte, error) { | ||||
| 	var reqBody *bytes.Buffer | ||||
|  | ||||
| 	// Check method | ||||
| 	if strings.EqualFold(method, "get") { | ||||
| 		reqBody = bytes.NewBuffer([]byte(data)) | ||||
| 	} else { | ||||
| 		reqBody = nil | ||||
| 	} | ||||
|  | ||||
| 	// Prepare request | ||||
| 	req, err := http.NewRequest(method, API_URL+path, reqBody) | ||||
| 	if err != nil { | ||||
| 		return []byte(""), err | ||||
| 	} | ||||
|  | ||||
| 	req.Header.Set("X-Bot-Token", c.Token) | ||||
| 	req.Header.Set("Content-Type", "application/json") | ||||
|  | ||||
| 	client := &http.Client{} | ||||
|  | ||||
| 	// Send request | ||||
| 	resp, err := client.Do(req) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return []byte(""), err | ||||
| 	} | ||||
|  | ||||
| 	defer resp.Body.Close() | ||||
| 	body, err := ioutil.ReadAll(resp.Body) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return []byte(""), err | ||||
| 	} | ||||
|  | ||||
| 	return body, nil | ||||
| } | ||||
							
								
								
									
										9
									
								
								types.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								types.go
									
									
									
									
									
								
							| @@ -1,9 +0,0 @@ | ||||
| package revoltgo | ||||
|  | ||||
| import "github.com/sacOO7/gowebsocket" | ||||
|  | ||||
| // Client struct | ||||
| type Client struct { | ||||
| 	Token  string | ||||
| 	Socket gowebsocket.Socket | ||||
| } | ||||
| @@ -7,8 +7,6 @@ import ( | ||||
| 	"github.com/sacOO7/gowebsocket" | ||||
| ) | ||||
|  | ||||
| const WS_URL = "wss://ws.revolt.chat" | ||||
|  | ||||
| func (c *Client) Start() { | ||||
| 	// Create new socket | ||||
| 	c.Socket = gowebsocket.New(WS_URL) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user