-
Notifications
You must be signed in to change notification settings - Fork 268
Firebase Database API #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
69 commits
Select commit
Hold shift + click to select a range
ef0a863
Experimental RTDB code
hiranya911 becb82f
Added ref.Set()
hiranya911 2d4e342
Added Push(), Update(), Remove() and tests
hiranya911 6bf42cf
Adding Transaction() support
hiranya911 0a21f4e
Fixed Transaction() API
hiranya911 0fcbbce
Code cleanup
hiranya911 65870f7
Implemented Query() API
hiranya911 cad2bfe
Added GetIfChanged() and integration tests
hiranya911 40c0691
More integration tests
hiranya911 0e85fa2
Updated unit test
hiranya911 20fdbf3
More integration tests
hiranya911 ef547a6
Integration tests for queries
hiranya911 b4e127d
Auth override support and more tests
hiranya911 6af57b0
More test cases; AuthOverride support in App
hiranya911 da6fc75
Implemented AuthOverride support; Added tests
hiranya911 641dbaa
Implementing the new API
hiranya911 f82a3e0
More code cleanup
hiranya911 6ecb8de
Code clean up
hiranya911 deb4eac
Refactored the http client code
hiranya911 caeaafe
More tests
hiranya911 bf092a2
Boosted test coverage to 97%
hiranya911 8450363
Better error messages in tests; Added license headers
hiranya911 993f639
Added documentatioon and cleaned up tests
hiranya911 63a5ace
Fixing a build break
hiranya911 835adaa
Finishing up documentation
hiranya911 57263ad
More test cases
hiranya911 e557b9a
Implemented a reusable HTTP client API
hiranya911 2c30329
Merge branch 'hkj-http-client' into hkj-rtdb
hiranya911 f69b5ad
Added test cases
hiranya911 d5a5fae
Comment clean up
hiranya911 591e216
Merge branch 'hkj-http-client' into hkj-rtdb
hiranya911 73bfd8f
Using the shared http client API
hiranya911 11455d8
Simplified the usage by adding HTTPClient
hiranya911 31fe147
Merge branch 'hkj-http-client' into hkj-rtdb
hiranya911 d2f90f2
using the new client API
hiranya911 6c1d6ee
Using the old ctx import
hiranya911 6ddf0f6
Using the old context import
hiranya911 e6d8e9e
Merge branch 'hkj-http-client' into hkj-rtdb
hiranya911 6bcb489
Refactored db code
hiranya911 9c5dc02
More refactoring
hiranya911 98a2e3c
Support for arbitrary entity types in the request
hiranya911 9cc76cb
Renamed fields; Added documentation
hiranya911 f96fc12
Removing a redundant else case
hiranya911 24cdb6b
Merge branch 'hkj-http-client' into hkj-rtdb
hiranya911 be94fad
Code readability improvements
hiranya911 f08b191
Cleaned up the RTDB HTTP client code
hiranya911 d68a2e1
Merged with dev
hiranya911 e008fc1
Merged with dev
hiranya911 d0090e6
Merge branch 'dev' into hkj-rtdb
hiranya911 681a529
Added shallow reads support; Added the new txn API
hiranya911 0dac62f
Implementing GetOrdered() for queries
hiranya911 01ad198
Adding more sorting tests
hiranya911 3cd8ea5
Added Query ordering tests
hiranya911 b8b9a84
Fixing some lint errors and compilation errors
hiranya911 5fe7d33
Removing unused function
hiranya911 a65e3e4
Cleaned up unit tests for db
hiranya911 d734308
Updated query impl and tests
hiranya911 0007b6b
Added integration tests for ordered queries
hiranya911 f0f2063
Removed With*() from query functions
hiranya911 acf4fbd
Merge branch 'dev' into hkj-rtdb
hiranya911 0e095a0
Updated change log; Added more tests
hiranya911 0bbe372
Support for database url in auto init
hiranya911 f4cf85a
Support for loading auth overrides from env
hiranya911 14b3591
Removed db.AuthOverride type
hiranya911 c38f24e
Renamed ao to authOverride everywhere; Other code review nits
hiranya911 6365187
Merge branch 'dev' into hkj-rtdb
hiranya911 db9249e
Introducing the QueryNode interface to handle ordered query results (…
hiranya911 7bc0e2a
Merged with dev
hiranya911 bbcdae2
Database Sample Snippets (#102)
hiranya911 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| # Unreleased | ||
|
|
||
| - | ||
| - [added] Added the `db` package for interacting with the Firebase database. | ||
|
|
||
| # v2.5.0 | ||
|
|
||
|
|
||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright 2018 Google Inc. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package db | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "golang.org/x/net/context" | ||
| ) | ||
|
|
||
| func TestAuthOverrideGet(t *testing.T) { | ||
| mock := &mockServer{Resp: "data"} | ||
| srv := mock.Start(aoClient) | ||
| defer srv.Close() | ||
|
|
||
| ref := aoClient.NewRef("peter") | ||
| var got string | ||
| if err := ref.Get(context.Background(), &got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != "data" { | ||
| t.Errorf("Ref(AuthOverride).Get() = %q; want = %q", got, "data") | ||
| } | ||
| checkOnlyRequest(t, mock.Reqs, &testReq{ | ||
| Method: "GET", | ||
| Path: "/peter.json", | ||
| Query: map[string]string{"auth_variable_override": testAuthOverrides}, | ||
| }) | ||
| } | ||
|
|
||
| func TestAuthOverrideSet(t *testing.T) { | ||
| mock := &mockServer{} | ||
| srv := mock.Start(aoClient) | ||
| defer srv.Close() | ||
|
|
||
| ref := aoClient.NewRef("peter") | ||
| want := map[string]interface{}{"name": "Peter Parker", "age": float64(17)} | ||
| if err := ref.Set(context.Background(), want); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| checkOnlyRequest(t, mock.Reqs, &testReq{ | ||
| Method: "PUT", | ||
| Body: serialize(want), | ||
| Path: "/peter.json", | ||
| Query: map[string]string{"auth_variable_override": testAuthOverrides, "print": "silent"}, | ||
| }) | ||
| } | ||
|
|
||
| func TestAuthOverrideQuery(t *testing.T) { | ||
| mock := &mockServer{Resp: "data"} | ||
| srv := mock.Start(aoClient) | ||
| defer srv.Close() | ||
|
|
||
| ref := aoClient.NewRef("peter") | ||
| var got string | ||
| if err := ref.OrderByChild("foo").Get(context.Background(), &got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != "data" { | ||
| t.Errorf("Ref(AuthOverride).OrderByChild() = %q; want = %q", got, "data") | ||
| } | ||
| checkOnlyRequest(t, mock.Reqs, &testReq{ | ||
| Method: "GET", | ||
| Path: "/peter.json", | ||
| Query: map[string]string{ | ||
| "auth_variable_override": testAuthOverrides, | ||
| "orderBy": "\"foo\"", | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestAuthOverrideRangeQuery(t *testing.T) { | ||
| mock := &mockServer{Resp: "data"} | ||
| srv := mock.Start(aoClient) | ||
| defer srv.Close() | ||
|
|
||
| ref := aoClient.NewRef("peter") | ||
| var got string | ||
| if err := ref.OrderByChild("foo").StartAt(1).EndAt(10).Get(context.Background(), &got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != "data" { | ||
| t.Errorf("Ref(AuthOverride).OrderByChild() = %q; want = %q", got, "data") | ||
| } | ||
| checkOnlyRequest(t, mock.Reqs, &testReq{ | ||
| Method: "GET", | ||
| Path: "/peter.json", | ||
| Query: map[string]string{ | ||
| "auth_variable_override": testAuthOverrides, | ||
| "orderBy": "\"foo\"", | ||
| "startAt": "1", | ||
| "endAt": "10", | ||
| }, | ||
| }) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Copyright 2018 Google Inc. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Package db contains functions for accessing the Firebase Realtime Database. | ||
| package db | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "runtime" | ||
| "strings" | ||
|
|
||
| "firebase.google.com/go/internal" | ||
|
|
||
| "net/url" | ||
|
|
||
| "golang.org/x/net/context" | ||
| "google.golang.org/api/option" | ||
| "google.golang.org/api/transport" | ||
| ) | ||
|
|
||
| const userAgentFormat = "Firebase/HTTP/%s/%s/AdminGo" | ||
| const invalidChars = "[].#$" | ||
| const authVarOverride = "auth_variable_override" | ||
|
|
||
| // Client is the interface for the Firebase Realtime Database service. | ||
| type Client struct { | ||
| hc *internal.HTTPClient | ||
| url string | ||
| authOverride string | ||
| } | ||
|
|
||
| // NewClient creates a new instance of the Firebase Database Client. | ||
| // | ||
| // This function can only be invoked from within the SDK. Client applications should access the | ||
| // Database service through firebase.App. | ||
| func NewClient(ctx context.Context, c *internal.DatabaseConfig) (*Client, error) { | ||
| opts := append([]option.ClientOption{}, c.Opts...) | ||
| ua := fmt.Sprintf(userAgentFormat, c.Version, runtime.Version()) | ||
| opts = append(opts, option.WithUserAgent(ua)) | ||
| hc, _, err := transport.NewHTTPClient(ctx, opts...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| p, err := url.ParseRequestURI(c.URL) | ||
| if err != nil { | ||
| return nil, err | ||
| } else if p.Scheme != "https" { | ||
| return nil, fmt.Errorf("invalid database URL: %q; want scheme: %q", c.URL, "https") | ||
| } else if !strings.HasSuffix(p.Host, ".firebaseio.com") { | ||
| return nil, fmt.Errorf("invalid database URL: %q; want host: %q", c.URL, "firebaseio.com") | ||
| } | ||
|
|
||
| var ao []byte | ||
| if c.AuthOverride == nil || len(c.AuthOverride) > 0 { | ||
| ao, err = json.Marshal(c.AuthOverride) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| ep := func(b []byte) string { | ||
| var p struct { | ||
| Error string `json:"error"` | ||
| } | ||
| if err := json.Unmarshal(b, &p); err != nil { | ||
| return "" | ||
| } | ||
| return p.Error | ||
| } | ||
| return &Client{ | ||
| hc: &internal.HTTPClient{Client: hc, ErrParser: ep}, | ||
| url: fmt.Sprintf("https://%s", p.Host), | ||
| authOverride: string(ao), | ||
| }, nil | ||
| } | ||
|
|
||
| // NewRef returns a new database reference representing the node at the specified path. | ||
| func (c *Client) NewRef(path string) *Ref { | ||
| segs := parsePath(path) | ||
| key := "" | ||
| if len(segs) > 0 { | ||
| key = segs[len(segs)-1] | ||
| } | ||
|
|
||
| return &Ref{ | ||
| Key: key, | ||
| Path: "/" + strings.Join(segs, "/"), | ||
| client: c, | ||
| segs: segs, | ||
| } | ||
| } | ||
|
|
||
| func (c *Client) send( | ||
| ctx context.Context, | ||
| method, path string, | ||
| body internal.HTTPEntity, | ||
| opts ...internal.HTTPOption) (*internal.Response, error) { | ||
|
|
||
| if strings.ContainsAny(path, invalidChars) { | ||
| return nil, fmt.Errorf("invalid path with illegal characters: %q", path) | ||
| } | ||
| if c.authOverride != "" { | ||
| opts = append(opts, internal.WithQueryParam(authVarOverride, c.authOverride)) | ||
| } | ||
| return c.hc.Do(ctx, &internal.Request{ | ||
| Method: method, | ||
| URL: fmt.Sprintf("%s%s.json", c.url, path), | ||
| Body: body, | ||
| Opts: opts, | ||
| }) | ||
| } | ||
|
|
||
| func parsePath(path string) []string { | ||
| var segs []string | ||
| for _, s := range strings.Split(path, "/") { | ||
| if s != "" { | ||
| segs = append(segs, s) | ||
| } | ||
| } | ||
| return segs | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For such a small set, this is the right way to do this. But I want to bring unicode.RangeTable to your attention, as it would be the right way to do this if the set were much more complicated.
https://golang.org/pkg/unicode/#Is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack