Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create the CreateAttribute functions #12

Closed
wants to merge 11 commits into from
62 changes: 25 additions & 37 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package appwrite

import (
"encoding/json"
"io/ioutil"
"io/ioutil"
"net/http"
"net/url"
"strings"
)

Expand Down Expand Up @@ -60,58 +59,47 @@ func (clt *Client) Call(method string, path string, headers map[string]interface
// Allow self signed requests
}

urlPath := clt.endpoint + path
isGet := strings.ToUpper(method) == "GET"

var reqBody *strings.Reader
if !isGet {
frm := url.Values{}
urlPath := clt.endpoint + path

var http_req *http.Request
var reqBody []byte
if isGet {
http_req, _ = http.NewRequest(strings.ToUpper(method), urlPath, strings.NewReader(string(reqBody)))
q := http_req.URL.Query()
for key, val := range params {
frm.Add(key, ToString(val))
q.Add(key, ToString(val))
}
reqBody = strings.NewReader(frm.Encode())
}

// Create and modify HTTP request before sending
req, err := http.NewRequest(method, urlPath, reqBody)
if err != nil {
return nil, err
http_req.URL.RawQuery = q.Encode()
} else {
reqBody, _ = json.Marshal(params)
http_req, _ = http.NewRequest(strings.ToUpper(method), urlPath, strings.NewReader(string(reqBody)))
}

// Set Client headers
// set client headers
for key, val := range clt.headers {
req.Header.Set(key, ToString(val))
http_req.Header.Set(key, val)
}

// Set Custom headers
// set custom headers
for key, val := range headers {
req.Header.Set(key, ToString(val))
http_req.Header.Set(key, ToString(val))
}

if isGet {
q := req.URL.Query()
for key, val := range params {
q.Add(key, ToString(val))
}
req.URL.RawQuery = q.Encode()
}

// Make request
response, err := clt.client.Do(req)
// submit the request
resp, err := clt.client.Do(http_req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

// Handle response
defer response.Body.Close()

responseData, err := ioutil.ReadAll(response.Body)
// read the response data
respData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var jsonResponse map[string]interface{}
json.Unmarshal(responseData, &jsonResponse)

return jsonResponse, nil
var jsonResp map[string]interface{}
json.Unmarshal(respData, &jsonResp)
return jsonResp, nil
}
Loading
Loading