Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## v3.1.0

* Added: Introduced `bigint` create/update APIs for legacy Databases attributes
* Added: Introduced `bigint` create/update APIs for `TablesDB` columns
* Updated: Extended key-list query filters with `key`, `resourceType`, `resourceId`, and `secret`

## v3.0.0

* [BREAKING] Renamed Webhook model fields: `security` → `tls`, `httpUser` → `authUsername`, `httpPass` → `authPassword`, `signatureKey` → `secret`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Appwrite Go SDK

![License](https://img.shields.io/github/license/appwrite/sdk-for-go.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-1.9.1-blue.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-1.9.4-blue.svg?style=flat-square)
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite)
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)
Expand Down
22 changes: 22 additions & 0 deletions appwrite/appwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/appwrite/sdk-for-go/v3/locale"
"github.com/appwrite/sdk-for-go/v3/messaging"
"github.com/appwrite/sdk-for-go/v3/project"
"github.com/appwrite/sdk-for-go/v3/proxy"
"github.com/appwrite/sdk-for-go/v3/sites"
"github.com/appwrite/sdk-for-go/v3/storage"
"github.com/appwrite/sdk-for-go/v3/tablesdb"
Expand Down Expand Up @@ -57,6 +58,9 @@ func NewMessaging(clt client.Client) *messaging.Messaging {
func NewProject(clt client.Client) *project.Project {
return project.New(clt)
}
func NewProxy(clt client.Client) *proxy.Proxy {
return proxy.New(clt)
}
func NewSites(clt client.Client) *sites.Sites {
return sites.New(clt)
}
Expand Down Expand Up @@ -177,6 +181,24 @@ func WithForwardedUserAgent(value string) client.ClientOption {
}
// Helper method to construct NewClient()
//
// Your secret dev API key
func WithDevKey(value string) client.ClientOption {
return func(clt *client.Client) error {
clt.Headers["X-Appwrite-Dev-Key"] = value
return nil
}
}
// Helper method to construct NewClient()
//
// The user cookie to authenticate with. Used by SDKs that forward an incoming Cookie header in server-side runtimes.
func WithCookie(value string) client.ClientOption {
return func(clt *client.Client) error {
clt.Headers["Cookie"] = value
return nil
}
}
// Helper method to construct NewClient()
//
// Impersonate a user by ID on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.
func WithImpersonateUserId(value string) client.ClientOption {
return func(clt *client.Client) error {
Expand Down
6 changes: 3 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ type Client struct {
// Initialize a new Appwrite client with a given timeout
func New(optionalSetters ...ClientOption) Client {
headers := map[string]string{
"X-Appwrite-Response-Format" : "1.9.1",
"user-agent" : fmt.Sprintf("AppwriteGoSDK/v3.0.0 (%s; %s)", runtime.GOOS, runtime.GOARCH),
"X-Appwrite-Response-Format" : "1.9.4",
"user-agent" : fmt.Sprintf("AppwriteGoSDK/v3.1.0 (%s; %s)", runtime.GOOS, runtime.GOARCH),
"x-sdk-name": "Go",
"x-sdk-platform": "server",
"x-sdk-language": "go",
"x-sdk-version": "v3.0.0",
"x-sdk-version": "v3.1.0",
}
httpClient, err := GetDefaultClient(defaultTimeout)
if err != nil {
Expand Down
186 changes: 186 additions & 0 deletions databases/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,192 @@ func (srv *Databases) ListAttributes(DatabaseId string, CollectionId string, opt
}
return &parsed, nil

}
type CreateBigIntAttributeOptions struct {
Min int
Max int
Default int
Array bool
enabledSetters map[string]bool
}
func (options CreateBigIntAttributeOptions) New() *CreateBigIntAttributeOptions {
options.enabledSetters = map[string]bool{
"Min": false,
"Max": false,
"Default": false,
"Array": false,
}
return &options
}
type CreateBigIntAttributeOption func(*CreateBigIntAttributeOptions)
func (srv *Databases) WithCreateBigIntAttributeMin(v int) CreateBigIntAttributeOption {
return func(o *CreateBigIntAttributeOptions) {
o.Min = v
o.enabledSetters["Min"] = true
}
}
func (srv *Databases) WithCreateBigIntAttributeMax(v int) CreateBigIntAttributeOption {
return func(o *CreateBigIntAttributeOptions) {
o.Max = v
o.enabledSetters["Max"] = true
}
}
func (srv *Databases) WithCreateBigIntAttributeDefault(v int) CreateBigIntAttributeOption {
return func(o *CreateBigIntAttributeOptions) {
o.Default = v
o.enabledSetters["Default"] = true
}
}
func (srv *Databases) WithCreateBigIntAttributeArray(v bool) CreateBigIntAttributeOption {
return func(o *CreateBigIntAttributeOptions) {
o.Array = v
o.enabledSetters["Array"] = true
}
}

// CreateBigIntAttribute create a bigint attribute. Optionally, minimum and
// maximum values can be provided.
//
// Deprecated: This API has been deprecated since 1.8.0. Please use `TablesDB.createBigIntColumn` instead.
func (srv *Databases) CreateBigIntAttribute(DatabaseId string, CollectionId string, Key string, Required bool, optionalSetters ...CreateBigIntAttributeOption)(*models.AttributeBigint, error) {
r := strings.NewReplacer("{databaseId}", DatabaseId, "{collectionId}", CollectionId)
path := r.Replace("/databases/{databaseId}/collections/{collectionId}/attributes/bigint")
options := CreateBigIntAttributeOptions{}.New()
for _, opt := range optionalSetters {
opt(options)
}
params := map[string]interface{}{}
params["databaseId"] = DatabaseId
params["collectionId"] = CollectionId
params["key"] = Key
params["required"] = Required
if options.enabledSetters["Min"] {
params["min"] = options.Min
}
if options.enabledSetters["Max"] {
params["max"] = options.Max
}
if options.enabledSetters["Default"] {
params["default"] = options.Default
}
if options.enabledSetters["Array"] {
params["array"] = options.Array
}
headers := map[string]interface{}{
"content-type": "application/json",
}

resp, err := srv.client.Call("POST", path, headers, params)
if err != nil {
return nil, err
}
if strings.HasPrefix(resp.Type, "application/json") {
bytes := []byte(resp.Result.(string))

parsed := models.AttributeBigint{}.New(bytes)

err = json.Unmarshal(bytes, parsed)
if err != nil {
return nil, err
}

return parsed, nil
}
var parsed models.AttributeBigint
parsed, ok := resp.Result.(models.AttributeBigint)
if !ok {
return nil, errors.New("unexpected response type")
}
return &parsed, nil

}
type UpdateBigIntAttributeOptions struct {
Min int
Max int
NewKey string
enabledSetters map[string]bool
}
func (options UpdateBigIntAttributeOptions) New() *UpdateBigIntAttributeOptions {
options.enabledSetters = map[string]bool{
"Min": false,
"Max": false,
"NewKey": false,
}
return &options
}
type UpdateBigIntAttributeOption func(*UpdateBigIntAttributeOptions)
func (srv *Databases) WithUpdateBigIntAttributeMin(v int) UpdateBigIntAttributeOption {
return func(o *UpdateBigIntAttributeOptions) {
o.Min = v
o.enabledSetters["Min"] = true
}
}
func (srv *Databases) WithUpdateBigIntAttributeMax(v int) UpdateBigIntAttributeOption {
return func(o *UpdateBigIntAttributeOptions) {
o.Max = v
o.enabledSetters["Max"] = true
}
}
func (srv *Databases) WithUpdateBigIntAttributeNewKey(v string) UpdateBigIntAttributeOption {
return func(o *UpdateBigIntAttributeOptions) {
o.NewKey = v
o.enabledSetters["NewKey"] = true
}
}

// UpdateBigIntAttribute update a bigint attribute. Changing the `default`
// value will not update already existing documents.
//
// Deprecated: This API has been deprecated since 1.8.0. Please use `TablesDB.updateBigIntColumn` instead.
func (srv *Databases) UpdateBigIntAttribute(DatabaseId string, CollectionId string, Key string, Required bool, Default int, optionalSetters ...UpdateBigIntAttributeOption)(*models.AttributeBigint, error) {
r := strings.NewReplacer("{databaseId}", DatabaseId, "{collectionId}", CollectionId, "{key}", Key)
path := r.Replace("/databases/{databaseId}/collections/{collectionId}/attributes/bigint/{key}")
options := UpdateBigIntAttributeOptions{}.New()
for _, opt := range optionalSetters {
opt(options)
}
params := map[string]interface{}{}
params["databaseId"] = DatabaseId
params["collectionId"] = CollectionId
params["key"] = Key
params["required"] = Required
params["default"] = Default
if options.enabledSetters["Min"] {
params["min"] = options.Min
}
if options.enabledSetters["Max"] {
params["max"] = options.Max
}
if options.enabledSetters["NewKey"] {
params["newKey"] = options.NewKey
}
headers := map[string]interface{}{
"content-type": "application/json",
}

resp, err := srv.client.Call("PATCH", path, headers, params)
if err != nil {
return nil, err
}
if strings.HasPrefix(resp.Type, "application/json") {
bytes := []byte(resp.Result.(string))

parsed := models.AttributeBigint{}.New(bytes)

err = json.Unmarshal(bytes, parsed)
if err != nil {
return nil, err
}

return parsed, nil
}
var parsed models.AttributeBigint
parsed, ok := resp.Result.(models.AttributeBigint)
if !ok {
return nil, errors.New("unexpected response type")
}
return &parsed, nil

}
type CreateBooleanAttributeOptions struct {
Default bool
Expand Down
64 changes: 64 additions & 0 deletions databases/databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,70 @@ func TestDatabases(t *testing.T) {
}
})

t.Run("Test CreateBigIntAttribute", func(t *testing.T) {
mockResponse := `
{
"key": "count",
"type": "bigint",
"status": "available",
"error": "string",
"required": true,
"$createdAt": "2020-10-15T06:38:00.000+00:00",
"$updatedAt": "2020-10-15T06:38:00.000+00:00"
}
`

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected method POST, got %s", r.Method)
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(mockResponse))
}))
defer ts.Close()

srv := New(newTestClient(ts))

_, err := srv.CreateBigIntAttribute("<DATABASE_ID>", "<COLLECTION_ID>", "", true)
if err != nil {
t.Errorf("Method CreateBigIntAttribute failed: %v", err)
}
})

