From 160f45b0a6ac38a3f35fff50c0daf2b25a0adb72 Mon Sep 17 00:00:00 2001 From: nevalla Date: Fri, 29 Aug 2014 23:47:42 +0300 Subject: [PATCH] add github.com/stvp/flowdock to Godeps --- Godeps/Godeps.json | 4 + .../src/github.com/stvp/flowdock/README.md | 53 +++++++++ .../src/github.com/stvp/flowdock/flowdock.go | 105 ++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 Godeps/_workspace/src/github.com/stvp/flowdock/README.md create mode 100644 Godeps/_workspace/src/github.com/stvp/flowdock/flowdock.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 0ca983eb7..6ded8527f 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -174,6 +174,10 @@ "Comment": "1.5.0-168-g2abea07", "Rev": "2abea075ec076abf0572d29bdb28ae7da64cfb7a" }, + { + "ImportPath": "github.com/stvp/flowdock", + "Rev": "50362abeabebf40b0f56a326d62f17e61a59302b" + }, { "ImportPath": "launchpad.net/goyaml", "Comment": "51", diff --git a/Godeps/_workspace/src/github.com/stvp/flowdock/README.md b/Godeps/_workspace/src/github.com/stvp/flowdock/README.md new file mode 100644 index 000000000..16da2b066 --- /dev/null +++ b/Godeps/_workspace/src/github.com/stvp/flowdock/README.md @@ -0,0 +1,53 @@ +flowdock +======== + +A Flowdock client for Golang. See the [Flowdock API docs][api_docs] for more +information on the individual attributes. + +[Go API Documentation][godocs] + +Examples +-------- + +You can set global defaults and use `flowdock.Inbox()` directly: + +```go +import ( + "github.com/stvp/flowdock" +) + +func main() { + flowdock.Token = "732da505d0284d5b1d909b1a32426345" + flowdock.Source = "My Cool App" + flowdock.FromAddress = "cool@dudes.com" + // See API docs for more variables + + go flowdock.Inbox("My subject", "My content goes here.") +} +``` + +Or you can create a `flowdock.Client` to use different Flowdock message +settings in the same app: + +```go +import ( + "github.com/stvp/flowdock" +) + +func main() { + client := flowdock.Client{ + Token: "732da505d0284d5b1d909b1a32426345", + Source: "App A", + FromAddress: "email@stovepipestudios.com", + FromName: "Client A", + ReplyTo: "app_a@stovepipestudios.com", + Tags: []string{"app_a"}, + } + + go client.Inbox("Subject", "Content") +} +``` + +[api_docs]: https://www.flowdock.com/api/team-inbox +[godocs]: http://godoc.org/github.com/stvp/flowdock + diff --git a/Godeps/_workspace/src/github.com/stvp/flowdock/flowdock.go b/Godeps/_workspace/src/github.com/stvp/flowdock/flowdock.go new file mode 100644 index 000000000..ba0c7835c --- /dev/null +++ b/Godeps/_workspace/src/github.com/stvp/flowdock/flowdock.go @@ -0,0 +1,105 @@ +package flowdock + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" +) + +const ( + ENDPOINT = "https://api.flowdock.com/v1/messages/team_inbox/" +) + +var ( + // Required default client settings + Token = "" + Source = "" + FromAddress = "" + + // Optional default client settings + FromName = "" + ReplyTo = "" + Project = "" + Link = "" + Tags = []string{} +) + +type Client struct { + // Required + Token string + Source string + FromAddress string + Subject string + Content string + + // Optional + FromName string + ReplyTo string + Project string + Link string + Tags []string +} + +func (c *Client) Inbox(subject, content string) error { + return send(c.Token, c.Source, c.FromAddress, subject, content, c.FromName, c.ReplyTo, c.Project, c.Link, c.Tags) +} + +func Inbox(subject, content string) error { + return send(Token, Source, FromAddress, subject, content, FromName, ReplyTo, Project, Link, Tags) +} + +func send(token, source, fromAddress, subject, content, fromName, replyTo, project, link string, tags []string) error { + // Required validation + if len(token) == 0 { + return fmt.Errorf(`"Token" is required`) + } + if len(source) == 0 { + return fmt.Errorf(`"Source" is required`) + } + if len(fromAddress) == 0 { + return fmt.Errorf(`"FromAddress" is required`) + } + if len(subject) == 0 { + return fmt.Errorf(`"Subject" is required`) + } + + // Build payload + payload := map[string]interface{}{ + "source": source, + "from_address": fromAddress, + "subject": subject, + "content": content, + } + if len(fromName) > 0 { + payload["from_name"] = fromName + } + if len(replyTo) > 0 { + payload["reply_to"] = replyTo + } + if len(project) > 0 { + payload["project"] = project + } + if len(link) > 0 { + payload["link"] = link + } + if len(tags) > 0 { + payload["tags"] = tags + } + jsonPayload, err := json.Marshal(payload) + if err != nil { + return err + } + + // Send to Flowdock + resp, err := http.Post(ENDPOINT+token, "application/json", bytes.NewReader(jsonPayload)) + defer resp.Body.Close() + + if resp.StatusCode == 200 { + return nil + } else { + bodyBytes, _ := ioutil.ReadAll(resp.Body) + return fmt.Errorf("Unexpected response from Flowdock: %s %s", resp.Status, string(bodyBytes)) + } +}