Documentation ¶
Overview ¶
Package firego is a REST client for Firebase (https://firebase.com).
Index ¶
- Constants
- Variables
- type ErrTimeout
- type Event
- type Firebase
- func (fb *Firebase) Auth(token string)
- func (fb *Firebase) Child(child string) *Firebase
- func (fb *Firebase) EndAt(value string) *Firebase
- func (fb *Firebase) IncludePriority(v bool)
- func (fb *Firebase) OrderBy(value string) *Firebase
- func (fb *Firebase) Push(v interface{}) (*Firebase, error)
- func (fb *Firebase) Remove() error
- func (fb *Firebase) Set(v interface{}) error
- func (fb *Firebase) Shallow(v bool)
- func (fb *Firebase) StartAt(value string) *Firebase
- func (fb *Firebase) StopWatching()
- func (fb *Firebase) String() string
- func (fb *Firebase) Unauth()
- func (fb *Firebase) Update(v interface{}) error
- func (fb *Firebase) Value(v interface{}) error
- func (fb *Firebase) Watch(notifications chan Event) error
Examples ¶
Constants ¶
const EventTypeError = "event_error"
EventTypeError is the type that is set on an Event struct if an error occurs while watching a Firebase reference
Variables ¶
var TimeoutDuration = 30 * time.Second
TimeoutDuration is the length of time any request will have to establish a connection and receive headers from Firebase before returning an ErrTimeout error
Functions ¶
This section is empty.
Types ¶
type ErrTimeout ¶
type ErrTimeout struct {
// contains filtered or unexported fields
}
ErrTimeout is an error type is that is returned if a request exceeds the TimeoutDuration configured
type Event ¶
type Event struct { // Type of event that was received Type string // Path to the data that changed Path string // Data that changed Data interface{} }
Event represents a notification received when watching a firebase reference
type Firebase ¶
type Firebase struct {
// contains filtered or unexported fields
}
Firebase represents a location in the cloud
func (*Firebase) Auth ¶
Auth sets the custom Firebase token used to authenticate to Firebase
Example ¶
package main import ( "github.com/CloudCom/firego" ) func main() { fb := firego.New("https://someapp.firebaseio.com") fb.Auth("my-token") }
Output:
func (*Firebase) Child ¶
Child creates a new Firebase reference for the requested child with the same configuration as the parent
Example ¶
package main import ( "log" "github.com/CloudCom/firego" ) func main() { fb := firego.New("https://someapp.firebaseio.com") childFB := fb.Child("some/child/path") log.Printf("My new ref %s\n", childFB) }
Output:
func (*Firebase) EndAt ¶
EndAt creates a new Firebase reference with the requested EndAt configuration
func (*Firebase) IncludePriority ¶
IncludePriority determines whether or not to ask Firebase for the values priority. By default, the priority is not returned
Reference https://www.firebase.com/docs/rest/api/#section-param-format
Example ¶
package main import ( "github.com/CloudCom/firego" ) func main() { fb := firego.New("https://someapp.firebaseio.com") // turn on fb.IncludePriority(true) // turn off fb.IncludePriority(false) }
Output:
func (*Firebase) OrderBy ¶
OrderBy creates a new Firebase reference with the requested OrderBy configuration
func (*Firebase) Push ¶
Push creates a reference to an auto-generated child location
Example ¶
package main import ( "log" "github.com/CloudCom/firego" ) func main() { fb := firego.New("https://someapp.firebaseio.com") newRef, err := fb.Push("my-value") if err != nil { log.Fatal(err) } log.Printf("My new ref %s\n", newRef) }
Output:
func (*Firebase) Remove ¶
Remove the Firebase reference from the cloud
Example ¶
package main import ( "log" "github.com/CloudCom/firego" ) func main() { fb := firego.New("https://someapp.firebaseio.com/some/value") if err := fb.Remove(); err != nil { log.Fatal(err) } }
Output:
func (*Firebase) Set ¶
Set the value of the Firebase reference
Example ¶
package main import ( "log" "github.com/CloudCom/firego" ) func main() { fb := firego.New("https://someapp.firebaseio.com") v := map[string]interface{}{ "foo": "bar", "bar": 1, "bez": []string{"hello", "world"}, } if err := fb.Set(v); err != nil { log.Fatal(err) } }
Output:
func (*Firebase) Shallow ¶
Shallow limits the depth of the data returned when calling Value. If the data at the location is a JSON primitive (string, number or boolean), its value will be returned. If the data is a JSON object, the values for each key will be truncated to true.
Reference https://www.firebase.com/docs/rest/api/#section-param-shallow
Example ¶
package main import ( "github.com/CloudCom/firego" ) func main() { fb := firego.New("https://someapp.firebaseio.com") // turn on fb.Shallow(true) // turn off fb.Shallow(false) }
Output:
func (*Firebase) StartAt ¶
StartAt creates a new Firebase reference with the requested StartAt configuration
func (*Firebase) StopWatching ¶
func (fb *Firebase) StopWatching()
StopWatching stops tears down all connections that are watching
Example ¶
package main import ( "log" "time" "github.com/CloudCom/firego" ) func main() { fb := firego.New("https://someapp.firebaseio.com/some/value") notifications := make(chan firego.Event) if err := fb.Watch(notifications); err != nil { log.Fatal(err) } go func() { for _ = range notifications { } log.Println("Channel closed") }() time.Sleep(10 * time.Millisecond) // let go routine start fb.StopWatching() }
Output:
func (*Firebase) Unauth ¶
func (fb *Firebase) Unauth()
Unauth removes the current token being used to authenticate to Firebase
func (*Firebase) Update ¶
Update the specific child with the given value
Example ¶
package main import ( "log" "github.com/CloudCom/firego" ) func main() { fb := firego.New("https://someapp.firebaseio.com/some/value") if err := fb.Update("new-value"); err != nil { log.Fatal(err) } }
Output:
func (*Firebase) Value ¶
Value gets the value of the Firebase reference
Example ¶
package main import ( "log" "github.com/CloudCom/firego" ) func main() { fb := firego.New("https://someapp.firebaseio.com/some/value") var v interface{} if err := fb.Value(v); err != nil { log.Fatal(err) } log.Printf("My value %v\n", v) }
Output:
func (*Firebase) Watch ¶
Watch listens for changes on a firebase instance and passes over to the given chan.
Only one connection can be established at a time. The second call to this function without a call to fb.StopWatching will close the channel given and return nil immediately
Example ¶
package main import ( "log" "github.com/CloudCom/firego" ) func main() { fb := firego.New("https://someapp.firebaseio.com/some/value") notifications := make(chan firego.Event) if err := fb.Watch(notifications); err != nil { log.Fatal(err) } for event := range notifications { log.Println("Event Received") log.Printf("Type: %s\n", event.Type) log.Printf("Path: %s\n", event.Path) log.Printf("Data: %v\n", event.Data) if event.Type == firego.EventTypeError { log.Print("Error occurred, loop ending") } } }
Output: