Created
February 27, 2013 17:29
-
-
Save oz/5049791 to your computer and use it in GitHub Desktop.
Quick and dirty Prowl API client in Go. You'd better use prowl libs on github, this is just my backup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"path" | |
"regexp" | |
"strings" | |
) | |
const ( | |
PROWL_API = "https://api.prowlapp.com/publicapi/add" | |
API_KEY_LENGTH = 40 | |
) | |
type ProwlNotification struct { | |
apiKey string | |
priority string | |
application string | |
event string | |
description string | |
} | |
func NewProwlNotification() *ProwlNotification { | |
return &ProwlNotification{priority: "0", | |
application: "GoProwl", | |
event: "GoProwl notification"} | |
} | |
func (n ProwlNotification) FormParameters() url.Values { | |
params := url.Values{} | |
params.Add("apikey", n.apiKey) | |
params.Add("priority", n.priority) | |
params.Add("application", n.application) | |
params.Add("event", n.event) | |
params.Add("description", n.description) | |
return params | |
} | |
func (n ProwlNotification) Send() bool { | |
resp, err := http.PostForm(PROWL_API, n.FormParameters()) | |
defer resp.Body.Close() | |
if err != nil { | |
log.Fatal(err) | |
return false | |
} | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatal(err) | |
return false | |
} | |
// Well, no I'm not parsing XML to check thoses responses. BOOH! | |
matched, err := regexp.Match("<success code=\"200\" ", body) | |
if err != nil || !matched { | |
if err != nil { | |
log.Fatal(err) | |
} | |
return false | |
} | |
return true | |
} | |
/** | |
* Read the user's API key from ~/.prowlrc, and complain if the file | |
* can't be read, or does not seem to contain an API key. | |
*/ | |
func ReadAPIKey() (apiKey string, err error) { | |
filename := os.Getenv("HOME") | |
file, err := os.Open(path.Join(filename, ".prowlrc")) | |
if err != nil { | |
return "", err | |
} | |
buf := make([]byte, API_KEY_LENGTH) | |
count, err := file.Read(buf) | |
if err != nil { | |
return "", err | |
} | |
if count != API_KEY_LENGTH { | |
return "", errors.New("Invalid API key") | |
} | |
apiKey = string(buf) | |
return apiKey, err | |
} | |
func main() { | |
apiKey, err := ReadAPIKey() | |
if err != nil { | |
fmt.Println("Can not read ~/.prowlrc file, make sure it exists ", | |
"and contains an API key.") | |
log.Fatal(err) | |
} | |
if 2 > len(os.Args) { | |
fmt.Println("Message needed!") | |
return | |
} | |
notif := NewProwlNotification() | |
notif.apiKey = apiKey | |
notif.description = strings.Join(os.Args[1:len(os.Args)], " ") | |
if notif.Send() { | |
fmt.Println("ok") | |
} else { | |
fmt.Println("ko") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment