Start fleshing out basic Client types from Go-NEB

This commit is contained in:
Kegan Dougal 2016-11-29 17:03:42 +00:00
parent d356e4af9e
commit 97f4e98e1d
3 changed files with 79 additions and 0 deletions

View File

@ -1,2 +1,4 @@
# gomatrix
[![GoDoc](https://godoc.org/github.com/matrix-org/gomatrix?status.svg)](https://godoc.org/github.com/matrix-org/gomatrix)
A Golang Matrix client

42
client.go Normal file
View File

@ -0,0 +1,42 @@
package gomatrix
import (
"net/http"
"net/url"
"sync"
)
// Client represents a Matrix client.
type Client struct {
HomeserverURL *url.URL // The base homeserver URL
Prefix string // The API prefix eg '/_matrix/client/r0'
UserID string // The user ID of the client. Used for forming HTTP paths which use the client's user ID.
AccessToken string // The access_token for the client.
syncingMutex sync.Mutex // protects syncingID
syncingID uint32 // Identifies the current Sync. Only one Sync can be active at any given time.
Client *http.Client // The underlying HTTP client which will be used to make HTTP requests.
FilterStorer FilterStorer // Interface for saving and loading the filter ID for sync.
NextBatchStorer NextBatchStorer // Interface for saving and loading the "next_batch" sync token.
// TODO: Worker and Rooms
}
// NewClient creates a new Matrix Client ready for syncing
func NewClient(httpClient *http.Client, homeserverURL *url.URL, accessToken, userID string) *Client {
cli := Client{
AccessToken: accessToken,
HomeserverURL: homeserverURL,
UserID: userID,
Prefix: "/_matrix/client/r0",
}
// By default, use a no-op next_batch storer which will never save tokens and always
// "load" the empty string as a token. The client will work with this storer: it just won't
// remember the token across restarts. In practice, a database backend should be used.
cli.NextBatchStorer = NopNextBatchStore{}
// By default, use a no-op filter storer which will never save the filter ID and always
// "load" nothing. The client will work with this storer: it just won't remember the filter
// ID across restarts and hence request a new one. In practice, a database backend should be used.
cli.FilterStorer = NopFilterStore{}
cli.Client = httpClient
return &cli
}

35
sync.go Normal file
View File

@ -0,0 +1,35 @@
package gomatrix
// NextBatchStorer controls loading/saving of next_batch tokens for users
type NextBatchStorer interface {
// Save a next_batch token for a given user. Best effort.
Save(userID, nextBatch string)
// Load a next_batch token for a given user. Return an empty string if no token exists.
Load(userID string) string
}
// NopNextBatchStore does not load or save next_batch tokens.
type NopNextBatchStore struct{}
// Save does nothing
func (s NopNextBatchStore) Save(userID, nextBatch string) {}
// Load does nothing
func (s NopNextBatchStore) Load(userID string) string { return "" }
// FilterStorer controls loading/saving of filter IDs for users
type FilterStorer interface {
// Save a filter ID for a given user. Best effort.
Save(userID, filterID string)
// Load a filter ID for a given user. Return an empty string if no token exists.
Load(userID string) string
}
// NopFilterStore does not load or save filter IDs.
type NopFilterStore struct{}
// Save does nothing
func (s NopFilterStore) Save(userID, filterID string) {}
// Load does nothing
func (s NopFilterStore) Load(userID string) string { return "" }