t.Run("Test UpdateBigIntAttribute", func(t *testing.T) {
mockResponse := `
{
"key": "count",
"type": "bigint",
"status": "available",
"error": "string",
"required": true,
"$createdAt": "2020-10-15T06:38:00.000+00:00",
"$updatedAt": "2020-10-15T06:38:00.000+00:00"
}
`

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PATCH" {
t.Errorf("Expected method PATCH, got %s", r.Method)
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(mockResponse))
}))
defer ts.Close()

srv := New(newTestClient(ts))

_, err := srv.UpdateBigIntAttribute("<DATABASE_ID>", "<COLLECTION_ID>", "", true, 1)
if err != nil {
t.Errorf("Method UpdateBigIntAttribute failed: %v", err)
}
})

t.Run("Test CreateBooleanAttribute", func(t *testing.T) {
mockResponse := `
{
Expand Down
28 changes: 28 additions & 0 deletions docs/examples/databases/create-big-int-attribute.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
```go
package main

import (
"fmt"
"github.com/appwrite/sdk-for-go/v3/client"
"github.com/appwrite/sdk-for-go/v3/databases"
)

client := client.New(
client.WithEndpoint("https://<REGION>.cloud.appwrite.io/v1")
client.WithProject("<YOUR_PROJECT_ID>")
client.WithKey("<YOUR_API_KEY>")
)

service := databases.New(client)

response, error := service.CreateBigIntAttribute(
"<DATABASE_ID>",
"<COLLECTION_ID>",
"",
false,
databases.WithCreateBigIntAttributeMin(0),
databases.WithCreateBigIntAttributeMax(0),
databases.WithCreateBigIntAttributeDefault(0),
databases.WithCreateBigIntAttributeArray(false),
)
```
Loading