Skip to content

Commit

Permalink
separate request scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
UtopistMan committed Dec 25, 2021
1 parent 7e193d1 commit 74a8b91
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 123 deletions.
128 changes: 128 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package utopiago

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"

"gopkg.in/grignaak/tribool.v1"
)

func (c *UtopiaClient) apiQuery(methodName string, params map[string]interface{}) (map[string]interface{}, error) {
var responseMap map[string]interface{}
url := c.Protocol + "://" + c.Host + ":" + strconv.Itoa(c.Port) + "/api/1.0/"
var query = Query{
Method: methodName,
Token: c.Token,
}
if params != nil {
query.Params = params
}

var jsonStr, err = json.Marshal(query)
if err != nil {
return responseMap, errors.New("failed to dedcode response json: " + err.Error())
}

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return responseMap, err
}
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)

if !json.Valid(body) {
return responseMap, errors.New("failed to validate json from client")
}

json.Unmarshal(body, &responseMap)
return responseMap, nil
}

func (c *UtopiaClient) queryResultToInterface(methodName string, params map[string]interface{}) (interface{}, error) {
if !c.CheckClientConnection() {
return nil, errors.New("client disconected")
}
response, err := c.apiQuery(methodName, params)
if result, ok := response["result"]; ok {
return result, err
}
return nil, errors.New("result field doesn't exists in client response")
}

func (c *UtopiaClient) queryResultToInterfaceArray(methodName string, params map[string]interface{}) ([]interface{}, error) {
if !c.CheckClientConnection() {
return nil, errors.New("client disconected")
}
response, err := c.apiQuery(methodName, params)
if result, ok := response["result"]; ok {
//check type assertion
IResult, isConvertable := result.([]interface{})
if !isConvertable {
return nil, errors.New("failed to get result array")
}
return IResult, err
}
return nil, errors.New("accaptable result doesn't exists in client response")
}

func (c *UtopiaClient) queryResultToStringsArray(methodName string, params map[string]interface{}) ([]string, error) {
if !c.CheckClientConnection() {
return nil, errors.New("client disconected")
}
response, err := c.apiQuery(methodName, params)
if result, ok := response["result"]; ok {
//check type assertion
IResult, isConvertable := result.([]string)
if !isConvertable {
return nil, errors.New("failed to get result array")
}
return IResult, err
}
return nil, errors.New("accaptable result doesn't exists in client response")
}

func (c *UtopiaClient) queryResultToString(methodName string, params map[string]interface{}) (string, error) {
if !c.CheckClientConnection() {
return "", errors.New("client disconected")
}
response, err := c.apiQuery(methodName, params)
if result, ok := response["result"]; ok {
resultstr := fmt.Sprintf("%v", result)
return resultstr, err
}
return "", errors.New("result field doesn't exists in client response")
}

func (c *UtopiaClient) queryResultToBool(methodName string, params map[string]interface{}) (bool, error) {
resultstr, err := c.queryResultToString(methodName, params)
resultBool := tribool.FromString(resultstr).WithMaybeAsTrue()
return resultBool, err
}

func (c *UtopiaClient) queryResultToFloat64(methodName string, params map[string]interface{}) (float64, error) {
resultstr, err := c.queryResultToString(methodName, params)
if err != nil {
return 0, err
}
resultFloat, err := strconv.ParseFloat(resultstr, 64)
return resultFloat, err
}

func (c *UtopiaClient) queryResultToInt(methodName string, params map[string]interface{}) (int64, error) {
resultstr, err := c.queryResultToString(methodName, params)
if err != nil {
return 0, err
}
result, err := strconv.ParseInt(resultstr, 10, 64)
return result, err
}
123 changes: 0 additions & 123 deletions utopia.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package utopiago

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"

"gopkg.in/grignaak/tribool.v1"
)

// Query is a filter for API requests
Expand Down Expand Up @@ -43,42 +35,6 @@ type UtopiaClientInterface interface {
SendChannelPicture(channelID, base64Image, comment, filenameForImage string) (string, error)
}

