mirror of
https://github.com/ViViDboarder/gopush.git
synced 2024-11-23 15:16:33 +00:00
A little more betterr
This commit is contained in:
parent
6210a11486
commit
5defc63ce3
@ -81,7 +81,7 @@ func main() {
|
|||||||
config.Write()
|
config.Write()
|
||||||
|
|
||||||
if options.Push {
|
if options.Push {
|
||||||
pb.Push(options.Message)
|
pb.PushNote(options.Message)
|
||||||
} else if options.List {
|
} else if options.List {
|
||||||
devices := pb.GetDevices()
|
devices := pb.GetDevices()
|
||||||
PrintDevices(devices, pb.ActiveDevice)
|
PrintDevices(devices, pb.ActiveDevice)
|
||||||
|
@ -94,21 +94,26 @@ func (pb *PushBullet) SetActiveDevice(key string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Method for communicating with PushBullet
|
// Method for communicating with PushBullet
|
||||||
func (pb *PushBullet) pbRequest(method string, url string, body map[string]interface{}, result interface{}) error {
|
func (pb *PushBullet) pbRequest(method string, url string, body map[string]interface{}, result interface{}) (err error) {
|
||||||
if pb.client == nil {
|
if pb.client == nil {
|
||||||
pb.client = &http.Client{}
|
pb.client = &http.Client{}
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonBody, err := json.Marshal(body)
|
var r *http.Request
|
||||||
if err != nil {
|
if body == nil {
|
||||||
fmt.Println("Error marshalling body")
|
r, err = http.NewRequest(method, url, nil)
|
||||||
fmt.Println(err)
|
} else {
|
||||||
|
jsonBody, jsonError := json.Marshal(body)
|
||||||
|
if jsonError != nil {
|
||||||
|
fmt.Println("Error marshalling body: " + url)
|
||||||
|
return jsonError
|
||||||
|
}
|
||||||
|
r, err = http.NewRequest(method, url, bytes.NewBuffer(jsonBody))
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := http.NewRequest(method, url, bytes.NewBuffer(jsonBody))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error on request create")
|
fmt.Println("Error on request create: " + url)
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Header.Add("Content-Type", "application/json")
|
r.Header.Add("Content-Type", "application/json")
|
||||||
@ -119,19 +124,24 @@ func (pb *PushBullet) pbRequest(method string, url string, body map[string]inter
|
|||||||
|
|
||||||
resp, err := pb.client.Do(r)
|
resp, err := pb.client.Do(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error on request do")
|
fmt.Println("Error on request do: ")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
responseBody, err := ioutil.ReadAll(resp.Body)
|
responseBody, err := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error on request read")
|
fmt.Println("Error on response read: " + url)
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(responseBody, result)
|
err = json.Unmarshal(responseBody, result)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error on response parse: " + url)
|
||||||
|
fmt.Println("json: " + string(responseBody))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user