|
| 1 | +// Copyright 2018 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package db contains functions for accessing the Firebase Realtime Database. |
| 16 | +package db |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "runtime" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "firebase.google.com/go/internal" |
| 25 | + |
| 26 | + "net/url" |
| 27 | + |
| 28 | + "golang.org/x/net/context" |
| 29 | + "google.golang.org/api/option" |
| 30 | + "google.golang.org/api/transport" |
| 31 | +) |
| 32 | + |
| 33 | +const userAgentFormat = "Firebase/HTTP/%s/%s/AdminGo" |
| 34 | +const invalidChars = "[].#$" |
| 35 | +const authVarOverride = "auth_variable_override" |
| 36 | + |
| 37 | +// Client is the interface for the Firebase Realtime Database service. |
| 38 | +type Client struct { |
| 39 | + hc *internal.HTTPClient |
| 40 | + url string |
| 41 | + authOverride string |
| 42 | +} |
| 43 | + |
| 44 | +// NewClient creates a new instance of the Firebase Database Client. |
| 45 | +// |
| 46 | +// This function can only be invoked from within the SDK. Client applications should access the |
| 47 | +// Database service through firebase.App. |
| 48 | +func NewClient(ctx context.Context, c *internal.DatabaseConfig) (*Client, error) { |
| 49 | + opts := append([]option.ClientOption{}, c.Opts...) |
| 50 | + ua := fmt.Sprintf(userAgentFormat, c.Version, runtime.Version()) |
| 51 | + opts = append(opts, option.WithUserAgent(ua)) |
| 52 | + hc, _, err := transport.NewHTTPClient(ctx, opts...) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + p, err := url.ParseRequestURI(c.URL) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } else if p.Scheme != "https" { |
| 61 | + return nil, fmt.Errorf("invalid database URL: %q; want scheme: %q", c.URL, "https") |
| 62 | + } else if !strings.HasSuffix(p.Host, ".firebaseio.com") { |
| 63 | + return nil, fmt.Errorf("invalid database URL: %q; want host: %q", c.URL, "firebaseio.com") |
| 64 | + } |
| 65 | + |
| 66 | + var ao []byte |
| 67 | + if c.AuthOverride == nil || len(c.AuthOverride) > 0 { |
| 68 | + ao, err = json.Marshal(c.AuthOverride) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + ep := func(b []byte) string { |
| 75 | + var p struct { |
| 76 | + Error string `json:"error"` |
| 77 | + } |
| 78 | + if err := json.Unmarshal(b, &p); err != nil { |
| 79 | + return "" |
| 80 | + } |
| 81 | + return p.Error |
| 82 | + } |
| 83 | + return &Client{ |
| 84 | + hc: &internal.HTTPClient{Client: hc, ErrParser: ep}, |
| 85 | + url: fmt.Sprintf("https://%s", p.Host), |
| 86 | + authOverride: string(ao), |
| 87 | + }, nil |
| 88 | +} |
| 89 | + |
| 90 | +// NewRef returns a new database reference representing the node at the specified path. |
| 91 | +func (c *Client) NewRef(path string) *Ref { |
| 92 | + segs := parsePath(path) |
| 93 | + key := "" |
| 94 | + if len(segs) > 0 { |
| 95 | + key = segs[len(segs)-1] |
| 96 | + } |
| 97 | + |
| 98 | + return &Ref{ |
| 99 | + Key: key, |
| 100 | + Path: "/" + strings.Join(segs, "/"), |
| 101 | + client: c, |
| 102 | + segs: segs, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func (c *Client) send( |
| 107 | + ctx context.Context, |
| 108 | + method, path string, |
| 109 | + body internal.HTTPEntity, |
| 110 | + opts ...internal.HTTPOption) (*internal.Response, error) { |
| 111 | + |
| 112 | + if strings.ContainsAny(path, invalidChars) { |
| 113 | + return nil, fmt.Errorf("invalid path with illegal characters: %q", path) |
| 114 | + } |
| 115 | + if c.authOverride != "" { |
| 116 | + opts = append(opts, internal.WithQueryParam(authVarOverride, c.authOverride)) |
| 117 | + } |
| 118 | + return c.hc.Do(ctx, &internal.Request{ |
| 119 | + Method: method, |
| 120 | + URL: fmt.Sprintf("%s%s.json", c.url, path), |
| 121 | + Body: body, |
| 122 | + Opts: opts, |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +func parsePath(path string) []string { |
| 127 | + var segs []string |
| 128 | + for _, s := range strings.Split(path, "/") { |
| 129 | + if s != "" { |
| 130 | + segs = append(segs, s) |
| 131 | + } |
| 132 | + } |
| 133 | + return segs |
| 134 | +} |
0 commit comments