firego.v1: gopkg.in/zabawaba99/firego.v1 Index | Examples | Files | Directories

package firego

import "gopkg.in/zabawaba99/firego.v1"

Package firego is a REST client for Firebase (https://firebase.com).

Index

Examples

Package Files

event_callback.go firebase.go query.go snapshot.go transaction.go watch.go

Constants

const (
    // EventTypePut is the event type sent when new data is inserted to the
    // Firebase instance.
    EventTypePut = "put"
    // EventTypePatch is the event type sent when data at the Firebase instance is
    // updated.
    EventTypePatch = "patch"
    // EventTypeError is the event type sent when an unknown error is encountered.
    EventTypeError = "event_error"
    // EventTypeAuthRevoked is the event type sent when the supplied auth parameter
    // is no longer valid.
    EventTypeAuthRevoked = "auth_revoked"
)

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.

type ChildEventFunc Uses

type ChildEventFunc func(snapshot DataSnapshot, previousChildKey string)

ChildEventFunc is the type of function that is called for every new child added under a firebase reference. The snapshot argument contains the data that was added. The previousChildKey argument contains the key of the previous child that this function was called for.

type DataSnapshot Uses

type DataSnapshot struct {
    // Key retrieves the key for the source location of this snapshot
    Key string

    // Value retrieves the data contained in this snapshot.
    Value interface{}
}

DataSnapshot instances contains data from a Firebase reference.

func (*DataSnapshot) Child Uses

func (d *DataSnapshot) Child(name string) (DataSnapshot, bool)

Child gets a DataSnapshot for the location at the specified relative path. The relative path can either be a simple child key (e.g. 'fred') or a deeper slash-separated path (e.g. 'fred/name/first').

type ErrTimeout Uses

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 Uses

type Event struct {
    // Type of event that was received
    Type string
    // Path to the data that changed
    Path string
    // Data that changed
    Data interface{}
    // contains filtered or unexported fields
}

Event represents a notification received when watching a firebase reference.

func (Event) Value Uses

func (e Event) Value(v interface{}) error

Value converts the raw payload of the event into the given interface.

type Firebase Uses

type Firebase struct {
    // contains filtered or unexported fields
}

Firebase represents a location in the cloud.

func New Uses

func New(url string, client *http.Client) *Firebase

New creates a new Firebase reference, if client is nil, http.DefaultClient is used.

func (*Firebase) Auth Uses

func (fb *Firebase) Auth(token string)

Auth sets the custom Firebase token used to authenticate to Firebase.

Code:

fb := firego.New("https://someapp.firebaseio.com", nil)
fb.Auth("my-token")

func (*Firebase) Child Uses

func (fb *Firebase) Child(child string) *Firebase

Child creates a new Firebase reference for the requested child with the same configuration as the parent.

Code:

fb := firego.New("https://someapp.firebaseio.com", nil)
childFB := fb.Child("some/child/path")

log.Printf("My new ref %s\n", childFB)

func (*Firebase) ChildAdded Uses

func (fb *Firebase) ChildAdded(fn ChildEventFunc) error

ChildAdded listens on the firebase instance and executes the callback for every child that is added.

You cannot set the same function twice on a Firebase reference, if you do the first function will be overridden and you will not be able to close the connection.

func (*Firebase) ChildChanged Uses

func (fb *Firebase) ChildChanged(fn ChildEventFunc) error

ChildChanged listens on the firebase instance and executes the callback for every child that is changed.

You cannot set the same function twice on a Firebase reference, if you do the first function will be overridden and you will not be able to close the connection.

func (*Firebase) ChildRemoved Uses

func (fb *Firebase) ChildRemoved(fn ChildEventFunc) error

ChildRemoved listens on the firebase instance and executes the callback for every child that is deleted.

You cannot set the same function twice on a Firebase reference, if you do the first function will be overridden and you will not be able to close the connection.

func (*Firebase) EndAt Uses

func (fb *Firebase) EndAt(value string) *Firebase

EndAt creates a new Firebase reference with the requested EndAt configuration. The value that is passed in is automatically escaped if it is a string value. Numeric strings are automatically converted to numbers.

EndAt(7)        // -> endAt=7
EndAt("7")      // -> endAt=7
EndAt("foo")    // -> endAt="foo"
EndAt(`"foo"`)  // -> endAt="foo"

Reference https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-filtering

Code:

fb := firego.New("https://someapp.firebaseio.com", nil)
// Set value
fb = fb.EndAt("a")
// Remove query parameter
fb = fb.EndAt("")

func (*Firebase) EndAtValue Uses

func (fb *Firebase) EndAtValue(value interface{}) *Firebase

EndAtValue creates a new Firebase reference with the requested EndAt configuration. The value that is passed in is automatically escaped if it is a string value. Numeric strings are preserved as strings.

EndAtValue(7)        // -> endAt=7
EndAtValue("7")      // -> endAt="7"
EndAtValue("foo")    // -> endAt="foo"
EndAtValue(`"foo"`)  // -> endAt="foo"

Reference https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-filtering

func (*Firebase) EqualTo Uses

func (fb *Firebase) EqualTo(value string) *Firebase

EqualTo sends the query string equalTo so that one can find nodes with exactly matching values. The value that is passed in is automatically escaped if it is a string value. Numeric strings are automatically converted to numbers.

EqualTo(7)        // -> equalTo=7
EqualTo("7")      // -> equalTo=7
EqualTo("foo")    // -> equalTo="foo"
EqualTo(`"foo"`)  // -> equalTo="foo"

Reference https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-filtering

func (*Firebase) EqualToValue Uses

func (fb *Firebase) EqualToValue(value interface{}) *Firebase

EqualToValue sends the query string equalTo so that one can find nodes with exactly matching values. The value that is passed in is automatically escaped if it is a string value. Numeric strings are preserved as strings.

EqualToValue(7)        // -> equalTo=7
EqualToValue("7")      // -> equalTo="7"
EqualToValue("foo")    // -> equalTo="foo"
EqualToValue(`"foo"`)  // -> equalTo="foo"

Reference https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-filtering

func (*Firebase) IncludePriority Uses

func (fb *Firebase) IncludePriority(v bool)

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

Code:

fb := firego.New("https://someapp.firebaseio.com", nil)
// Set value
fb.IncludePriority(true)
// Remove query parameter
fb.IncludePriority(false)

func (*Firebase) LimitToFirst Uses

func (fb *Firebase) LimitToFirst(value int64) *Firebase

LimitToFirst creates a new Firebase reference with the requested limitToFirst configuration.

Reference https://firebase.google.com/docs/database/rest/retrieve-data#limit-queries

Code:

fb := firego.New("https://someapp.firebaseio.com", nil)
// Set value
fb = fb.LimitToFirst(5)
// Remove query parameter
fb = fb.LimitToFirst(-1)

func (*Firebase) LimitToLast Uses

func (fb *Firebase) LimitToLast(value int64) *Firebase

LimitToLast creates a new Firebase reference with the requested limitToLast configuration.

Reference https://firebase.google.com/docs/database/rest/retrieve-data#limit-queries

Code:

fb := firego.New("https://someapp.firebaseio.com", nil)
// Set value
fb = fb.LimitToLast(8)
// Remove query parameter
fb = fb.LimitToLast(-1)

func (*Firebase) OrderBy Uses

func (fb *Firebase) OrderBy(value string) *Firebase

OrderBy creates a new Firebase reference with the requested OrderBy configuration. The value that is passed in is automatically escaped if it is a string value.

OrderBy("foo")   // -> orderBy="foo"
OrderBy(`"foo"`) // -> orderBy="foo"
OrderBy("$key")  // -> orderBy="$key"

Reference https://firebase.google.com/docs/database/rest/retrieve-data#orderby

Code:

fb := firego.New("https://someapp.firebaseio.com", nil)
// Set value
fb = fb.OrderBy("a")
// Remove query parameter
fb = fb.OrderBy("")

func (*Firebase) Push Uses

func (fb *Firebase) Push(v interface{}) (*Firebase, error)

Push creates a reference to an auto-generated child location.

Code:

fb := firego.New("https://someapp.firebaseio.com", nil)
newRef, err := fb.Push("my-value")
if err != nil {
    log.Fatal(err)
}

log.Printf("My new ref %s\n", newRef)

func (*Firebase) Ref Uses

func (fb *Firebase) Ref(path string) (*Firebase, error)

Ref returns a copy of an existing Firebase reference with a new path.

func (*Firebase) Remove Uses

func (fb *Firebase) Remove() error

Remove the Firebase reference from the cloud.

Code:

fb := firego.New("https://someapp.firebaseio.com/some/value", nil)
if err := fb.Remove(); err != nil {
    log.Fatal(err)
}

func (*Firebase) RemoveEventFunc Uses

func (fb *Firebase) RemoveEventFunc(fn ChildEventFunc)

RemoveEventFunc removes the given function from the firebase reference.

func (*Firebase) Set Uses

func (fb *Firebase) Set(v interface{}) error

Set the value of the Firebase reference.

Code:

fb := firego.New("https://someapp.firebaseio.com", nil)

v := map[string]interface{}{
    "foo": "bar",
    "bar": 1,
    "bez": []string{"hello", "world"},
}
if err := fb.Set(v); err != nil {
    log.Fatal(err)
}

func (*Firebase) SetURL Uses

func (fb *Firebase) SetURL(url string)

SetURL changes the url for a firebase reference.

func (*Firebase) Shallow Uses

func (fb *Firebase) Shallow(v bool)

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://firebase.google.com/docs/database/rest/retrieve-data#shallow

Code:

fb := firego.New("https://someapp.firebaseio.com", nil)
// Set value
fb.Shallow(true)
// Remove query parameter
fb.Shallow(false)

func (*Firebase) StartAt Uses

func (fb *Firebase) StartAt(value string) *Firebase

StartAt creates a new Firebase reference with the requested StartAt configuration. The value that is passed in is automatically escaped if it is a string value. Numeric strings are automatically converted to numbers.

StartAt(7)        // -> startAt=7
StartAt("7")      // -> startAt=7
StartAt("foo")    // -> startAt="foo"
StartAt(`"foo"`)  // -> startAt="foo"

Reference https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-filtering

Code:

fb := firego.New("https://someapp.firebaseio.com", nil)
// Set value
fb = fb.StartAt("a")
// Remove query parameter
fb = fb.StartAt("")

func (*Firebase) StartAtValue Uses

func (fb *Firebase) StartAtValue(value interface{}) *Firebase

StartAtValue creates a new Firebase reference with the requested StartAt configuration. The value that is passed in is automatically escaped if it is a string value. Numeric strings are preserved as strings.

StartAtValue(7)        // -> startAt=7
StartAtValue("7")      // -> startAt="7"
StartAtValue("foo")    // -> startAt="foo"
StartAtValue(`"foo"`)  // -> startAt="foo"

Reference https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-filtering

func (*Firebase) StopWatching Uses

func (fb *Firebase) StopWatching()

StopWatching stops tears down all connections that are watching.

Code:

fb := firego.New("https://someapp.firebaseio.com/some/value", nil)
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()

func (*Firebase) String Uses

func (fb *Firebase) String() string

String returns the string representation of the Firebase reference.

func (*Firebase) Transaction Uses

func (fb *Firebase) Transaction(fn TransactionFn) error

Transaction runs a transaction on the data at this location. The TransactionFn parameter will be called, possibly multiple times, with the current data at this location. It is responsible for inspecting that data and specifying either the desired new data at the location or that the transaction should be aborted.

Since the provided function may be called repeatedly for the same transaction, be extremely careful of any side effects that may be triggered by this method.

Best practices for this method are to rely only on the data that is passed in.

func (*Firebase) URL Uses

func (fb *Firebase) URL() string

URL returns firebase reference URL

func (*Firebase) Unauth Uses

func (fb *Firebase) Unauth()

Unauth removes the current token being used to authenticate to Firebase.

func (*Firebase) Update Uses

func (fb *Firebase) Update(v interface{}) error

Update the specific child with the given value.

Code:

fb := firego.New("https://someapp.firebaseio.com/some/value", nil)
if err := fb.Update("new-value"); err != nil {
    log.Fatal(err)
}

func (*Firebase) Value Uses

func (fb *Firebase) Value(v interface{}) error

Value gets the value of the Firebase reference.

Code:

fb := firego.New("https://someapp.firebaseio.com/some/value", nil)
var v interface{}
if err := fb.Value(v); err != nil {
    log.Fatal(err)
}

log.Printf("My value %v\n", v)

func (*Firebase) Watch Uses

func (fb *Firebase) Watch(notifications chan Event) error

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.

Code:

fb := firego.New("https://someapp.firebaseio.com/some/value", nil)
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")
    }
}

type TransactionFn Uses

type TransactionFn func(currentSnapshot interface{}) (result interface{}, err error)

TransactionFn is used to run a transaction on a Firebase reference. See Firebase.Transaction for more information.

Directories

PathSynopsis
firetestPackage firetest provides utilities for Firebase testing
sync

Package firego imports 16 packages (graph) and is imported by 3 packages. Updated 2019-04-23. Refresh now. Tools for package owners.