func (c *UtopiaClient) apiQuery(methodName string, params map[string]interface{}) (map[string]interface{}, error) {
var responseMap map[string]interface{}
url := c.Protocol + "://" + c.Host + ":" + strconv.Itoa(c.Port) + "/api/1.0/"
var query = Query{
Method: methodName,
Token: c.Token,
}
if params != nil {
query.Params = params
}

var jsonStr, err = json.Marshal(query)
if err != nil {
return responseMap, errors.New("failed to dedcode response json: " + err.Error())
}

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return responseMap, err
}
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)

if !json.Valid(body) {
return responseMap, errors.New("failed to validate json from client")
}

json.Unmarshal(body, &responseMap)
return responseMap, nil
}

// GetProfileStatus gets data about the status of the current account
func (c *UtopiaClient) GetProfileStatus() (map[string]interface{}, error) {
return c.apiQuery("getProfileStatus", nil)
Expand All @@ -89,85 +45,6 @@ func (c *UtopiaClient) GetSystemInfo() (map[string]interface{}, error) {
return c.apiQuery("getSystemInfo", nil)
}

func (c *UtopiaClient) queryResultToInterface(methodName string, params map[string]interface{}) (interface{}, error) {
if !c.CheckClientConnection() {
return nil, errors.New("client disconected")
}
response, err := c.apiQuery(methodName, params)
if result, ok := response["result"]; ok {
return result, err
}
return nil, errors.New("result field doesn't exists in client response")
}

func (c *UtopiaClient) queryResultToInterfaceArray(methodName string, params map[string]interface{}) ([]interface{}, error) {
if !c.CheckClientConnection() {
return nil, errors.New("client disconected")
}
response, err := c.apiQuery(methodName, params)
if result, ok := response["result"]; ok {
//check type assertion
IResult, isConvertable := result.([]interface{})
if !isConvertable {
return nil, errors.New("failed to get result array")
}
return IResult, err
}
return nil, errors.New("accaptable result doesn't exists in client response")
}

func (c *UtopiaClient) queryResultToStringsArray(methodName string, params map[string]interface{}) ([]string, error) {
if !c.CheckClientConnection() {
return nil, errors.New("client disconected")
}
response, err := c.apiQuery(methodName, params)
if result, ok := response["result"]; ok {
//check type assertion
IResult, isConvertable := result.([]string)
if !isConvertable {
return nil, errors.New("failed to get result array")
}
return IResult, err
}
return nil, errors.New("accaptable result doesn't exists in client response")
}

func (c *UtopiaClient) queryResultToString(methodName string, params map[string]interface{}) (string, error) {
if !c.CheckClientConnection() {
return "", errors.New("client disconected")
}
response, err := c.apiQuery(methodName, params)
if result, ok := response["result"]; ok {
resultstr := fmt.Sprintf("%v", result)
return resultstr, err
}
return "", errors.New("result field doesn't exists in client response")
}

func (c *UtopiaClient) queryResultToBool(methodName string, params map[string]interface{}) (bool, error) {
resultstr, err := c.queryResultToString(methodName, params)
resultBool := tribool.FromString(resultstr).WithMaybeAsTrue()
return resultBool, err
}

func (c *UtopiaClient) queryResultToFloat64(methodName string, params map[string]interface{}) (float64, error) {
resultstr, err := c.queryResultToString(methodName, params)
if err != nil {
return 0, err
}
resultFloat, err := strconv.ParseFloat(resultstr, 64)
return resultFloat, err
}

func (c *UtopiaClient) queryResultToInt(methodName string, params map[string]interface{}) (int64, error) {
resultstr, err := c.queryResultToString(methodName, params)
if err != nil {
return 0, err
}
result, err := strconv.ParseInt(resultstr, 10, 64)
return result, err
}

// SetProfileStatus updates data about the status of the current account
func (c *UtopiaClient) SetProfileStatus(status string, mood string) error {
queryMap := make(map[string]interface{})
Expand Down

0 comments on commit 74a8b91

Please sign in to comment.