firego

package module
v0.0.0-...-2e2d788 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2016 License: MIT Imports: 14 Imported by: 0

README

Firego


Build Status Coverage Status

A Firebase client written in Go

Installation

go get -u github.com/zabawaba99/firego

Usage

Import firego

import "github.com/zabawaba99/firego"

Create a new firego reference

f := firego.New("https://my-firebase-app.firebaseIO.com", nil)

with existing http client

f := firego.New("https://my-firebase-app.firebaseIO.com", client)
Request Timeouts

By default, the Firebase reference will timeout after 30 seconds of trying to reach a Firebase server. You can configure this value by setting the global timeout duration

firego.TimeoutDuration = time.Minute
Auth Tokens
f.Auth("some-token-that-was-created-for-me")
f.Unauth()

Visit Fireauth if you'd like to generate your own auth tokens

Get Value
var v map[string]interface{}
if err := f.Value(&v); err != nil {
  log.Fatal(err)
}
fmt.Printf("%s\n", v)
Querying

Take a look at Firebase's query parameters for more information on what each function does.

var v map[string]interface{}
if err := f.StartAt("a").EndAt("c").LimitToFirst(8).OrderBy("field").Value(&v); err != nil {
	log.Fatal(err)
}
fmt.Printf("%s\n", v)
Set Value
v := map[string]string{"foo":"bar"}
if err := f.Set(v); err != nil {
  log.Fatal(err)
}
Push Value
v := "bar"
pushedFirego, err := f.Push(v)
if err != nil {
	log.Fatal(err)
}

var bar string
if err := pushedFirego.Value(&bar); err != nil {
	log.Fatal(err)
}

// prints "https://my-firebase-app.firebaseIO.com/-JgvLHXszP4xS0AUN-nI: bar"
fmt.Printf("%s: %s\n", pushedFirego, bar)
Update Child
v := map[string]string{"foo":"bar"}
if err := f.Update(v); err != nil {
  log.Fatal(err)
}
Remove Value
if err := f.Remove(); err != nil {
  log.Fatal(err)
}
Watch a Node
notifications := make(chan firego.Event)
if err := f.Watch(notifications); err != nil {
	log.Fatal(err)
}

defer f.StopWatching()
for event := range notifications {
	fmt.Printf("Event %#v\n", event)
}
fmt.Printf("Notifications have stopped")

Check the GoDocs or Firebase Documentation for more details

Running Tests

In order to run the tests you need to go get -t ./... first to go-get the test dependencies.

Issues Management

Feel free to open an issue if you come across any bugs or if you'd like to request a new feature.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b new-feature)
  3. Commit your changes (git commit -am 'Some cool reflection')
  4. Push to the branch (git push origin new-feature)
  5. Create new Pull Request

Documentation

Overview

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

Index

Examples

Constants

View Source
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

View Source
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 New

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

func (fb *Firebase) Auth(token string)

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

Example
package main

import (
	"github.com/zabawaba99/firego"
)

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

func (*Firebase) Child

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

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

Example
package main

import (
	"log"

	"github.com/zabawaba99/firego"
)

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

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

func (*Firebase) EndAt

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 escape if it is a string value.

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

Reference https://www.firebase.com/docs/rest/guide/retrieving-data.html#section-rest-filtering

Example
package main

import (
	"github.com/zabawaba99/firego"
)

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

func (*Firebase) EqualTo

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

EqualTo sends the query string equalTo so that one can find a single value

Reference https://www.firebase.com/docs/rest/guide/retrieving-data.html#section-rest-filtering

func (*Firebase) IncludePriority

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

Example
package main

import (
	"github.com/zabawaba99/firego"
)

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

func (*Firebase) LimitToFirst

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

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

Reference https://www.firebase.com/docs/rest/api/#section-param-query

Example
package main

import (
	"github.com/zabawaba99/firego"
)

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

func (*Firebase) LimitToLast

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

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

Reference https://www.firebase.com/docs/rest/api/#section-param-query

Example
package main

import (
	"github.com/zabawaba99/firego"
)

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

func (*Firebase) OrderBy

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 escape if it is a string value.

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

Reference https://www.firebase.com/docs/rest/guide/retrieving-data.html#section-rest-filtering

Example
package main

import (
	"github.com/zabawaba99/firego"
)

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

func (*Firebase) Push

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

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

Example
package main

import (
	"log"

	"github.com/zabawaba99/firego"
)

func main() {
	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)
}
Output:

func (*Firebase) Remove

func (fb *Firebase) Remove() error

Remove the Firebase reference from the cloud.

Example
package main

import (
	"log"

	"github.com/zabawaba99/firego"
)

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

func (*Firebase) Set

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

Set the value of the Firebase reference.

Example
package main

import (
	"log"

	"github.com/zabawaba99/firego"
)

func main() {
	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)
	}
}
Output:

func (*Firebase) Shallow

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://www.firebase.com/docs/rest/api/#section-param-shallow

Example
package main

import (
	"github.com/zabawaba99/firego"
)

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

func (*Firebase) StartAt

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 escape if it is a string value.

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

Reference https://www.firebase.com/docs/rest/guide/retrieving-data.html#section-rest-filtering

Example
package main

import (
	"github.com/zabawaba99/firego"
)

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

func (*Firebase) StopWatching

func (fb *Firebase) StopWatching()

StopWatching stops tears down all connections that are watching.

Example
package main

import (
	"log"
	"time"

	"github.com/zabawaba99/firego"
)

func main() {
	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()
}
Output:

func (*Firebase) String

func (fb *Firebase) String() string

String returns the string representation of the Firebase reference.

func (*Firebase) Unauth

func (fb *Firebase) Unauth()

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

func (*Firebase) Update

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

Update the specific child with the given value.

Example
package main

import (
	"log"

	"github.com/zabawaba99/firego"
)

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

func (*Firebase) Value

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

Value gets the value of the Firebase reference.

Example
package main

import (
	"log"

	"github.com/zabawaba99/firego"
)

func main() {
	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)
}
Output:

func (*Firebase) Watch

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.

Example
package main

import (
	"log"

	"github.com/zabawaba99/firego"
)

func main() {
	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")
		}
	}
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL