vendor: add github.com/ncw/dropbox-sdk-go-unofficial and remove github.com/stacktic/dropbox

In due course this will become github.com/dropbox/dropbox-sdk-go-unofficial
when the fate of https://github.com/dropbox/dropbox-sdk-go-unofficial/pull/14
has been decided.
This commit is contained in:
Nick Craig-Wood
2017-05-30 15:49:29 +01:00
parent 9d335eb5cb
commit 178ff62d6a
51 changed files with 36327 additions and 4288 deletions
+237
View File
@@ -0,0 +1,237 @@
# Dropbox Go SDK Generator
This directory contains the [Stone](https://github.com/dropbox/stone) code generators
used to programmatically generate the [Dropbox Go SDK](https://github.com/dropbox/dropbox-sdk-go).
## Requirements
* While not a hard requirement, this repo currently assumes `python3` in the path.
* Assumes you have already installed [Stone](https://github.com/dropbox/stone)
* Requires [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports) to fix up imports in the auto-generated code
## Basic Setup
. Clone this repo
. Run `git submodule init` followed by `git submodule update`
. Run `./generate-sdk.sh` to generate code under `../dropbox`
## Generated Code
### Basic Types
Here is how Stone [basic types](https://github.com/dropbox/stone/blob/master/doc/lang_ref.rst#basic-types) map to Go types:
Stone Type | Go Type
---------- | -------
Int32/Int64/UInt32/UInt64 | int32/int64/uint32/uint64
Float32/Float64 | float32/float64
Boolean | bool
String | string
Timestamp | time.Time
Void | struct{}
### Structs
Stone [structs](https://github.com/dropbox/stone/blob/master/doc/lang_ref.rst#struct) are represented as Go [structs](https://gobyexample.com/structs) in a relatively straight-forward manner. Each struct member is exported and also gets assigned the correct json tag. The latter is used for serializing requests and deserializing responses. Non-primitive types are represented as pointers to the corresponding type.
```
struct Account
"The amount of detail revealed about an account depends on the user
being queried and the user making the query."
account_id AccountId
"The user's unique Dropbox ID."
name Name
"Details of a user's name."
```
```go
// The amount of detail revealed about an account depends on the user being
// queried and the user making the query.
type Account struct {
// The user's unique Dropbox ID.
AccountId string `json:"account_id"`
// Details of a user's name.
Name *Name `json:"name"`
}
```
#### Inheritance
Stone supports [struct inheritance](https://github.com/dropbox/stone/blob/master/doc/lang_ref.rst#inheritance). In Go, we support this via [embedding](https://golang.org/doc/effective_go.html#embedding)
```
struct BasicAccount extends Account
"Basic information about any account."
is_teammate Boolean
"Whether this user is a teammate of the current user. If this account
is the current user's account, then this will be :val:`true`."
```
```go
// Basic information about any account.
type BasicAccount struct {
Account
// Whether this user is a teammate of the current user. If this account is
// the current user's account, then this will be `True`.
IsTeammate bool `json:"is_teammate"`
```
### Unions
Stone https://github.com/dropbox/stone/blob/master/doc/lang_ref.rst#union[unions] are bit more complex as Go doesn't have native support for union types (tagged or otherwise). We declare a union as a Go struct with all the possible fields as pointer types, and then use the tag value to populate the correct field during deserialization. This necessitates the use of an intermediate wrapper struct for the deserialization to work correctly, see below for a concrete example.
```
union SpaceAllocation
"Space is allocated differently based on the type of account."
individual IndividualSpaceAllocation
"The user's space allocation applies only to their individual account."
team TeamSpaceAllocation
"The user shares space with other members of their team."
```
```go
// Space is allocated differently based on the type of account.
type SpaceAllocation struct {
dropbox.Tagged
// The user's space allocation applies only to their individual account.
Individual *IndividualSpaceAllocation `json:"individual,omitempty"`
// The user shares space with other members of their team.
Team *TeamSpaceAllocation `json:"team,omitempty"`
}
// Valid tag values for `SpaceAllocation`
const (
SpaceAllocation_Individual = "individual"
SpaceAllocation_Team = "team"
SpaceAllocation_Other = "other"
)
func (u *SpaceAllocation) UnmarshalJSON(body []byte) error {
type wrap struct {
dropbox.Tagged
// The user's space allocation applies only to their individual account.
Individual json.RawMessage `json:"individual,omitempty"`
// The user shares space with other members of their team.
Team json.RawMessage `json:"team,omitempty"`
}
var w wrap
if err := json.Unmarshal(body, &w); err != nil {
return err
}
u.Tag = w.Tag
switch u.Tag {
case "individual":
if err := json.Unmarshal(body, &u.Individual); err != nil {
return err
}
case "team":
if err := json.Unmarshal(body, &u.Team); err != nil {
return err
}
}
return nil
}
```
### Struct with Enumerated Subtypes
Per the https://github.com/dropbox/stone/blob/master/doc/lang_ref.rst#struct-polymorphism[spec], structs with enumerated subtypes are a mechanism of inheritance:
> If a struct enumerates its subtypes, an instance of any subtype will satisfy the type constraint. This is useful when wanting to discriminate amongst types that are part of the same hierarchy while simultaneously being able to avoid discriminating when accessing common fields.
To represent structs with enumerated subtypes in Go, we use a combination of Go interface types and unions as implemented above. Considering the following:
```
struct Metadata
union
file FileMetadata
folder FolderMetadata
deleted DeletedMetadata # Used by list_folder* and search
name String
path_lower String?
path_display String?
parent_shared_folder_id common.SharedFolderId?
struct FileMetadata extends Metadata
id Id
client_modified common.DropboxTimestamp
...
```
In this case, `FileMetadata`, `FolderMetadata` etc are subtypes of `Metadata`. Specifically, any subtype can be used where a parent type is expected. Thus, if `list_folder` returns a list of `Metadata`s, we should be able to parse and "upcast" to one of the enumerated subtypes.
First, we define structs to represent the base and enumerated types as we did for inherited structs above:
```go
type Metadata struct {
Name string `json:"name"`
PathLower string `json:"path_lower,omitempty"`
PathDisplay string `json:"path_display,omitempty"`
ParentSharedFolderId string `json:"parent_shared_folder_id,omitempty"`
}
type FileMetadata struct {
Metadata
Id string `json:"id"`
ClientModified time.Time `json:"client_modified"`
...
}
```
Next, we define an interface type with a dummy method and ensure that both the base and the subtypes implement the interface:
```go
type IsMetadata interface {
IsMetadata()
}
func (u *Metadata) IsMetadata() {} // Subtypes get this for free due to embedding
```
At this point, types or methods that accept/return a struct with enumerated subtypes can use the interface type instead. For instance:
```go
func GetMetadata(arg *GetMetadataArg) (res IsMetadata, err error) {...}
type ListFolderResult struct {
// The files and (direct) subfolders in the folder.
Entries []IsMetadata `json:"entries"`
...
}
```
Finally, to actually deserialize a bag of bytes into the appropriate type or subtype, we use a trick similar to how we handle unions above.
```go
type metadataUnion struct {
dropbox.Tagged
File *FileMetadata `json:"file,omitempty"`
Folder *FolderMetadata `json:"folder,omitempty"`
Deleted *DeletedMetadata `json:"deleted,omitempty"`
}
func (u *metadataUnion) UnmarshalJSON(body []byte) error {...}
func (dbx *apiImpl) GetMetadata(arg *GetMetadataArg) (res IsMetadata, err error) {
...
var tmp metadataUnion
err = json.Unmarshal(body, &tmp)
if err != nil {
return
}
switch tmp.Tag {
case "file":
res = tmp.File
case "folder":
res = tmp.Folder
case "deleted":
res = tmp.Deleted
}
}
```
+25
View File
@@ -0,0 +1,25 @@
#! /usr/bin/env bash
set -euo pipefail
if [[ $# -ne 0 ]]; then
echo "$0: Not expecting any command-line arguments, got $#." 1>&2
exit 1
fi
loc=$(realpath -e $0)
base_dir=$(dirname "$loc")
spec_dir="$base_dir/dropbox-api-spec"
gen_dir=$(dirname ${base_dir})/dropbox
stone -v -a :all go_types.stoneg.py "$gen_dir" "$spec_dir"/*.stone
stone -v -a :all go_client.stoneg.py "$gen_dir" "$spec_dir"/*.stone
# Update SDK and API spec versions
sdk_version="1.0.0-beta"
pushd ${spec_dir}
spec_version=$(git rev-parse --short HEAD)
popd
sed -i -e "s/UNKNOWN SDK VERSION/${sdk_version}/" \
-e "s/UNKNOWN SPEC VERSION/${spec_version}/" ${gen_dir}/sdk.go
goimports -l -w ${gen_dir}
@@ -0,0 +1,207 @@
import os
from stone.generator import CodeGenerator
from stone.data_type import (
is_void_type,
is_struct_type
)
from go_helpers import (
HEADER,
fmt_type,
fmt_var,
generate_doc,
)
class GoClientGenerator(CodeGenerator):
def generate(self, api):
for namespace in api.namespaces.values():
if len(namespace.routes) > 0:
self._generate_client(namespace)
def _generate_client(self, namespace):
file_name = os.path.join(self.target_folder_path, namespace.name,
'client.go')
with self.output_to_relative_path(file_name):
self.emit_raw(HEADER)
self.emit()
self.emit('package %s' % namespace.name)
self.emit()
self.emit('// Client interface describes all routes in this namespace')
with self.block('type Client interface'):
for route in namespace.routes:
generate_doc(self, route)
self.emit(self._generate_route_signature(namespace, route))
self.emit()
self.emit('type apiImpl dropbox.Context')
for route in namespace.routes:
self._generate_route(namespace, route)
self.emit('// New returns a Client implementation for this namespace')
with self.block('func New(c dropbox.Config) *apiImpl'):
self.emit('ctx := apiImpl(dropbox.NewContext(c))')
self.emit('return &ctx')
def _generate_route_signature(self, namespace, route):
req = fmt_type(route.arg_data_type, namespace)
res = fmt_type(route.result_data_type, namespace, use_interface=True)
fn = fmt_var(route.name)
style = route.attrs.get('style', 'rpc')
arg = '' if is_void_type(route.arg_data_type) else 'arg {req}'
ret = '(err error)' if is_void_type(route.result_data_type) else \
'(res {res}, err error)'
signature = '{fn}(' + arg + ') ' + ret
if style == 'download':
signature = '{fn}(' + arg + \
') (res {res}, content io.ReadCloser, err error)'
elif style == 'upload':
signature = '{fn}(' + arg + ', content io.Reader) ' + ret
if is_void_type(route.arg_data_type):
signature = '{fn}(content io.Reader) ' + ret
return signature.format(fn=fn, req=req, res=res)
def _generate_route(self, namespace, route):
out = self.emit
fn = fmt_var(route.name)
err = fmt_type(route.error_data_type, namespace)
out('//%sAPIError is an error-wrapper for the %s route' %
(fn, route.name))
with self.block('type {fn}APIError struct'.format(fn=fn)):
out('dropbox.APIError')
out('EndpointError {err} `json:"error"`'.format(err=err))
out()
signature = 'func (dbx *apiImpl) ' + self._generate_route_signature(
namespace, route)
with self.block(signature):
out('cli := dbx.Client')
out()
self._generate_request(namespace, route)
self._generate_post()
self._generate_response(route)
ok_check = 'if resp.StatusCode == http.StatusOK'
if fn == "Download":
ok_check += ' || resp.StatusCode == http.StatusPartialContent'
with self.block(ok_check):
self._generate_result(route)
self._generate_error_handling(route)
out()
def _generate_request(self, namespace, route):
out = self.emit
auth = route.attrs.get('auth', '')
host = route.attrs.get('host', 'api')
style = route.attrs.get('style', 'rpc')
body = 'nil'
if not is_void_type(route.arg_data_type):
with self.block('if dbx.Config.Verbose'):
out('log.Printf("arg: %v", arg)')
out('b, err := json.Marshal(arg)')
with self.block('if err != nil'):
out('return')
out()
if host != 'content':
body = 'bytes.NewReader(b)'
if style == 'upload':
body = 'content'
headers = {}
if not is_void_type(route.arg_data_type):
if host == 'content':
headers["Dropbox-API-Arg"] = "string(b)"
else:
headers["Content-Type"] = '"application/json"'
if style == 'upload':
headers["Content-Type"] = '"application/octet-stream"'
out('headers := map[string]string{')
for k, v in sorted(headers.items()):
out('\t"{}": {},'.format(k, v))
out('}')
if fmt_var(route.name) == "Download":
out('for k, v := range arg.ExtraHeaders { headers[k] = v }')
if auth != 'noauth' and auth != 'team':
with self.block('if dbx.Config.AsMemberID != ""'):
out('headers["Dropbox-API-Select-User"] = dbx.Config.AsMemberID')
out()
authed = 'false' if auth == 'noauth' else 'true'
out('req, err := (*dropbox.Context)(dbx).NewRequest("{}", "{}", {}, "{}", "{}", headers, {})'.format(
host, style, authed, namespace.name, route.name, body))
with self.block('if err != nil'):
out('return')
with self.block('if dbx.Config.Verbose'):
out('log.Printf("req: %v", req)')
out()
def _generate_post(self):
out = self.emit
out('resp, err := cli.Do(req)')
with self.block('if dbx.Config.Verbose'):
out('log.Printf("resp: %v", resp)')
with self.block('if err != nil'):
out('return')
out()
def _generate_response(self, route):
out = self.emit
style = route.attrs.get('style', 'rpc')
if style == 'download':
out('body := []byte(resp.Header.Get("Dropbox-API-Result"))')
out('content = resp.Body')
else:
out('defer resp.Body.Close()')
with self.block('body, err := ioutil.ReadAll(resp.Body);'
'if err != nil'):
out('return')
out()
with self.block('if dbx.Config.Verbose'):
out('log.Printf("body: %s", body)')
def _generate_error_handling(self, route):
out = self.emit
with self.block('if resp.StatusCode == http.StatusConflict'):
out('var apiError %sAPIError' % fmt_var(route.name))
with self.block('err = json.Unmarshal(body, &apiError);'
'if err != nil'):
out('return')
out('err = apiError')
out('return')
out('var apiError dropbox.APIError')
with self.block('if resp.StatusCode == http.StatusBadRequest'):
out('apiError.ErrorSummary = string(body)')
out('err = apiError')
out('return')
with self.block('err = json.Unmarshal(body, &apiError);'
'if err != nil'):
out('return')
out('err = apiError')
out('return')
def _generate_result(self, route):
out = self.emit
if is_struct_type(route.result_data_type) and \
route.result_data_type.has_enumerated_subtypes():
out('var tmp %sUnion' % fmt_var(route.result_data_type.name, export=False))
with self.block('err = json.Unmarshal(body, &tmp);'
'if err != nil'):
out('return')
with self.block('switch tmp.Tag'):
for t in route.result_data_type.get_enumerated_subtypes():
with self.block('case "%s":' % t.name, delim=(None, None)):
self.emit('res = tmp.%s' % fmt_var(t.name))
elif not is_void_type(route.result_data_type):
with self.block('err = json.Unmarshal(body, &res);'
'if err != nil'):
out('return')
out()
out('return')
+134
View File
@@ -0,0 +1,134 @@
from stone.api import ApiNamespace
from stone.data_type import (
Boolean,
Float32,
Float64,
Int32,
Int64,
String,
Timestamp,
UInt32,
UInt64,
unwrap_nullable,
is_composite_type,
is_list_type,
is_struct_type,
Void,
)
from stone.target import helpers
HEADER = """\
// Copyright (c) Dropbox, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
"""
_reserved_keywords = {
'break', 'default', 'func', 'interface', 'select',
'case', 'defer', 'go', 'map', 'struct',
'chan', 'else', 'goto', 'package', 'switch',
'const', 'fallthrough', 'if', 'range', 'type',
'continue', 'for', 'import', 'return', 'var',
}
_type_table = {
UInt64: 'uint64',
Int64: 'int64',
UInt32: 'uint32',
Int32: 'int32',
Float64: 'float64',
Float32: 'float32',
Boolean: 'bool',
String: 'string',
Timestamp: 'time.Time',
Void: 'struct{}',
}
def _rename_if_reserved(s):
if s in _reserved_keywords:
return s + '_'
else:
return s
def fmt_type(data_type, namespace=None, use_interface=False):
data_type, nullable = unwrap_nullable(data_type)
if is_list_type(data_type):
return '[]%s' % fmt_type(data_type.data_type, namespace, use_interface)
type_name = data_type.name
if use_interface and _needs_base_type(data_type):
type_name = 'Is' + type_name
if is_composite_type(data_type) and namespace is not None and \
namespace.name != data_type.namespace.name:
type_name = data_type.namespace.name + '.' + type_name
if use_interface and _needs_base_type(data_type):
return _type_table.get(data_type.__class__, type_name)
else:
return _type_table.get(data_type.__class__, '*' + type_name)
def fmt_var(name, export=True, check_reserved=False):
s = helpers.fmt_pascal(name) if export else helpers.fmt_camel(name)
return _rename_if_reserved(s) if check_reserved else s
def _doc_handler(tag, val):
if tag == 'type':
return '`{}`'.format(val)
elif tag == 'route':
return '`{}`'.format(helpers.fmt_camel(val))
elif tag == 'link':
anchor, link = val.rsplit(' ', 1)
return '`{}` <{}>'.format(anchor, link)
elif tag == 'val':
if val == 'null':
return 'nil'
else:
return val
elif tag == 'field':
return '`{}`'.format(val)
else:
raise RuntimeError('Unknown doc ref tag %r' % tag)
def generate_doc(code_generator, t):
doc = t.doc
if doc is None:
doc = 'has no documentation (yet)'
doc = code_generator.process_doc(doc, _doc_handler)
d = '%s : %s' % (fmt_var(t.name), doc)
if isinstance(t, ApiNamespace):
d = 'Package %s : %s' % (t.name, doc)
code_generator.emit_wrapped_text(d, prefix='// ')
def _needs_base_type(data_type):
if is_struct_type(data_type) and data_type.has_enumerated_subtypes():
return True
if is_list_type(data_type):
return _needs_base_type(data_type.data_type)
return False
def needs_base_type(struct):
for field in struct.fields:
if _needs_base_type(field.data_type):
return True
return False
@@ -0,0 +1,75 @@
// Copyright (c) Dropbox, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package files
import "encoding/json"
type listFolderResult struct {
Entries []metadataUnion `json:"entries"`
Cursor string `json:"cursor"`
HasMore bool `json:"has_more"`
}
// UnmarshalJSON deserializes into a ListFolderResult instance
func (r *ListFolderResult) UnmarshalJSON(b []byte) error {
var l listFolderResult
if err := json.Unmarshal(b, &l); err != nil {
return err
}
r.Cursor = l.Cursor
r.HasMore = l.HasMore
r.Entries = make([]IsMetadata, len(l.Entries))
for i, e := range l.Entries {
switch e.Tag {
case "file":
r.Entries[i] = e.File
case "folder":
r.Entries[i] = e.Folder
case "deleted":
r.Entries[i] = e.Deleted
}
}
return nil
}
type searchMatch struct {
MatchType *SearchMatchType `json:"match_type"`
Metadata metadataUnion `json:"metadata"`
}
// UnmarshalJSON deserializes into a SearchMatch instance
func (s *SearchMatch) UnmarshalJSON(b []byte) error {
var m searchMatch
if err := json.Unmarshal(b, &m); err != nil {
return err
}
s.MatchType = m.MatchType
e := m.Metadata
switch e.Tag {
case "file":
s.Metadata = e.File
case "folder":
s.Metadata = e.Folder
case "deleted":
s.Metadata = e.Deleted
}
return nil
}
@@ -0,0 +1,170 @@
// Copyright (c) Dropbox, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package dropbox
import (
"fmt"
"io"
"net/http"
"golang.org/x/oauth2"
)
const (
apiVersion = 2
defaultDomain = ".dropboxapi.com"
hostAPI = "api"
hostContent = "content"
hostNotify = "notify"
sdkVersion = "UNKNOWN SDK VERSION"
specVersion = "UNKNOWN SPEC VERSION"
)
// Version returns the current SDK version and API Spec version
func Version() (string, string) {
return sdkVersion, specVersion
}
// Config contains parameters for configuring the SDK.
type Config struct {
// OAuth2 access token
Token string
// Enable verbose logging in SDK
Verbose bool
// Used with APIs that support operations as another user
AsMemberID string
// No need to set -- for testing only
Domain string
// No need to set -- for testing only
Client *http.Client
// No need to set -- for testing only
HeaderGenerator func(hostType string, style string, namespace string, route string) map[string]string
// No need to set -- for testing only
URLGenerator func(hostType string, style string, namespace string, route string) string
}
// Context is the base client context used to implement per-namespace clients.
type Context struct {
Config Config
Client *http.Client
HeaderGenerator func(hostType string, style string, namespace string, route string) map[string]string
URLGenerator func(hostType string, style string, namespace string, route string) string
}
// NewRequest returns an appropriate Request object for the given namespace/route.
func (c *Context) NewRequest(
hostType string,
style string,
authed bool,
namespace string,
route string,
headers map[string]string,
body io.Reader,
) (*http.Request, error) {
url := c.URLGenerator(hostType, style, namespace, route)
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header.Add(k, v)
}
for k, v := range c.HeaderGenerator(hostType, style, namespace, route) {
req.Header.Add(k, v)
}
if req.Header.Get("Host") != "" {
req.Host = req.Header.Get("Host")
}
if !authed {
req.Header.Del("Authorization")
}
return req, nil
}
// NewContext returns a new Context with the given Config.
func NewContext(c Config) Context {
domain := c.Domain
if domain == "" {
domain = defaultDomain
}
client := c.Client
if client == nil {
var conf = &oauth2.Config{Endpoint: OAuthEndpoint(domain)}
tok := &oauth2.Token{AccessToken: c.Token}
client = conf.Client(oauth2.NoContext, tok)
}
headerGenerator := c.HeaderGenerator
if headerGenerator == nil {
headerGenerator = func(hostType string, style string, namespace string, route string) map[string]string {
return map[string]string{}
}
}
urlGenerator := c.URLGenerator
if urlGenerator == nil {
hostMap := map[string]string{
hostAPI: hostAPI + domain,
hostContent: hostContent + domain,
hostNotify: hostNotify + domain,
}
urlGenerator = func(hostType string, style string, namespace string, route string) string {
fqHost := hostMap[hostType]
return fmt.Sprintf("https://%s/%d/%s/%s", fqHost, apiVersion, namespace, route)
}
}
return Context{c, client, headerGenerator, urlGenerator}
}
// OAuthEndpoint constructs an `oauth2.Endpoint` for the given domain
func OAuthEndpoint(domain string) oauth2.Endpoint {
if domain == "" {
domain = defaultDomain
}
authURL := fmt.Sprintf("https://meta%s/1/oauth2/authorize", domain)
tokenURL := fmt.Sprintf("https://api%s/1/oauth2/token", domain)
if domain == defaultDomain {
authURL = "https://www.dropbox.com/1/oauth2/authorize"
}
return oauth2.Endpoint{AuthURL: authURL, TokenURL: tokenURL}
}
// Tagged is used for tagged unions.
type Tagged struct {
Tag string `json:".tag"`
}
// APIError is the base type for endpoint-specific errors.
type APIError struct {
ErrorSummary string `json:"error_summary"`
}
func (e APIError) Error() string {
return e.ErrorSummary
}
func init() {
// These are not registered in the oauth library by default
oauth2.RegisterBrokenAuthHeaderProvider("https://api.dropboxapi.com")
oauth2.RegisterBrokenAuthHeaderProvider("https://api-dbdev.dev.corp.dropbox.com")
}
@@ -0,0 +1,49 @@
// Copyright (c) Dropbox, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package sharing
import "encoding/json"
type listSharedLinksResult struct {
Links []sharedLinkMetadataUnion `json:"links"`
HasMore bool `json:"has_more"`
Cursor string `json:"cursor,omitempty"`
}
// UnmarshalJSON deserializes into a ListSharedLinksResult instance
func (r *ListSharedLinksResult) UnmarshalJSON(b []byte) error {
var l listSharedLinksResult
if err := json.Unmarshal(b, &l); err != nil {
return err
}
r.Cursor = l.Cursor
r.HasMore = l.HasMore
r.Links = make([]IsSharedLinkMetadata, len(l.Links))
for i, e := range l.Links {
switch e.Tag {
case "file":
r.Links[i] = e.File
case "folder":
r.Links[i] = e.Folder
}
}
return nil
}
@@ -0,0 +1,198 @@
import os
import shutil
from stone.generator import CodeGenerator
from stone.data_type import (
is_boolean_type,
is_nullable_type,
is_primitive_type,
is_struct_type,
is_union_type,
is_void_type,
)
from go_helpers import (
HEADER,
fmt_type,
fmt_var,
generate_doc,
)
class GoTypesGenerator(CodeGenerator):
def generate(self, api):
rsrc_folder = os.path.join(os.path.dirname(__file__), 'go_rsrc')
shutil.copy(os.path.join(rsrc_folder, 'sdk.go'),
self.target_folder_path)
for namespace in api.namespaces.values():
self._generate_namespace(namespace)
if namespace.name == 'files' or namespace.name == 'sharing':
self.logger.info('Copying metadata.go to files')
shutil.copy(os.path.join(rsrc_folder, namespace.name, 'metadata.go'),
os.path.join(self.target_folder_path, namespace.name))
def _generate_namespace(self, namespace):
file_name = os.path.join(self.target_folder_path, namespace.name,
'types.go')
with self.output_to_relative_path(file_name):
self.emit_raw(HEADER)
self.emit()
generate_doc(self, namespace)
self.emit('package %s' % namespace.name)
self.emit()
for data_type in namespace.linearize_data_types():
self._generate_data_type(data_type)
def _generate_data_type(self, data_type):
generate_doc(self, data_type)
if is_struct_type(data_type):
self._generate_struct(data_type)
if data_type.has_enumerated_subtypes():
self._generate_base_type(data_type)
elif is_union_type(data_type):
self._generate_union(data_type)
else:
self.logger.info("Unhandled data type", data_type)
def _generate_base_type(self, base):
t = fmt_type(base).lstrip('*')
self.emit('// Is{0} is the interface type for {0} and its subtypes'.format(t))
with self.block('type Is%s interface' % t):
self.emit('Is%s()' % t)
self.emit()
self.emit('// Is{0} implements the Is{0} interface'.format(t))
self.emit("func (u *{0}) Is{0}() {{}}".format(t))
self.emit()
self._generate_union_helper(base)
self.emit("// Is{0}FromJSON converts JSON to a concrete Is{0} instance".format(t))
with self.block("func Is{0}FromJSON(data []byte) (Is{0}, error)".format(t)):
name = fmt_var(t, export=False) + 'Union'
self.emit("var t {0}".format(name))
with self.block("if err := json.Unmarshal(data, &t); err != nil"):
self.emit("return nil, err")
with self.block("switch t.Tag"):
fields = base.get_enumerated_subtypes()
for field in fields:
with self.block('case "%s":' % field.name, delim=(None, None)):
self.emit("return t.{0}, nil".format(fmt_var(field.name)))
# FIX THIS
self.emit("return nil, nil")
def _generate_struct(self, struct):
with self.block('type %s struct' % struct.name):
if struct.parent_type:
self.emit(fmt_type(struct.parent_type, struct.namespace).lstrip('*'))
for field in struct.fields:
self._generate_field(field, namespace=struct.namespace)
if struct.name in ('DownloadArg',):
self.emit('// ExtraHeaders can be used to pass Range, If-None-Match headers')
self.emit('ExtraHeaders map[string]string `json:"-"`')
self._generate_struct_builder(struct)
def _generate_struct_builder(self, struct):
fields = ["%s %s" % (fmt_var(field.name),
fmt_type(field.data_type, struct.namespace,
use_interface=True))
for field in struct.all_required_fields]
self.emit('// New{0} returns a new {0} instance'.format(struct.name))
signature = "func New{0}({1}) *{0}".format(struct.name, ', '.join(fields))
with self.block(signature):
self.emit('s := new({0})'.format(struct.name))
for field in struct.all_required_fields:
field_name = fmt_var(field.name)
self.emit("s.{0} = {0}".format(field_name))
for field in struct.all_optional_fields:
if field.has_default:
if is_primitive_type(field.data_type):
default = field.default
if is_boolean_type(field.data_type):
default = str(default).lower()
self.emit('s.{0} = {1}'.format(fmt_var(field.name), default))
elif is_union_type(field.data_type):
self.emit('s.%s = &%s{Tagged:dropbox.Tagged{"%s"}}' %
(fmt_var(field.name),
fmt_type(field.data_type, struct.namespace).lstrip('*'),
field.default.tag_name))
self.emit('return s')
self.emit()
def _generate_field(self, field, union_field=False, namespace=None, raw=False):
generate_doc(self, field)
field_name = fmt_var(field.name)
type_name = fmt_type(field.data_type, namespace, use_interface=True)
json_tag = '`json:"%s"`' % field.name
if is_nullable_type(field.data_type) or union_field:
json_tag = '`json:"%s,omitempty"`' % field.name
if raw:
self.emit('%s json.RawMessage %s' % (field_name, json_tag))
else:
self.emit('%s %s %s' % (field_name, type_name, json_tag))
def _generate_union(self, union):
self._generate_union_helper(union)
def _generate_union_helper(self, u):
name = u.name
namespace = u.namespace
fields = u.fields
if is_struct_type(u) and u.has_enumerated_subtypes():
name = fmt_var(name, export=False) + 'Union'
fields = u.get_enumerated_subtypes()
with self.block('type %s struct' % name):
self.emit('dropbox.Tagged')
for field in fields:
if is_void_type(field.data_type):
continue
self._generate_field(field, union_field=True,
namespace=namespace)
self.emit()
self.emit('// Valid tag values for %s' % fmt_var(u.name))
with self.block('const', delim=('(', ')')):
for field in fields:
self.emit('%s%s = "%s"' % (fmt_var(u.name), fmt_var(field.name), field.name))
self.emit()
num_void_fields = sum([is_void_type(f.data_type) for f in fields])
# Simple structure, no need in UnmarshalJSON
if len(fields) == num_void_fields:
return
self.emit('// UnmarshalJSON deserializes into a %s instance' % name)
with self.block('func (u *%s) UnmarshalJSON(body []byte) error' % name):
with self.block('type wrap struct'):
self.emit('dropbox.Tagged')
for field in fields:
if is_void_type(field.data_type) or \
is_primitive_type(field.data_type):
continue
self._generate_field(field, union_field=True,
namespace=namespace, raw=True)
self.emit('var w wrap')
self.emit('var err error')
with self.block('if err = json.Unmarshal(body, &w); err != nil'):
self.emit('return err')
self.emit('u.Tag = w.Tag')
with self.block('switch u.Tag'):
for field in fields:
if is_void_type(field.data_type):
continue
field_name = fmt_var(field.name)
with self.block('case "%s":' % field.name, delim=(None, None)):
if is_union_type(field.data_type):
self.emit('err = json.Unmarshal(w.{0}, &u.{0})'
.format(field_name))
elif is_struct_type(field.data_type) and \
field.data_type.has_enumerated_subtypes():
self.emit("u.{0}, err = Is{1}FromJSON(body)"
.format(field_name, field.data_type.name))
else:
self.emit('err = json.Unmarshal(body, &u.{0})'
.format(field_name))
with self.block("if err != nil"):
self.emit("return err")
self.emit('return nil')
self.emit()