-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
143 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.