Skip to content

Commit

Permalink
add Client class
Browse files Browse the repository at this point in the history
  • Loading branch information
dreampuf committed Jun 21, 2015
1 parent e4a1e20 commit 9366031
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 4 deletions.
91 changes: 91 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package client

import (
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
"github.com/dreampuf/evernote-sdk-golang/userstore"
"github.com/mrjones/oauth"
"github.com/dreampuf/evernote-sdk-golang/notestore"
)

type EnvironmentType int

const (
SANDBOX EnvironmentType = iota
PRODUCTION
)

type EvernoteClient struct {
host string
oauthClient *oauth.Consumer
userStore *userstore.UserStoreClient
}

func NewClient(key, secret string, envType EnvironmentType) *EvernoteClient {
host := "www.evernote.com"
if envType == SANDBOX {
host = "sandbox.evernote.com"
}
client := oauth.NewConsumer(
key, secret,
oauth.ServiceProvider{
RequestTokenUrl: fmt.Sprintf("https://%s/oauth", host),
AuthorizeTokenUrl: fmt.Sprintf("https://%s/OAuth.action", host),
AccessTokenUrl: fmt.Sprintf("https://%s/oauth", host),
},
)
return &EvernoteClient{
host: host,
oauthClient: client,
}
}

func (c *EvernoteClient) GetRequestToken(callBackURL string) (*oauth.RequestToken, string, error) {
return c.oauthClient.GetRequestTokenAndUrl(callBackURL)
}

func (c *EvernoteClient) GetAuthorizedToken(requestToken *oauth.RequestToken, oauthVerifier string) (*oauth.AccessToken, error) {
return c.oauthClient.AuthorizeToken(requestToken, oauthVerifier)
}

func (c *EvernoteClient) GetUserStore() (*userstore.UserStoreClient, error) {
if c.userStore != nil {
return c.userStore, nil
}
evernoteUserStoreServerURL := fmt.Sprintf("https://%s/edam/user", c.host)
evernoteUserTrans, err := thrift.NewTHttpPostClient(evernoteUserStoreServerURL)
if err != nil {
return nil, err
}
c.userStore = userstore.NewUserStoreClientFactory(
evernoteUserTrans,
thrift.NewTBinaryProtocolFactoryDefault(),
)
return c.userStore, nil
}

func (c *EvernoteClient) GetNoteStore(authenticationToken string) (*notestore.NoteStoreClient, error) {
us, err := c.GetUserStore()
if err != nil {
return nil, err
}
notestoreURL, err := us.GetNoteStoreUrl(authenticationToken)
if err != nil {
return nil, err
}
ns, err := c.GetNoteStoreWithURL(notestoreURL)
return ns, nil
}

func (c *EvernoteClient) GetNoteStoreWithURL(notestoreURL string) (*notestore.NoteStoreClient, error) {
evernoteNoteTrans, err := thrift.NewTHttpPostClient(notestoreURL)
if err != nil {
return nil, err
}
client := notestore.NewNoteStoreClientFactory(
evernoteNoteTrans,
thrift.NewTBinaryProtocolFactoryDefault(),
)
return client, nil
}

50 changes: 50 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package client
import (
"testing"
)
const (
EvernoteKey string = ""
EvernoteSecret string = ""
EvernoteAuthorToken string = ""
)


func TestClient(t *testing.T) {
c := NewClient(EvernoteKey, EvernoteSecret, SANDBOX)
us, err := c.GetUserStore()
if err != nil {
t.Fatal(err)
}
url, err := us.GetNoteStoreUrl(EvernoteAuthorToken)
if err != nil {
t.Fatal(err)
}
if len(url) < 1 {
t.Fatal("Invalid URL")
}
ns, err := c.GetNoteStoreWithURL(url)
if err != nil {
t.Fatal(err)
}
note, err := ns.GetDefaultNotebook(EvernoteAuthorToken)
if err != nil {
t.Fatal(err)
}
if note == nil {
t.Fatal("Invalid Note")
}
}

func TestRequestToken(t *testing.T) {
c := NewClient(EvernoteKey, EvernoteSecret, SANDBOX)
requestToken, url, err := c.GetRequestToken("http://test/")
if err != nil {
t.Fatal(err)
}
if requestToken == nil {
t.Fatal("Failed token request")
}
if len(url) < 1 {
t.Fatal("Invalid URL")
}
}
6 changes: 2 additions & 4 deletions userstore/user_store-remote/user_store-remote.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9366031

Please sign in to comment.