Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge latest python2 sdk #11

Merged
merged 8 commits into from
Aug 11, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 54 additions & 21 deletions lib/evernote/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import functools
import inspect
import re
from requests_oauthlib import OAuth1Session

import oauth2 as oauth
import urllib.error
import urllib.parse
import urllib.request
Expand All @@ -30,32 +30,65 @@ def __init__(self, **options):
self.token = options.get('token')
self.secret = options.get('secret')

def _get_oauth_client(self, token=None):
consumer = oauth.Consumer(self.consumer_key, self.consumer_secret)
if token:
client = oauth.Client(consumer, token)
else:
client = oauth.Client(consumer)
return client

def get_request_token(self, callback_url):
client = OAuth1Session(client_key=self.consumer_key,
client_secret=self.consumer_secret)

request_url = '%s?oauth_callback=%s' % (
self._get_endpoint('oauth'), urllib.parse.quote_plus(callback_url))
request_token = client.fetch_request_token(request_url)
client = self._get_oauth_client()
request_url = '{}?oauth_callback={}'.format(
self._get_endpoint('oauth'), urllib.parse.quote(callback_url)
)

resp, content = client.request(request_url, 'GET')
request_token = dict(urllib.parse.parse_qsl(content.decode('utf-8')))
return request_token

def get_authorize_url(self, request_token):
return '%s?oauth_token=%s' % (
return '{}?oauth_token={}'.format(
self._get_endpoint('OAuth.action'),
urllib.parse.quote(request_token['oauth_token']))

def get_access_token(
self, oauth_token, oauth_token_secret, oauth_verifier
):
client = OAuth1Session(client_key=self.consumer_key,
client_secret=self.consumer_secret,
resource_owner_key=oauth_token,
resource_owner_secret=oauth_token_secret,
verifier=oauth_verifier)
access_token_url = self._get_endpoint('oauth')
access_token = client.fetch_access_token(access_token_url)
self.token = access_token['oauth_token']
return self.token
urllib.parse.quote(request_token['oauth_token'])
)

def get_access_token(self, oauth_token,
oauth_token_secret, oauth_verifier, return_full_dict=False):
token = oauth.Token(oauth_token, oauth_token_secret)
token.set_verifier(oauth_verifier)
client = self._get_oauth_client(token)

resp, content = client.request(self._get_endpoint('oauth'), 'POST')
access_token_dict = dict(urllib.parse.parse_qsl(content.decode('utf-8')))
self.token = access_token_dict['oauth_token']

if return_full_dict:
return access_token_dict

return access_token_dict['oauth_token']

def get_access_token_dict(self, oauth_token,
oauth_token_secret, oauth_verifier):
"""
Full dict looks like:

{'edam_shard': 's146',
'edam_noteStoreUrl': 'https://www.evernote.com/shard/s146/notestore',
'edam_userId': '19358593',
'edam_webApiUrlPrefix': 'https://www.evernote.com/shard/s146/',
'edam_expires': '1481161090922',
'oauth_token': '...'}

Unit of expire time is millisecond.
"""
access_token_dict = self.get_access_token(oauth_token=oauth_token,
oauth_token_secret=oauth_token_secret,
oauth_verifier=oauth_verifier,
return_full_dict=True)
return access_token_dict

def get_user_store(self):
user_store_uri = self._get_endpoint("/edam/user")
Expand Down
2 changes: 1 addition & 1 deletion lib/thrift/transport/THttpClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,4 @@ def flush(self):

# Decorate if we know how to timeout
if hasattr(socket, 'getdefaulttimeout'):
flush = __withTimeout(flush)
flush = __withTimeout(flush)
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
oauth2>=1.9.0.post1
oauthlib>=1.0.3
requests>=2.8.1
requests-oauthlib>=0.5.0
5 changes: 4 additions & 1 deletion sample/client/EDAMTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import binascii
import evernote.edam.userstore.constants as UserStoreConstants
import evernote.edam.type.ttypes as Types
import os

from evernote.api.client import EvernoteClient

Expand Down Expand Up @@ -66,7 +67,9 @@
# for the attachment. At a minimum, the Resource contains the binary attachment
# data, an MD5 hash of the binary data, and the attachment MIME type.
# It can also include attributes such as filename and location.
image = open('enlogo.png', 'rb').read()
image_path = constants_path = os.path.join(os.path.dirname(__file__), "enlogo.png")
with open(image_path, 'rb') as image_file:
image = image_file.read()
md5 = hashlib.md5()
md5.update(image)
hash = md5.digest()
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
27 changes: 18 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import os
from setuptools import setup, find_packages

constants = open('lib/evernote/edam/userstore/constants.py').read().split("\n")
for x in [x for x in constants if x.startswith('EDAM_VERSION')]:
exec(x)

# Load version number from Thrift-Compiler-generated .py file
constants_path = os.path.join(os.path.dirname(__file__), "lib", "evernote",
"edam", "userstore", "constants.py")

with open(constants_path) as constants_file:
constants = constants_file.read().split("\n")
for x in [x for x in constants if x.startswith('EDAM_VERSION')]:
exec(x)


def read_from_same_directory(filename):
with open(os.path.join(os.path.dirname(__file__), filename)) as open_file:
content = open_file.read()
return content

def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
name='evernote',
name='evernote3',
version="{major}.{minor}.0".format(major=EDAM_VERSION_MAJOR,
minor=EDAM_VERSION_MINOR),
author='Evernote Corporation',
author_email='api@evernote.com',
url='http://dev.evernote.com',
description='Evernote SDK for Python',
long_description=read('README.md'),
description='Evernote SDK for Python3',
#long_description=read_from_same_directory('README.md'),
packages=find_packages('lib'),
package_dir={'': 'lib'},
classifiers=[
'Development Status :: 4 - Production/Beta',
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Topic :: Software Development :: Libraries',
Expand Down