pax_global_header00006660000000000000000000000064130417041240014506gustar00rootroot0000000000000052 comment=5f345ff819e3508a5765e47c26be7b5a4b634425 python-fitbit-0.3.0/000077500000000000000000000000001304170412400143065ustar00rootroot00000000000000python-fitbit-0.3.0/.gitignore000066400000000000000000000001561304170412400163000ustar00rootroot00000000000000*.pyc *.DS_Store .coverage .tox *~ docs/_build *.egg-info *.egg .eggs dist build env htmlcov # Editors .idea python-fitbit-0.3.0/.travis.yml000066400000000000000000000005401304170412400164160ustar00rootroot00000000000000language: python python: 3.5 env: # Avoid testing pypy on travis until the following issue is fixed: # https://github.com/travis-ci/travis-ci/issues/4756 #- TOX_ENV=pypy - TOX_ENV=py35 - TOX_ENV=py34 - TOX_ENV=py33 - TOX_ENV=py27 - TOX_ENV=docs install: - pip install coveralls tox script: tox -e $TOX_ENV after_success: coveralls python-fitbit-0.3.0/AUTHORS000066400000000000000000000004221304170412400153540ustar00rootroot00000000000000Issac Kelly (Kelly Creative Tech) Percy Perez (ORCAS) Rebecca Lovewell (Caktus Consulting Group) Dan Poirier (Caktus Consulting Group) Brad Pitcher (ORCAS) Silvio Tomatis Steven Skoczen Eric Xu Josh Gachnang Lorenzo Mancini David Grandinetti Chris Streeter Mario Sangiorgio python-fitbit-0.3.0/CHANGELOG.rst000066400000000000000000000033761304170412400163400ustar00rootroot000000000000000.2.4 (2016-11-10) ================== * Call a hook if it exists when tokens are refreshed 0.2.3 (2016-07-06) ================== * Refresh token when it expires 0.2.2 (2016-03-30) ================== * Refresh token bugfixes 0.2.1 (2016-03-28) ================== * Update requirements to use requests-oauthlib>=0.6.1 0.2 (2016-03-23) ================ * Drop OAuth1 support. See `OAuth1 deprecated `_ * Drop py26 and py32 support 0.1.3 (2015-02-04) ================== * Support Intraday Time Series API * Use connection pooling to avoid a TCP and SSL handshake for every API call 0.1.2 (2014-09-19) ================== * Quick fix for response objects without a status code 0.1.1 (2014-09-18) ================== * Fix the broken foods log date endpoint * Integrate with travis-ci.org, coveralls.io, and requires.io * Add HTTPTooManyRequests exception with retry_after_secs information * Enable adding parameters to authorize token URL 0.1.0 (2014-04-15) ================== * Officially test/support Python 3.2+ and PyPy in addition to Python 2.x * Clean up OAuth workflow, change the API slightly to match oauthlib terminology * Fix some minor bugs 0.0.5 (2014-03-30) ================== * Switch from python-oauth2 to the better supported oauthlib * Add get_bodyweight and get_bodyfat methods 0.0.3 (2014-02-05) ================== * Add get_badges method * Include error messages in the exception * Add API for alarms * Add API for log activity * Correctly pass headers on requests * Way more test coverage * Publish to PyPI 0.0.2 (2012-10-02) ================== * Add docs, including Readthedocs support * Add tests * Use official oauth2 version from pypi 0.0.1 (2012-02-25) ================== * Initial release python-fitbit-0.3.0/LICENSE000066400000000000000000000011021304170412400153050ustar00rootroot00000000000000Copyright 2012-2017 ORCAS Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. python-fitbit-0.3.0/MANIFEST.in000066400000000000000000000000711304170412400160420ustar00rootroot00000000000000include LICENSE AUTHORS README.rst requirements/* docs/* python-fitbit-0.3.0/README.rst000066400000000000000000000031151304170412400157750ustar00rootroot00000000000000python-fitbit ============= .. image:: https://travis-ci.org/orcasgit/python-fitbit.svg?branch=master :target: https://travis-ci.org/orcasgit/python-fitbit :alt: Build Status .. image:: https://coveralls.io/repos/orcasgit/python-fitbit/badge.png?branch=master :target: https://coveralls.io/r/orcasgit/python-fitbit?branch=master :alt: Coverage Status .. image:: https://requires.io/github/orcasgit/python-fitbit/requirements.png?branch=master :target: https://requires.io/github/orcasgit/python-fitbit/requirements/?branch=master :alt: Requirements Status Fitbit API Python Client Implementation For documentation: `http://python-fitbit.readthedocs.org/ `_ Requirements ============ * Python 2.7+ * `python-dateutil`_ (always) * `requests-oauthlib`_ (always) * `Sphinx`_ (to create the documention) * `tox`_ (for running the tests) * `coverage`_ (to create test coverage reports) .. _python-dateutil: https://pypi.python.org/pypi/python-dateutil/2.4.0 .. _requests-oauthlib: https://pypi.python.org/pypi/requests-oauthlib .. _Sphinx: https://pypi.python.org/pypi/Sphinx .. _tox: https://pypi.python.org/pypi/tox .. _coverage: https://pypi.python.org/pypi/coverage/ To use the library, you need to install the run time requirements: sudo pip install -r requirements/base.txt To modify and test the library, you need to install the developer requirements: sudo pip install -r requirements/dev.txt To run the library on a continuous integration server, you need to install the test requirements: sudo pip install -r requirements/test.txt python-fitbit-0.3.0/docs/000077500000000000000000000000001304170412400152365ustar00rootroot00000000000000python-fitbit-0.3.0/docs/Makefile000066400000000000000000000127301304170412400167010ustar00rootroot00000000000000# Makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build PAPER = BUILDDIR = _build # Internal variables. PAPEROPT_a4 = -D latex_paper_size=a4 PAPEROPT_letter = -D latex_paper_size=letter ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . # the i18n builder cannot share the environment and doctrees with the others I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext help: @echo "Please use \`make ' where is one of" @echo " html to make standalone HTML files" @echo " dirhtml to make HTML files named index.html in directories" @echo " singlehtml to make a single large HTML file" @echo " pickle to make pickle files" @echo " json to make JSON files" @echo " htmlhelp to make HTML files and a HTML help project" @echo " qthelp to make HTML files and a qthelp project" @echo " devhelp to make HTML files and a Devhelp project" @echo " epub to make an epub" @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" @echo " latexpdf to make LaTeX files and run them through pdflatex" @echo " text to make text files" @echo " man to make manual pages" @echo " texinfo to make Texinfo files" @echo " info to make Texinfo files and run them through makeinfo" @echo " gettext to make PO message catalogs" @echo " changes to make an overview of all changed/added/deprecated items" @echo " linkcheck to check all external links for integrity" @echo " doctest to run all doctests embedded in the documentation (if enabled)" clean: -rm -rf $(BUILDDIR)/* html: $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." dirhtml: $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." singlehtml: $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml @echo @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." pickle: $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle @echo @echo "Build finished; now you can process the pickle files." json: $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json @echo @echo "Build finished; now you can process the JSON files." htmlhelp: $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp @echo @echo "Build finished; now you can run HTML Help Workshop with the" \ ".hhp project file in $(BUILDDIR)/htmlhelp." qthelp: $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp @echo @echo "Build finished; now you can run "qcollectiongenerator" with the" \ ".qhcp project file in $(BUILDDIR)/qthelp, like this:" @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Python-Fitbit.qhcp" @echo "To view the help file:" @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Python-Fitbit.qhc" devhelp: $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp @echo @echo "Build finished." @echo "To view the help file:" @echo "# mkdir -p $$HOME/.local/share/devhelp/Python-Fitbit" @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Python-Fitbit" @echo "# devhelp" epub: $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub @echo @echo "Build finished. The epub file is in $(BUILDDIR)/epub." latex: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." @echo "Run \`make' in that directory to run these through (pdf)latex" \ "(use \`make latexpdf' here to do that automatically)." latexpdf: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through pdflatex..." $(MAKE) -C $(BUILDDIR)/latex all-pdf @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." text: $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text @echo @echo "Build finished. The text files are in $(BUILDDIR)/text." man: $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man @echo @echo "Build finished. The manual pages are in $(BUILDDIR)/man." texinfo: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." @echo "Run \`make' in that directory to run these through makeinfo" \ "(use \`make info' here to do that automatically)." info: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo "Running Texinfo files through makeinfo..." make -C $(BUILDDIR)/texinfo info @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." gettext: $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale @echo @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." changes: $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes @echo @echo "The overview file is in $(BUILDDIR)/changes." linkcheck: $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck @echo @echo "Link check complete; look for any errors in the above output " \ "or in $(BUILDDIR)/linkcheck/output.txt." doctest: $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest @echo "Testing of doctests in the sources finished, look at the " \ "results in $(BUILDDIR)/doctest/output.txt." python-fitbit-0.3.0/docs/conf.py000066400000000000000000000174201304170412400165410ustar00rootroot00000000000000# -*- coding: utf-8 -*- # # Python-Fitbit documentation build configuration file, created by # sphinx-quickstart on Wed Mar 14 18:51:57 2012. # # This file is execfile()d with the current directory set to its containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys, os # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. sys.path.insert(0, os.path.abspath('..')) # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. #needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.viewcode' ] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. import fitbit project = u'Python-Fitbit' copyright = fitbit.__copyright__ # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. version = fitbit.__version__ # The full version, including alpha/beta/rc tags. release = fitbit.__release__ # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. #language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ['_build'] # The reST default role (used for this markup: `text`) to use for all documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # -- Options for HTML output --------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. html_theme = 'alabaster' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. #html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. #html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". #html_title = None # A shorter title for the navigation bar. Default is the same as html_title. #html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. #html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = [] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_domain_indices = True # If false, no index is generated. #html_use_index = True # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. #html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. #html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. #html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = 'Python-Fitbitdoc' # -- Options for LaTeX output -------------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). #'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). #'pointsize': '10pt', # Additional stuff for the LaTeX preamble. #'preamble': '', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ ('index', 'Python-Fitbit.tex', u'Python-Fitbit Documentation', u'Issac Kelly, Percy Perez, Brad Pitcher', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output -------------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ ('index', 'python-fitbit', u'Python-Fitbit Documentation', [u'Issac Kelly, Percy Perez, Brad Pitcher'], 1) ] # If true, show URL addresses after external links. #man_show_urls = False # -- Options for Texinfo output ------------------------------------------------ # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ ('index', 'Python-Fitbit', u'Python-Fitbit Documentation', u'Issac Kelly, Percy Perez, Brad Pitcher', 'Python-Fitbit', 'Fitbit API Python Client Implementation', 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. #texinfo_appendices = [] # If false, no module index is generated. #texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. #texinfo_show_urls = 'footnote' python-fitbit-0.3.0/docs/index.rst000066400000000000000000000030071304170412400170770ustar00rootroot00000000000000.. Python-Fitbit documentation master file, created by sphinx-quickstart on Wed Mar 14 18:51:57 2012. You can adapt this file completely to your liking, but it should at least contain the root `toctree` directive. Overview ======== This is a complete python implementation of the Fitbit API. It uses oAuth for authentication, it supports both us and si measurements Quickstart ========== If you are only retrieving data that doesn't require authorization, then you can use the unauthorized interface:: import fitbit unauth_client = fitbit.Fitbit('', '') # certain methods do not require user keys unauth_client.food_units() Here is an example of authorizing with OAuth 2.0:: # You'll have to gather the tokens on your own, or use # ./gather_keys_oauth2.py authd_client = fitbit.Fitbit('', '', access_token='', refresh_token='') authd_client.sleep() Fitbit API ========== Some assumptions you should note. Anywhere it says user_id=None, it assumes the current user_id from the credentials given, and passes a ``-`` through the API. Anywhere it says date=None, it should accept either ``None`` or a ``date`` or ``datetime`` object (anything with proper strftime will do), or a string formatted as ``%Y-%m-%d``. .. autoclass:: fitbit.Fitbit :private-members: :members: Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search` python-fitbit-0.3.0/docs/make.bat000066400000000000000000000117661304170412400166560ustar00rootroot00000000000000@ECHO OFF REM Command file for Sphinx documentation if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build ) set BUILDDIR=_build set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . set I18NSPHINXOPTS=%SPHINXOPTS% . if NOT "%PAPER%" == "" ( set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% ) if "%1" == "" goto help if "%1" == "help" ( :help echo.Please use `make ^` where ^ is one of echo. html to make standalone HTML files echo. dirhtml to make HTML files named index.html in directories echo. singlehtml to make a single large HTML file echo. pickle to make pickle files echo. json to make JSON files echo. htmlhelp to make HTML files and a HTML help project echo. qthelp to make HTML files and a qthelp project echo. devhelp to make HTML files and a Devhelp project echo. epub to make an epub echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter echo. text to make text files echo. man to make manual pages echo. texinfo to make Texinfo files echo. gettext to make PO message catalogs echo. changes to make an overview over all changed/added/deprecated items echo. linkcheck to check all external links for integrity echo. doctest to run all doctests embedded in the documentation if enabled goto end ) if "%1" == "clean" ( for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i del /q /s %BUILDDIR%\* goto end ) if "%1" == "html" ( %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/html. goto end ) if "%1" == "dirhtml" ( %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. goto end ) if "%1" == "singlehtml" ( %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. goto end ) if "%1" == "pickle" ( %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the pickle files. goto end ) if "%1" == "json" ( %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the JSON files. goto end ) if "%1" == "htmlhelp" ( %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run HTML Help Workshop with the ^ .hhp project file in %BUILDDIR%/htmlhelp. goto end ) if "%1" == "qthelp" ( %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run "qcollectiongenerator" with the ^ .qhcp project file in %BUILDDIR%/qthelp, like this: echo.^> qcollectiongenerator %BUILDDIR%\qthelp\Python-Fitbit.qhcp echo.To view the help file: echo.^> assistant -collectionFile %BUILDDIR%\qthelp\Python-Fitbit.ghc goto end ) if "%1" == "devhelp" ( %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp if errorlevel 1 exit /b 1 echo. echo.Build finished. goto end ) if "%1" == "epub" ( %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub if errorlevel 1 exit /b 1 echo. echo.Build finished. The epub file is in %BUILDDIR%/epub. goto end ) if "%1" == "latex" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex if errorlevel 1 exit /b 1 echo. echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. goto end ) if "%1" == "text" ( %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text if errorlevel 1 exit /b 1 echo. echo.Build finished. The text files are in %BUILDDIR%/text. goto end ) if "%1" == "man" ( %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man if errorlevel 1 exit /b 1 echo. echo.Build finished. The manual pages are in %BUILDDIR%/man. goto end ) if "%1" == "texinfo" ( %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo if errorlevel 1 exit /b 1 echo. echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. goto end ) if "%1" == "gettext" ( %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale if errorlevel 1 exit /b 1 echo. echo.Build finished. The message catalogs are in %BUILDDIR%/locale. goto end ) if "%1" == "changes" ( %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes if errorlevel 1 exit /b 1 echo. echo.The overview file is in %BUILDDIR%/changes. goto end ) if "%1" == "linkcheck" ( %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck if errorlevel 1 exit /b 1 echo. echo.Link check complete; look for any errors in the above output ^ or in %BUILDDIR%/linkcheck/output.txt. goto end ) if "%1" == "doctest" ( %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest if errorlevel 1 exit /b 1 echo. echo.Testing of doctests in the sources finished, look at the ^ results in %BUILDDIR%/doctest/output.txt. goto end ) :end python-fitbit-0.3.0/fitbit/000077500000000000000000000000001304170412400155675ustar00rootroot00000000000000python-fitbit-0.3.0/fitbit/__init__.py000066400000000000000000000007061304170412400177030ustar00rootroot00000000000000# -*- coding: utf-8 -*- """ Fitbit API Library ------------------ :copyright: 2012-2017 ORCAS. :license: BSD, see LICENSE for more details. """ from .api import Fitbit, FitbitOauth2Client # Meta. __title__ = 'fitbit' __author__ = 'Issac Kelly and ORCAS' __author_email__ = 'bpitcher@orcasinc.com' __copyright__ = 'Copyright 2012-2017 ORCAS' __license__ = 'Apache 2.0' __version__ = '0.3.0' __release__ = '0.3.0' # Module namespace. all_tests = [] python-fitbit-0.3.0/fitbit/api.py000066400000000000000000001076401304170412400167220ustar00rootroot00000000000000# -*- coding: utf-8 -*- import datetime import json import requests try: from urllib.parse import urlencode except ImportError: # Python 2.x from urllib import urlencode from requests.auth import HTTPBasicAuth from requests_oauthlib import OAuth2Session from . import exceptions from .compliance import fitbit_compliance_fix from .utils import curry class FitbitOauth2Client(object): API_ENDPOINT = "https://api.fitbit.com" AUTHORIZE_ENDPOINT = "https://www.fitbit.com" API_VERSION = 1 request_token_url = "%s/oauth2/token" % API_ENDPOINT authorization_url = "%s/oauth2/authorize" % AUTHORIZE_ENDPOINT access_token_url = request_token_url refresh_token_url = request_token_url def __init__(self, client_id, client_secret, access_token=None, refresh_token=None, expires_at=None, refresh_cb=None, redirect_uri=None, *args, **kwargs): """ Create a FitbitOauth2Client object. Specify the first 7 parameters if you have them to access user data. Specify just the first 2 parameters to start the setup for user authorization (as an example see gather_key_oauth2.py) - client_id, client_secret are in the app configuration page https://dev.fitbit.com/apps - access_token, refresh_token are obtained after the user grants permission """ self.client_id, self.client_secret = client_id, client_secret token = {} if access_token and refresh_token: token.update({ 'access_token': access_token, 'refresh_token': refresh_token }) if expires_at: token['expires_at'] = expires_at self.session = fitbit_compliance_fix(OAuth2Session( client_id, auto_refresh_url=self.refresh_token_url, token_updater=refresh_cb, token=token, redirect_uri=redirect_uri, )) self.timeout = kwargs.get("timeout", None) def _request(self, method, url, **kwargs): """ A simple wrapper around requests. """ if self.timeout is not None and 'timeout' not in kwargs: kwargs['timeout'] = self.timeout try: response = self.session.request(method, url, **kwargs) # If our current token has no expires_at, or something manages to slip # through that check if response.status_code == 401: d = json.loads(response.content.decode('utf8')) if d['errors'][0]['errorType'] == 'expired_token': self.refresh_token() response = self.session.request(method, url, **kwargs) return response except requests.Timeout as e: raise exceptions.Timeout(*e.args) def make_request(self, url, data=None, method=None, **kwargs): """ Builds and makes the OAuth2 Request, catches errors https://dev.fitbit.com/docs/oauth2/#authorization-errors """ data = data or {} method = method or ('POST' if data else 'GET') response = self._request( method, url, data=data, client_id=self.client_id, client_secret=self.client_secret, **kwargs ) exceptions.detect_and_raise_error(response) return response def authorize_token_url(self, scope=None, redirect_uri=None, **kwargs): """Step 1: Return the URL the user needs to go to in order to grant us authorization to look at their data. Then redirect the user to that URL, open their browser to it, or tell them to copy the URL into their browser. - scope: pemissions that that are being requested [default ask all] - redirect_uri: url to which the reponse will posted. required here unless you specify only one Callback URL on the fitbit app or you already passed it to the constructor for more info see https://dev.fitbit.com/docs/oauth2/ """ self.session.scope = scope or [ "activity", "nutrition", "heartrate", "location", "nutrition", "profile", "settings", "sleep", "social", "weight", ] if redirect_uri: self.session.redirect_uri = redirect_uri return self.session.authorization_url(self.authorization_url, **kwargs) def fetch_access_token(self, code, redirect_uri=None): """Step 2: Given the code from fitbit from step 1, call fitbit again and returns an access token object. Extract the needed information from that and save it to use in future API calls. the token is internally saved """ if redirect_uri: self.session.redirect_uri = redirect_uri return self.session.fetch_token( self.access_token_url, username=self.client_id, password=self.client_secret, code=code) def refresh_token(self): """Step 3: obtains a new access_token from the the refresh token obtained in step 2. Only do the refresh if there is `token_updater(),` which saves the token. """ token = {} if self.session.token_updater: token = self.session.refresh_token( self.refresh_token_url, auth=HTTPBasicAuth(self.client_id, self.client_secret) ) self.session.token_updater(token) return token class Fitbit(object): US = 'en_US' METRIC = 'en_UK' API_ENDPOINT = "https://api.fitbit.com" API_VERSION = 1 WEEK_DAYS = ['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY'] PERIODS = ['1d', '7d', '30d', '1w', '1m', '3m', '6m', '1y', 'max'] RESOURCE_LIST = [ 'body', 'activities', 'foods/log', 'foods/log/water', 'sleep', 'heart', 'bp', 'glucose', ] QUALIFIERS = [ 'recent', 'favorite', 'frequent', ] def __init__(self, client_id, client_secret, system=US, **kwargs): """ Fitbit(, , access_token=, refresh_token=) """ self.system = system self.client = FitbitOauth2Client(client_id, client_secret, **kwargs) # All of these use the same patterns, define the method for accessing # creating and deleting records once, and use curry to make individual # Methods for each for resource in Fitbit.RESOURCE_LIST: underscore_resource = resource.replace('/', '_') setattr(self, underscore_resource, curry(self._COLLECTION_RESOURCE, resource)) if resource not in ['body', 'glucose']: # Body and Glucose entries are not currently able to be deleted setattr(self, 'delete_%s' % underscore_resource, curry( self._DELETE_COLLECTION_RESOURCE, resource)) for qualifier in Fitbit.QUALIFIERS: setattr(self, '%s_activities' % qualifier, curry(self.activity_stats, qualifier=qualifier)) setattr(self, '%s_foods' % qualifier, curry(self._food_stats, qualifier=qualifier)) def make_request(self, *args, **kwargs): # This should handle data level errors, improper requests, and bad # serialization headers = kwargs.get('headers', {}) headers.update({'Accept-Language': self.system}) kwargs['headers'] = headers method = kwargs.get('method', 'POST' if 'data' in kwargs else 'GET') response = self.client.make_request(*args, **kwargs) if response.status_code == 202: return True if method == 'DELETE': if response.status_code == 204: return True else: raise exceptions.DeleteError(response) try: rep = json.loads(response.content.decode('utf8')) except ValueError: raise exceptions.BadResponse return rep def user_profile_get(self, user_id=None): """ Get a user profile. You can get other user's profile information by passing user_id, or you can get the current user's by not passing a user_id .. note: This is not the same format that the GET comes back in, GET requests are wrapped in {'user': } https://dev.fitbit.com/docs/user/ """ url = "{0}/{1}/user/{2}/profile.json".format(*self._get_common_args(user_id)) return self.make_request(url) def user_profile_update(self, data): """ Set a user profile. You can set your user profile information by passing a dictionary of attributes that will be updated. .. note: This is not the same format that the GET comes back in, GET requests are wrapped in {'user': } https://dev.fitbit.com/docs/user/#update-profile """ url = "{0}/{1}/user/-/profile.json".format(*self._get_common_args()) return self.make_request(url, data) def _get_common_args(self, user_id=None): common_args = (self.API_ENDPOINT, self.API_VERSION,) if not user_id: user_id = '-' common_args += (user_id,) return common_args def _get_date_string(self, date): if not isinstance(date, str): return date.strftime('%Y-%m-%d') return date def _COLLECTION_RESOURCE(self, resource, date=None, user_id=None, data=None): """ Retrieving and logging of each type of collection data. Arguments: resource, defined automatically via curry [date] defaults to today [user_id] defaults to current logged in user [data] optional, include for creating a record, exclude for access This implements the following methods:: body(date=None, user_id=None, data=None) activities(date=None, user_id=None, data=None) foods_log(date=None, user_id=None, data=None) foods_log_water(date=None, user_id=None, data=None) sleep(date=None, user_id=None, data=None) heart(date=None, user_id=None, data=None) bp(date=None, user_id=None, data=None) * https://dev.fitbit.com/docs/ """ if not date: date = datetime.date.today() date_string = self._get_date_string(date) kwargs = {'resource': resource, 'date': date_string} if not data: base_url = "{0}/{1}/user/{2}/{resource}/date/{date}.json" else: data['date'] = date_string base_url = "{0}/{1}/user/{2}/{resource}.json" url = base_url.format(*self._get_common_args(user_id), **kwargs) return self.make_request(url, data) def _DELETE_COLLECTION_RESOURCE(self, resource, log_id): """ deleting each type of collection data Arguments: resource, defined automatically via curry log_id, required, log entry to delete This builds the following methods:: delete_body(log_id) delete_activities(log_id) delete_foods_log(log_id) delete_foods_log_water(log_id) delete_sleep(log_id) delete_heart(log_id) delete_bp(log_id) """ url = "{0}/{1}/user/-/{resource}/{log_id}.json".format( *self._get_common_args(), resource=resource, log_id=log_id ) response = self.make_request(url, method='DELETE') return response def _resource_goal(self, resource, data={}, period=None): """ Handles GETting and POSTing resource goals of all types """ url = "{0}/{1}/user/-/{resource}/goal{postfix}.json".format( *self._get_common_args(), resource=resource, postfix=('s/' + period) if period else '' ) return self.make_request(url, data=data) def _filter_nones(self, data): filter_nones = lambda item: item[1] is not None filtered_kwargs = list(filter(filter_nones, data.items())) return {} if not filtered_kwargs else dict(filtered_kwargs) def body_fat_goal(self, fat=None): """ Implements the following APIs * https://dev.fitbit.com/docs/body/#get-body-goals * https://dev.fitbit.com/docs/body/#update-body-fat-goal Pass no arguments to get the body fat goal. Pass a ``fat`` argument to update the body fat goal. Arguments: * ``fat`` -- Target body fat in %; in the format X.XX """ return self._resource_goal('body/log/fat', {'fat': fat} if fat else {}) def body_weight_goal(self, start_date=None, start_weight=None, weight=None): """ Implements the following APIs * https://dev.fitbit.com/docs/body/#get-body-goals * https://dev.fitbit.com/docs/body/#update-weight-goal Pass no arguments to get the body weight goal. Pass ``start_date``, ``start_weight`` and optionally ``weight`` to set the weight goal. ``weight`` is required if it hasn't been set yet. Arguments: * ``start_date`` -- Weight goal start date; in the format yyyy-MM-dd * ``start_weight`` -- Weight goal start weight; in the format X.XX * ``weight`` -- Weight goal target weight; in the format X.XX """ data = self._filter_nones({ 'startDate': start_date, 'startWeight': start_weight, 'weight': weight }) if data and not ('startDate' in data and 'startWeight' in data): raise ValueError('start_date and start_weight are both required') return self._resource_goal('body/log/weight', data) def activities_daily_goal(self, calories_out=None, active_minutes=None, floors=None, distance=None, steps=None): """ Implements the following APIs for period equal to daily https://dev.fitbit.com/docs/activity/#get-activity-goals https://dev.fitbit.com/docs/activity/#update-activity-goals Pass no arguments to get the daily activities goal. Pass any one of the optional arguments to set that component of the daily activities goal. Arguments: * ``calories_out`` -- New goal value; in an integer format * ``active_minutes`` -- New goal value; in an integer format * ``floors`` -- New goal value; in an integer format * ``distance`` -- New goal value; in the format X.XX or integer * ``steps`` -- New goal value; in an integer format """ data = self._filter_nones({ 'caloriesOut': calories_out, 'activeMinutes': active_minutes, 'floors': floors, 'distance': distance, 'steps': steps }) return self._resource_goal('activities', data, period='daily') def activities_weekly_goal(self, distance=None, floors=None, steps=None): """ Implements the following APIs for period equal to weekly https://dev.fitbit.com/docs/activity/#get-activity-goals https://dev.fitbit.com/docs/activity/#update-activity-goals Pass no arguments to get the weekly activities goal. Pass any one of the optional arguments to set that component of the weekly activities goal. Arguments: * ``distance`` -- New goal value; in the format X.XX or integer * ``floors`` -- New goal value; in an integer format * ``steps`` -- New goal value; in an integer format """ data = self._filter_nones({'distance': distance, 'floors': floors, 'steps': steps}) return self._resource_goal('activities', data, period='weekly') def food_goal(self, calories=None, intensity=None, personalized=None): """ Implements the following APIs https://dev.fitbit.com/docs/food-logging/#get-food-goals https://dev.fitbit.com/docs/food-logging/#update-food-goal Pass no arguments to get the food goal. Pass at least ``calories`` or ``intensity`` and optionally ``personalized`` to update the food goal. Arguments: * ``calories`` -- Manual Calorie Consumption Goal; calories, integer; * ``intensity`` -- Food Plan intensity; (MAINTENANCE, EASIER, MEDIUM, KINDAHARD, HARDER); * ``personalized`` -- Food Plan type; ``True`` or ``False`` """ data = self._filter_nones({'calories': calories, 'intensity': intensity, 'personalized': personalized}) if data and not ('calories' in data or 'intensity' in data): raise ValueError('Either calories or intensity is required') return self._resource_goal('foods/log', data) def water_goal(self, target=None): """ Implements the following APIs https://dev.fitbit.com/docs/food-logging/#get-water-goal https://dev.fitbit.com/docs/food-logging/#update-water-goal Pass no arguments to get the water goal. Pass ``target`` to update it. Arguments: * ``target`` -- Target water goal in the format X.X, will be set in unit based on locale """ data = self._filter_nones({'target': target}) return self._resource_goal('foods/log/water', data) def time_series(self, resource, user_id=None, base_date='today', period=None, end_date=None): """ The time series is a LOT of methods, (documented at urls below) so they don't get their own method. They all follow the same patterns, and return similar formats. Taking liberty, this assumes a base_date of today, the current user, and a 1d period. https://dev.fitbit.com/docs/activity/#activity-time-series https://dev.fitbit.com/docs/body/#body-time-series https://dev.fitbit.com/docs/food-logging/#food-or-water-time-series https://dev.fitbit.com/docs/heart-rate/#heart-rate-time-series https://dev.fitbit.com/docs/sleep/#sleep-time-series """ if period and end_date: raise TypeError("Either end_date or period can be specified, not both") if end_date: end = self._get_date_string(end_date) else: if not period in Fitbit.PERIODS: raise ValueError("Period must be one of %s" % ','.join(Fitbit.PERIODS)) end = period url = "{0}/{1}/user/{2}/{resource}/date/{base_date}/{end}.json".format( *self._get_common_args(user_id), resource=resource, base_date=self._get_date_string(base_date), end=end ) return self.make_request(url) def intraday_time_series(self, resource, base_date='today', detail_level='1min', start_time=None, end_time=None): """ The intraday time series extends the functionality of the regular time series, but returning data at a more granular level for a single day, defaulting to 1 minute intervals. To access this feature, one must fill out the Private Support form here (see https://dev.fitbit.com/docs/help/). For details on the resources available and more information on how to get access, see: https://dev.fitbit.com/docs/activity/#get-activity-intraday-time-series """ # Check that the time range is valid time_test = lambda t: not (t is None or isinstance(t, str) and not t) time_map = list(map(time_test, [start_time, end_time])) if not all(time_map) and any(time_map): raise TypeError('You must provide both the end and start time or neither') """ Per https://dev.fitbit.com/docs/activity/#get-activity-intraday-time-series the detail-level is now (OAuth 2.0 ): either "1min" or "15min" (optional). "1sec" for heart rate. """ if not detail_level in ['1sec', '1min', '15min']: raise ValueError("Period must be either '1sec', '1min', or '15min'") url = "{0}/{1}/user/-/{resource}/date/{base_date}/1d/{detail_level}".format( *self._get_common_args(), resource=resource, base_date=self._get_date_string(base_date), detail_level=detail_level ) if all(time_map): url = url + '/time' for time in [start_time, end_time]: time_str = time if not isinstance(time_str, str): time_str = time.strftime('%H:%M') url = url + ('/%s' % (time_str)) url = url + '.json' return self.make_request(url) def activity_stats(self, user_id=None, qualifier=''): """ * https://dev.fitbit.com/docs/activity/#activity-types * https://dev.fitbit.com/docs/activity/#get-favorite-activities * https://dev.fitbit.com/docs/activity/#get-recent-activity-types * https://dev.fitbit.com/docs/activity/#get-frequent-activities This implements the following methods:: recent_activities(user_id=None, qualifier='') favorite_activities(user_id=None, qualifier='') frequent_activities(user_id=None, qualifier='') """ if qualifier: if qualifier in Fitbit.QUALIFIERS: qualifier = '/%s' % qualifier else: raise ValueError("Qualifier must be one of %s" % ', '.join(Fitbit.QUALIFIERS)) else: qualifier = '' url = "{0}/{1}/user/{2}/activities{qualifier}.json".format( *self._get_common_args(user_id), qualifier=qualifier ) return self.make_request(url) def _food_stats(self, user_id=None, qualifier=''): """ This builds the convenience methods on initialization:: recent_foods(user_id=None, qualifier='') favorite_foods(user_id=None, qualifier='') frequent_foods(user_id=None, qualifier='') * https://dev.fitbit.com/docs/food-logging/#get-favorite-foods * https://dev.fitbit.com/docs/food-logging/#get-frequent-foods * https://dev.fitbit.com/docs/food-logging/#get-recent-foods """ url = "{0}/{1}/user/{2}/foods/log/{qualifier}.json".format( *self._get_common_args(user_id), qualifier=qualifier ) return self.make_request(url) def add_favorite_activity(self, activity_id): """ https://dev.fitbit.com/docs/activity/#add-favorite-activity """ url = "{0}/{1}/user/-/activities/favorite/{activity_id}.json".format( *self._get_common_args(), activity_id=activity_id ) return self.make_request(url, method='POST') def log_activity(self, data): """ https://dev.fitbit.com/docs/activity/#log-activity """ url = "{0}/{1}/user/-/activities.json".format(*self._get_common_args()) return self.make_request(url, data=data) def delete_favorite_activity(self, activity_id): """ https://dev.fitbit.com/docs/activity/#delete-favorite-activity """ url = "{0}/{1}/user/-/activities/favorite/{activity_id}.json".format( *self._get_common_args(), activity_id=activity_id ) return self.make_request(url, method='DELETE') def add_favorite_food(self, food_id): """ https://dev.fitbit.com/docs/food-logging/#add-favorite-food """ url = "{0}/{1}/user/-/foods/log/favorite/{food_id}.json".format( *self._get_common_args(), food_id=food_id ) return self.make_request(url, method='POST') def delete_favorite_food(self, food_id): """ https://dev.fitbit.com/docs/food-logging/#delete-favorite-food """ url = "{0}/{1}/user/-/foods/log/favorite/{food_id}.json".format( *self._get_common_args(), food_id=food_id ) return self.make_request(url, method='DELETE') def create_food(self, data): """ https://dev.fitbit.com/docs/food-logging/#create-food """ url = "{0}/{1}/user/-/foods.json".format(*self._get_common_args()) return self.make_request(url, data=data) def get_meals(self): """ https://dev.fitbit.com/docs/food-logging/#get-meals """ url = "{0}/{1}/user/-/meals.json".format(*self._get_common_args()) return self.make_request(url) def get_devices(self): """ https://dev.fitbit.com/docs/devices/#get-devices """ url = "{0}/{1}/user/-/devices.json".format(*self._get_common_args()) return self.make_request(url) def get_alarms(self, device_id): """ https://dev.fitbit.com/docs/devices/#get-alarms """ url = "{0}/{1}/user/-/devices/tracker/{device_id}/alarms.json".format( *self._get_common_args(), device_id=device_id ) return self.make_request(url) def add_alarm(self, device_id, alarm_time, week_days, recurring=False, enabled=True, label=None, snooze_length=None, snooze_count=None, vibe='DEFAULT'): """ https://dev.fitbit.com/docs/devices/#add-alarm alarm_time should be a timezone aware datetime object. """ url = "{0}/{1}/user/-/devices/tracker/{device_id}/alarms.json".format( *self._get_common_args(), device_id=device_id ) alarm_time = alarm_time.strftime("%H:%M%z") # Check week_days list if not isinstance(week_days, list): raise ValueError("Week days needs to be a list") for day in week_days: if day not in self.WEEK_DAYS: raise ValueError("Incorrect week day %s. see WEEK_DAY_LIST." % day) data = { 'time': alarm_time, 'weekDays': week_days, 'recurring': recurring, 'enabled': enabled, 'vibe': vibe } if label: data['label'] = label if snooze_length: data['snoozeLength'] = snooze_length if snooze_count: data['snoozeCount'] = snooze_count return self.make_request(url, data=data, method="POST") # return def update_alarm(self, device_id, alarm_id, alarm_time, week_days, recurring=False, enabled=True, label=None, snooze_length=None, snooze_count=None, vibe='DEFAULT'): """ https://dev.fitbit.com/docs/devices/#update-alarm alarm_time should be a timezone aware datetime object. """ # TODO Refactor with create_alarm. Tons of overlap. # Check week_days list if not isinstance(week_days, list): raise ValueError("Week days needs to be a list") for day in week_days: if day not in self.WEEK_DAYS: raise ValueError("Incorrect week day %s. see WEEK_DAY_LIST." % day) url = "{0}/{1}/user/-/devices/tracker/{device_id}/alarms/{alarm_id}.json".format( *self._get_common_args(), device_id=device_id, alarm_id=alarm_id ) alarm_time = alarm_time.strftime("%H:%M%z") data = { 'time': alarm_time, 'weekDays': week_days, 'recurring': recurring, 'enabled': enabled, 'vibe': vibe } if label: data['label'] = label if snooze_length: data['snoozeLength'] = snooze_length if snooze_count: data['snoozeCount'] = snooze_count return self.make_request(url, data=data, method="POST") # return def delete_alarm(self, device_id, alarm_id): """ https://dev.fitbit.com/docs/devices/#delete-alarm """ url = "{0}/{1}/user/-/devices/tracker/{device_id}/alarms/{alarm_id}.json".format( *self._get_common_args(), device_id=device_id, alarm_id=alarm_id ) return self.make_request(url, method="DELETE") def get_sleep(self, date): """ https://dev.fitbit.com/docs/sleep/#get-sleep-logs date should be a datetime.date object. """ url = "{0}/{1}/user/-/sleep/date/{year}-{month}-{day}.json".format( *self._get_common_args(), year=date.year, month=date.month, day=date.day ) return self.make_request(url) def log_sleep(self, start_time, duration): """ https://dev.fitbit.com/docs/sleep/#log-sleep start time should be a datetime object. We will be using the year, month, day, hour, and minute. """ data = { 'startTime': start_time.strftime("%H:%M"), 'duration': duration, 'date': start_time.strftime("%Y-%m-%d"), } url = "{0}/{1}/user/-/sleep.json".format(*self._get_common_args()) return self.make_request(url, data=data, method="POST") def activities_list(self): """ https://dev.fitbit.com/docs/activity/#browse-activity-types """ url = "{0}/{1}/activities.json".format(*self._get_common_args()) return self.make_request(url) def activity_detail(self, activity_id): """ https://dev.fitbit.com/docs/activity/#get-activity-type """ url = "{0}/{1}/activities/{activity_id}.json".format( *self._get_common_args(), activity_id=activity_id ) return self.make_request(url) def search_foods(self, query): """ https://dev.fitbit.com/docs/food-logging/#search-foods """ url = "{0}/{1}/foods/search.json?{encoded_query}".format( *self._get_common_args(), encoded_query=urlencode({'query': query}) ) return self.make_request(url) def food_detail(self, food_id): """ https://dev.fitbit.com/docs/food-logging/#get-food """ url = "{0}/{1}/foods/{food_id}.json".format( *self._get_common_args(), food_id=food_id ) return self.make_request(url) def food_units(self): """ https://dev.fitbit.com/docs/food-logging/#get-food-units """ url = "{0}/{1}/foods/units.json".format(*self._get_common_args()) return self.make_request(url) def get_bodyweight(self, base_date=None, user_id=None, period=None, end_date=None): """ https://dev.fitbit.com/docs/body/#get-weight-logs base_date should be a datetime.date object (defaults to today), period can be '1d', '7d', '30d', '1w', '1m', '3m', '6m', '1y', 'max' or None end_date should be a datetime.date object, or None. You can specify period or end_date, or neither, but not both. """ return self._get_body('weight', base_date, user_id, period, end_date) def get_bodyfat(self, base_date=None, user_id=None, period=None, end_date=None): """ https://dev.fitbit.com/docs/body/#get-body-fat-logs base_date should be a datetime.date object (defaults to today), period can be '1d', '7d', '30d', '1w', '1m', '3m', '6m', '1y', 'max' or None end_date should be a datetime.date object, or None. You can specify period or end_date, or neither, but not both. """ return self._get_body('fat', base_date, user_id, period, end_date) def _get_body(self, type_, base_date=None, user_id=None, period=None, end_date=None): if not base_date: base_date = datetime.date.today() if period and end_date: raise TypeError("Either end_date or period can be specified, not both") base_date_string = self._get_date_string(base_date) kwargs = {'type_': type_} base_url = "{0}/{1}/user/{2}/body/log/{type_}/date/{date_string}.json" if period: if not period in Fitbit.PERIODS: raise ValueError("Period must be one of %s" % ','.join(Fitbit.PERIODS)) kwargs['date_string'] = '/'.join([base_date_string, period]) elif end_date: end_string = self._get_date_string(end_date) kwargs['date_string'] = '/'.join([base_date_string, end_string]) else: kwargs['date_string'] = base_date_string url = base_url.format(*self._get_common_args(user_id), **kwargs) return self.make_request(url) def get_friends(self, user_id=None): """ https://dev.fitbit.com/docs/friends/#get-friends """ url = "{0}/{1}/user/{2}/friends.json".format(*self._get_common_args(user_id)) return self.make_request(url) def get_friends_leaderboard(self, period): """ https://dev.fitbit.com/docs/friends/#get-friends-leaderboard """ if not period in ['7d', '30d']: raise ValueError("Period must be one of '7d', '30d'") url = "{0}/{1}/user/-/friends/leaders/{period}.json".format( *self._get_common_args(), period=period ) return self.make_request(url) def invite_friend(self, data): """ https://dev.fitbit.com/docs/friends/#invite-friend """ url = "{0}/{1}/user/-/friends/invitations.json".format(*self._get_common_args()) return self.make_request(url, data=data) def invite_friend_by_email(self, email): """ Convenience Method for https://dev.fitbit.com/docs/friends/#invite-friend """ return self.invite_friend({'invitedUserEmail': email}) def invite_friend_by_userid(self, user_id): """ Convenience Method for https://dev.fitbit.com/docs/friends/#invite-friend """ return self.invite_friend({'invitedUserId': user_id}) def respond_to_invite(self, other_user_id, accept=True): """ https://dev.fitbit.com/docs/friends/#respond-to-friend-invitation """ url = "{0}/{1}/user/-/friends/invitations/{user_id}.json".format( *self._get_common_args(), user_id=other_user_id ) accept = 'true' if accept else 'false' return self.make_request(url, data={'accept': accept}) def accept_invite(self, other_user_id): """ Convenience method for respond_to_invite """ return self.respond_to_invite(other_user_id) def reject_invite(self, other_user_id): """ Convenience method for respond_to_invite """ return self.respond_to_invite(other_user_id, accept=False) def get_badges(self, user_id=None): """ https://dev.fitbit.com/docs/friends/#badges """ url = "{0}/{1}/user/{2}/badges.json".format(*self._get_common_args(user_id)) return self.make_request(url) def subscription(self, subscription_id, subscriber_id, collection=None, method='POST'): """ https://dev.fitbit.com/docs/subscriptions/ """ base_url = "{0}/{1}/user/-{collection}/apiSubscriptions/{end_string}.json" kwargs = {'collection': '', 'end_string': subscription_id} if collection: kwargs = { 'end_string': '-'.join([subscription_id, collection]), 'collection': '/' + collection } return self.make_request( base_url.format(*self._get_common_args(), **kwargs), method=method, headers={"X-Fitbit-Subscriber-id": subscriber_id} ) def list_subscriptions(self, collection=''): """ https://dev.fitbit.com/docs/subscriptions/#getting-a-list-of-subscriptions """ url = "{0}/{1}/user/-{collection}/apiSubscriptions.json".format( *self._get_common_args(), collection='/{0}'.format(collection) if collection else '' ) return self.make_request(url) python-fitbit-0.3.0/fitbit/compliance.py000066400000000000000000000016121304170412400202530ustar00rootroot00000000000000""" The Fitbit API breaks from the OAuth2 RFC standard by returning an "errors" object list, rather than a single "error" string. This puts hooks in place so that oauthlib can process an error in the results from access token and refresh token responses. This is necessary to prevent getting the generic red herring MissingTokenError. """ from json import loads, dumps from oauthlib.common import to_unicode def fitbit_compliance_fix(session): def _missing_error(r): token = loads(r.text) if 'errors' in token: # Set the error to the first one we have token['error'] = token['errors'][0]['errorType'] r._content = to_unicode(dumps(token)).encode('UTF-8') return r session.register_compliance_hook('access_token_response', _missing_error) session.register_compliance_hook('refresh_token_response', _missing_error) return session python-fitbit-0.3.0/fitbit/exceptions.py000066400000000000000000000041311304170412400203210ustar00rootroot00000000000000import json class BadResponse(Exception): """ Currently used if the response can't be json encoded, despite a .json extension """ pass class DeleteError(Exception): """ Used when a delete request did not return a 204 """ pass class Timeout(Exception): """ Used when a timeout occurs. """ pass class HTTPException(Exception): def __init__(self, response, *args, **kwargs): try: errors = json.loads(response.content.decode('utf8'))['errors'] message = '\n'.join([error['message'] for error in errors]) except Exception: if hasattr(response, 'status_code') and response.status_code == 401: message = response.content.decode('utf8') else: message = response super(HTTPException, self).__init__(message, *args, **kwargs) class HTTPBadRequest(HTTPException): """Generic >= 400 error """ pass class HTTPUnauthorized(HTTPException): """401 """ pass class HTTPForbidden(HTTPException): """403 """ pass class HTTPNotFound(HTTPException): """404 """ pass class HTTPConflict(HTTPException): """409 - returned when creating conflicting resources """ pass class HTTPTooManyRequests(HTTPException): """429 - returned when exceeding rate limits """ pass class HTTPServerError(HTTPException): """Generic >= 500 error """ pass def detect_and_raise_error(response): if response.status_code == 401: raise HTTPUnauthorized(response) elif response.status_code == 403: raise HTTPForbidden(response) elif response.status_code == 404: raise HTTPNotFound(response) elif response.status_code == 409: raise HTTPConflict(response) elif response.status_code == 429: exc = HTTPTooManyRequests(response) exc.retry_after_secs = int(response.headers['Retry-After']) raise exc elif response.status_code >= 500: raise HTTPServerError(response) elif response.status_code >= 400: raise HTTPBadRequest(response) python-fitbit-0.3.0/fitbit/utils.py000066400000000000000000000034361304170412400173070ustar00rootroot00000000000000""" Curry was copied from Django's implementation. License is reproduced here. Copyright (c) Django Software Foundation and individual contributors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of Django nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ def curry(_curried_func, *args, **kwargs): def _curried(*moreargs, **morekwargs): return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs)) return _curried python-fitbit-0.3.0/fitbit_tests/000077500000000000000000000000001304170412400170115ustar00rootroot00000000000000python-fitbit-0.3.0/fitbit_tests/__init__.py000066400000000000000000000015141304170412400211230ustar00rootroot00000000000000import unittest from .test_exceptions import ExceptionTest from .test_auth import Auth2Test from .test_api import ( APITest, CollectionResourceTest, DeleteCollectionResourceTest, ResourceAccessTest, SubscriptionsTest, PartnerAPITest ) def all_tests(consumer_key="", consumer_secret="", user_key=None, user_secret=None): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(ExceptionTest)) suite.addTest(unittest.makeSuite(Auth2Test)) suite.addTest(unittest.makeSuite(APITest)) suite.addTest(unittest.makeSuite(CollectionResourceTest)) suite.addTest(unittest.makeSuite(DeleteCollectionResourceTest)) suite.addTest(unittest.makeSuite(ResourceAccessTest)) suite.addTest(unittest.makeSuite(SubscriptionsTest)) suite.addTest(unittest.makeSuite(PartnerAPITest)) return suite python-fitbit-0.3.0/fitbit_tests/test_api.py000066400000000000000000001067651304170412400212120ustar00rootroot00000000000000from unittest import TestCase import datetime import mock import requests from fitbit import Fitbit from fitbit.exceptions import DeleteError, Timeout URLBASE = "%s/%s/user" % (Fitbit.API_ENDPOINT, Fitbit.API_VERSION) class TestBase(TestCase): def setUp(self): self.fb = Fitbit('x', 'y') def common_api_test(self, funcname, args, kwargs, expected_args, expected_kwargs): # Create a fitbit object, call the named function on it with the given # arguments and verify that make_request is called with the expected args and kwargs with mock.patch.object(self.fb, 'make_request') as make_request: retval = getattr(self.fb, funcname)(*args, **kwargs) mr_args, mr_kwargs = make_request.call_args self.assertEqual(expected_args, mr_args) self.assertEqual(expected_kwargs, mr_kwargs) def verify_raises(self, funcname, args, kwargs, exc): self.assertRaises(exc, getattr(self.fb, funcname), *args, **kwargs) class TimeoutTest(TestCase): def setUp(self): self.fb = Fitbit('x', 'y') self.fb_timeout = Fitbit('x', 'y', timeout=10) self.test_url = 'invalid://do.not.connect' def test_fb_without_timeout(self): with mock.patch.object(self.fb.client.session, 'request') as request: mock_response = mock.Mock() mock_response.status_code = 200 mock_response.content = b'{}' request.return_value = mock_response result = self.fb.make_request(self.test_url) request.assert_called_once() self.assertNotIn('timeout', request.call_args[1]) self.assertEqual({}, result) def test_fb_with_timeout__timing_out(self): with mock.patch.object(self.fb_timeout.client.session, 'request') as request: request.side_effect = requests.Timeout('Timed out') with self.assertRaisesRegexp(Timeout, 'Timed out'): self.fb_timeout.make_request(self.test_url) request.assert_called_once() self.assertEqual(10, request.call_args[1]['timeout']) def test_fb_with_timeout__not_timing_out(self): with mock.patch.object(self.fb_timeout.client.session, 'request') as request: mock_response = mock.Mock() mock_response.status_code = 200 mock_response.content = b'{}' request.return_value = mock_response result = self.fb_timeout.make_request(self.test_url) request.assert_called_once() self.assertEqual(10, request.call_args[1]['timeout']) self.assertEqual({}, result) class APITest(TestBase): """ Tests for python-fitbit API, not directly involved in getting authenticated """ def test_make_request(self): # If make_request returns a response with status 200, # we get back the json decoded value that was in the response.content ARGS = (1, 2) KWARGS = {'a': 3, 'b': 4, 'headers': {'Accept-Language': self.fb.system}} mock_response = mock.Mock() mock_response.status_code = 200 mock_response.content = b"1" with mock.patch.object(self.fb.client, 'make_request') as client_make_request: client_make_request.return_value = mock_response retval = self.fb.make_request(*ARGS, **KWARGS) self.assertEqual(1, client_make_request.call_count) self.assertEqual(1, retval) args, kwargs = client_make_request.call_args self.assertEqual(ARGS, args) self.assertEqual(KWARGS, kwargs) def test_make_request_202(self): # If make_request returns a response with status 202, # we get back True mock_response = mock.Mock() mock_response.status_code = 202 mock_response.content = "1" ARGS = (1, 2) KWARGS = {'a': 3, 'b': 4, 'Accept-Language': self.fb.system} with mock.patch.object(self.fb.client, 'make_request') as client_make_request: client_make_request.return_value = mock_response retval = self.fb.make_request(*ARGS, **KWARGS) self.assertEqual(True, retval) def test_make_request_delete_204(self): # If make_request returns a response with status 204, # and the method is DELETE, we get back True mock_response = mock.Mock() mock_response.status_code = 204 mock_response.content = "1" ARGS = (1, 2) KWARGS = {'a': 3, 'b': 4, 'method': 'DELETE', 'Accept-Language': self.fb.system} with mock.patch.object(self.fb.client, 'make_request') as client_make_request: client_make_request.return_value = mock_response retval = self.fb.make_request(*ARGS, **KWARGS) self.assertEqual(True, retval) def test_make_request_delete_not_204(self): # If make_request returns a response with status not 204, # and the method is DELETE, DeleteError is raised mock_response = mock.Mock() mock_response.status_code = 205 mock_response.content = "1" ARGS = (1, 2) KWARGS = {'a': 3, 'b': 4, 'method': 'DELETE', 'Accept-Language': self.fb.system} with mock.patch.object(self.fb.client, 'make_request') as client_make_request: client_make_request.return_value = mock_response self.assertRaises(DeleteError, self.fb.make_request, *ARGS, **KWARGS) class CollectionResourceTest(TestBase): """ Tests for _COLLECTION_RESOURCE """ def test_all_args(self): # If we pass all the optional args, the right things happen resource = "RESOURCE" date = datetime.date(1962, 1, 13) user_id = "bilbo" data = {'a': 1, 'b': 2} expected_data = data.copy() expected_data['date'] = date.strftime("%Y-%m-%d") url = URLBASE + "/%s/%s.json" % (user_id, resource) self.common_api_test('_COLLECTION_RESOURCE', (resource, date, user_id, data), {}, (url, expected_data), {}) def test_date_string(self): # date can be a "yyyy-mm-dd" string resource = "RESOURCE" date = "1962-1-13" user_id = "bilbo" data = {'a': 1, 'b': 2} expected_data = data.copy() expected_data['date'] = date url = URLBASE + "/%s/%s.json" % (user_id, resource) self.common_api_test('_COLLECTION_RESOURCE', (resource, date, user_id, data), {}, (url, expected_data), {}) def test_no_date(self): # If we omit the date, it uses today resource = "RESOURCE" user_id = "bilbo" data = {'a': 1, 'b': 2} expected_data = data.copy() expected_data['date'] = datetime.date.today().strftime("%Y-%m-%d") # expect today url = URLBASE + "/%s/%s.json" % (user_id, resource) self.common_api_test('_COLLECTION_RESOURCE', (resource, None, user_id, data), {}, (url, expected_data), {}) def test_no_userid(self): # If we omit the user_id, it uses "-" resource = "RESOURCE" date = datetime.date(1962, 1, 13) user_id = None data = {'a': 1, 'b': 2} expected_data = data.copy() expected_data['date'] = date.strftime("%Y-%m-%d") expected_user_id = "-" url = URLBASE + "/%s/%s.json" % (expected_user_id, resource) self.common_api_test( '_COLLECTION_RESOURCE', (resource, date, user_id, data), {}, (url, expected_data), {} ) def test_no_data(self): # If we omit the data arg, it does the right thing resource = "RESOURCE" date = datetime.date(1962, 1, 13) user_id = "bilbo" data = None url = URLBASE + "/%s/%s/date/%s.json" % (user_id, resource, date) self.common_api_test('_COLLECTION_RESOURCE', (resource, date, user_id, data), {}, (url, data), {}) def test_body(self): # Test the first method defined in __init__ to see if it calls # _COLLECTION_RESOURCE okay - if it does, they should all since # they're all built the same way # We need to mock _COLLECTION_RESOURCE before we create the Fitbit object, # since the __init__ is going to set up references to it with mock.patch('fitbit.api.Fitbit._COLLECTION_RESOURCE') as coll_resource: coll_resource.return_value = 999 fb = Fitbit('x', 'y') retval = fb.body(date=1, user_id=2, data=3) args, kwargs = coll_resource.call_args self.assertEqual(('body',), args) self.assertEqual({'date': 1, 'user_id': 2, 'data': 3}, kwargs) self.assertEqual(999, retval) class DeleteCollectionResourceTest(TestBase): """Tests for _DELETE_COLLECTION_RESOURCE""" def test_impl(self): # _DELETE_COLLECTION_RESOURCE calls make_request with the right args resource = "RESOURCE" log_id = "Foo" url = URLBASE + "/-/%s/%s.json" % (resource, log_id) self.common_api_test( '_DELETE_COLLECTION_RESOURCE', (resource, log_id), {}, (url,), {"method": "DELETE"} ) def test_cant_delete_body(self): self.assertFalse(hasattr(self.fb, 'delete_body')) def test_delete_foods_log(self): log_id = "fake_log_id" # We need to mock _DELETE_COLLECTION_RESOURCE before we create the Fitbit object, # since the __init__ is going to set up references to it with mock.patch('fitbit.api.Fitbit._DELETE_COLLECTION_RESOURCE') as delete_resource: delete_resource.return_value = 999 fb = Fitbit('x', 'y') retval = fb.delete_foods_log(log_id=log_id) args, kwargs = delete_resource.call_args self.assertEqual(('foods/log',), args) self.assertEqual({'log_id': log_id}, kwargs) self.assertEqual(999, retval) def test_delete_foods_log_water(self): log_id = "OmarKhayyam" # We need to mock _DELETE_COLLECTION_RESOURCE before we create the Fitbit object, # since the __init__ is going to set up references to it with mock.patch('fitbit.api.Fitbit._DELETE_COLLECTION_RESOURCE') as delete_resource: delete_resource.return_value = 999 fb = Fitbit('x', 'y') retval = fb.delete_foods_log_water(log_id=log_id) args, kwargs = delete_resource.call_args self.assertEqual(('foods/log/water',), args) self.assertEqual({'log_id': log_id}, kwargs) self.assertEqual(999, retval) class ResourceAccessTest(TestBase): """ Class for testing the Fitbit Resource Access API: https://dev.fitbit.com/docs/ """ def test_user_profile_get(self): """ Test getting a user profile. https://dev.fitbit.com/docs/user/ Tests the following HTTP method/URLs: GET https://api.fitbit.com/1/user/FOO/profile.json GET https://api.fitbit.com/1/user/-/profile.json """ user_id = "FOO" url = URLBASE + "/%s/profile.json" % user_id self.common_api_test('user_profile_get', (user_id,), {}, (url,), {}) url = URLBASE + "/-/profile.json" self.common_api_test('user_profile_get', (), {}, (url,), {}) def test_user_profile_update(self): """ Test updating a user profile. https://dev.fitbit.com/docs/user/#update-profile Tests the following HTTP method/URLs: POST https://api.fitbit.com/1/user/-/profile.json """ data = "BAR" url = URLBASE + "/-/profile.json" self.common_api_test('user_profile_update', (data,), {}, (url, data), {}) def test_recent_activities(self): user_id = "LukeSkywalker" with mock.patch('fitbit.api.Fitbit.activity_stats') as act_stats: fb = Fitbit('x', 'y') retval = fb.recent_activities(user_id=user_id) args, kwargs = act_stats.call_args self.assertEqual((), args) self.assertEqual({'user_id': user_id, 'qualifier': 'recent'}, kwargs) def test_activity_stats(self): user_id = "O B 1 Kenobi" qualifier = "frequent" url = URLBASE + "/%s/activities/%s.json" % (user_id, qualifier) self.common_api_test('activity_stats', (), dict(user_id=user_id, qualifier=qualifier), (url,), {}) def test_activity_stats_no_qualifier(self): user_id = "O B 1 Kenobi" qualifier = None self.common_api_test('activity_stats', (), dict(user_id=user_id, qualifier=qualifier), (URLBASE + "/%s/activities.json" % user_id,), {}) def test_body_fat_goal(self): self.common_api_test( 'body_fat_goal', (), dict(), (URLBASE + '/-/body/log/fat/goal.json',), {'data': {}}) self.common_api_test( 'body_fat_goal', (), dict(fat=10), (URLBASE + '/-/body/log/fat/goal.json',), {'data': {'fat': 10}}) def test_body_weight_goal(self): self.common_api_test( 'body_weight_goal', (), dict(), (URLBASE + '/-/body/log/weight/goal.json',), {'data': {}}) self.common_api_test( 'body_weight_goal', (), dict(start_date='2015-04-01', start_weight=180), (URLBASE + '/-/body/log/weight/goal.json',), {'data': {'startDate': '2015-04-01', 'startWeight': 180}}) self.verify_raises('body_weight_goal', (), {'start_date': '2015-04-01'}, ValueError) self.verify_raises('body_weight_goal', (), {'start_weight': 180}, ValueError) def test_activities_daily_goal(self): self.common_api_test( 'activities_daily_goal', (), dict(), (URLBASE + '/-/activities/goals/daily.json',), {'data': {}}) self.common_api_test( 'activities_daily_goal', (), dict(steps=10000), (URLBASE + '/-/activities/goals/daily.json',), {'data': {'steps': 10000}}) self.common_api_test( 'activities_daily_goal', (), dict(calories_out=3107, active_minutes=30, floors=10, distance=5, steps=10000), (URLBASE + '/-/activities/goals/daily.json',), {'data': {'caloriesOut': 3107, 'activeMinutes': 30, 'floors': 10, 'distance': 5, 'steps': 10000}}) def test_activities_weekly_goal(self): self.common_api_test( 'activities_weekly_goal', (), dict(), (URLBASE + '/-/activities/goals/weekly.json',), {'data': {}}) self.common_api_test( 'activities_weekly_goal', (), dict(steps=10000), (URLBASE + '/-/activities/goals/weekly.json',), {'data': {'steps': 10000}}) self.common_api_test( 'activities_weekly_goal', (), dict(floors=10, distance=5, steps=10000), (URLBASE + '/-/activities/goals/weekly.json',), {'data': {'floors': 10, 'distance': 5, 'steps': 10000}}) def test_food_goal(self): self.common_api_test( 'food_goal', (), dict(), (URLBASE + '/-/foods/log/goal.json',), {'data': {}}) self.common_api_test( 'food_goal', (), dict(calories=2300), (URLBASE + '/-/foods/log/goal.json',), {'data': {'calories': 2300}}) self.common_api_test( 'food_goal', (), dict(intensity='EASIER', personalized=True), (URLBASE + '/-/foods/log/goal.json',), {'data': {'intensity': 'EASIER', 'personalized': True}}) self.verify_raises('food_goal', (), {'personalized': True}, ValueError) def test_water_goal(self): self.common_api_test( 'water_goal', (), dict(), (URLBASE + '/-/foods/log/water/goal.json',), {'data': {}}) self.common_api_test( 'water_goal', (), dict(target=63), (URLBASE + '/-/foods/log/water/goal.json',), {'data': {'target': 63}}) def test_timeseries(self): resource = 'FOO' user_id = 'BAR' base_date = '1992-05-12' period = '1d' end_date = '1998-12-31' # Not allowed to specify both period and end date self.assertRaises( TypeError, self.fb.time_series, resource, user_id, base_date, period, end_date) # Period must be valid self.assertRaises( ValueError, self.fb.time_series, resource, user_id, base_date, period="xyz", end_date=None) def test_timeseries(fb, resource, user_id, base_date, period, end_date, expected_url): with mock.patch.object(fb, 'make_request') as make_request: retval = fb.time_series(resource, user_id, base_date, period, end_date) args, kwargs = make_request.call_args self.assertEqual((expected_url,), args) # User_id defaults = "-" test_timeseries(self.fb, resource, user_id=None, base_date=base_date, period=period, end_date=None, expected_url=URLBASE + "/-/FOO/date/1992-05-12/1d.json") # end_date can be a date object test_timeseries(self.fb, resource, user_id=user_id, base_date=base_date, period=None, end_date=datetime.date(1998, 12, 31), expected_url=URLBASE + "/BAR/FOO/date/1992-05-12/1998-12-31.json") # base_date can be a date object test_timeseries(self.fb, resource, user_id=user_id, base_date=datetime.date(1992,5,12), period=None, end_date=end_date, expected_url=URLBASE + "/BAR/FOO/date/1992-05-12/1998-12-31.json") def test_sleep(self): today = datetime.date.today().strftime('%Y-%m-%d') self.common_api_test('sleep', (today,), {}, ("%s/-/sleep/date/%s.json" % (URLBASE, today), None), {}) self.common_api_test('sleep', (today, "USER_ID"), {}, ("%s/USER_ID/sleep/date/%s.json" % (URLBASE, today), None), {}) def test_foods(self): today = datetime.date.today().strftime('%Y-%m-%d') self.common_api_test('recent_foods', ("USER_ID",), {}, (URLBASE+"/USER_ID/foods/log/recent.json",), {}) self.common_api_test('favorite_foods', ("USER_ID",), {}, (URLBASE+"/USER_ID/foods/log/favorite.json",), {}) self.common_api_test('frequent_foods', ("USER_ID",), {}, (URLBASE+"/USER_ID/foods/log/frequent.json",), {}) self.common_api_test('foods_log', (today, "USER_ID",), {}, ("%s/USER_ID/foods/log/date/%s.json" % (URLBASE, today), None), {}) self.common_api_test('recent_foods', (), {}, (URLBASE+"/-/foods/log/recent.json",), {}) self.common_api_test('favorite_foods', (), {}, (URLBASE+"/-/foods/log/favorite.json",), {}) self.common_api_test('frequent_foods', (), {}, (URLBASE+"/-/foods/log/frequent.json",), {}) self.common_api_test('foods_log', (today,), {}, ("%s/-/foods/log/date/%s.json" % (URLBASE, today), None), {}) url = URLBASE + "/-/foods/log/favorite/food_id.json" self.common_api_test('add_favorite_food', ('food_id',), {}, (url,), {'method': 'POST'}) self.common_api_test('delete_favorite_food', ('food_id',), {}, (url,), {'method': 'DELETE'}) url = URLBASE + "/-/foods.json" self.common_api_test('create_food', (), {'data': 'FOO'}, (url,), {'data': 'FOO'}) url = URLBASE + "/-/meals.json" self.common_api_test('get_meals', (), {}, (url,), {}) url = "%s/%s/foods/search.json?query=FOOBAR" % (Fitbit.API_ENDPOINT, Fitbit.API_VERSION) self.common_api_test('search_foods', ("FOOBAR",), {}, (url,), {}) url = "%s/%s/foods/FOOBAR.json" % (Fitbit.API_ENDPOINT, Fitbit.API_VERSION) self.common_api_test('food_detail', ("FOOBAR",), {}, (url,), {}) url = "%s/%s/foods/units.json" % (Fitbit.API_ENDPOINT, Fitbit.API_VERSION) self.common_api_test('food_units', (), {}, (url,), {}) def test_devices(self): url = URLBASE + "/-/devices.json" self.common_api_test('get_devices', (), {}, (url,), {}) def test_badges(self): url = URLBASE + "/-/badges.json" self.common_api_test('get_badges', (), {}, (url,), {}) def test_activities(self): """ Test the getting/creating/deleting various activity related items. Tests the following HTTP method/URLs: GET https://api.fitbit.com/1/activities.json POST https://api.fitbit.com/1/user/-/activities.json GET https://api.fitbit.com/1/activities/FOOBAR.json POST https://api.fitbit.com/1/user/-/activities/favorite/activity_id.json DELETE https://api.fitbit.com/1/user/-/activities/favorite/activity_id.json """ url = "%s/%s/activities.json" % (Fitbit.API_ENDPOINT, Fitbit.API_VERSION) self.common_api_test('activities_list', (), {}, (url,), {}) url = "%s/%s/user/-/activities.json" % (Fitbit.API_ENDPOINT, Fitbit.API_VERSION) self.common_api_test('log_activity', (), {'data' : 'FOO'}, (url,), {'data' : 'FOO'} ) url = "%s/%s/activities/FOOBAR.json" % (Fitbit.API_ENDPOINT, Fitbit.API_VERSION) self.common_api_test('activity_detail', ("FOOBAR",), {}, (url,), {}) url = URLBASE + "/-/activities/favorite/activity_id.json" self.common_api_test('add_favorite_activity', ('activity_id',), {}, (url,), {'method': 'POST'}) self.common_api_test('delete_favorite_activity', ('activity_id',), {}, (url,), {'method': 'DELETE'}) def _test_get_bodyweight(self, base_date=None, user_id=None, period=None, end_date=None, expected_url=None): """ Helper method for testing retrieving body weight measurements """ with mock.patch.object(self.fb, 'make_request') as make_request: self.fb.get_bodyweight(base_date, user_id=user_id, period=period, end_date=end_date) args, kwargs = make_request.call_args self.assertEqual((expected_url,), args) def test_bodyweight(self): """ Tests for retrieving body weight measurements. https://dev.fitbit.com/docs/body/#get-weight-logs Tests the following methods/URLs: GET https://api.fitbit.com/1/user/-/body/log/weight/date/1992-05-12.json GET https://api.fitbit.com/1/user/BAR/body/log/weight/date/1992-05-12/1998-12-31.json GET https://api.fitbit.com/1/user/BAR/body/log/weight/date/1992-05-12/1d.json GET https://api.fitbit.com/1/user/-/body/log/weight/date/2015-02-26.json """ user_id = 'BAR' # No end_date or period self._test_get_bodyweight( base_date=datetime.date(1992, 5, 12), user_id=None, period=None, end_date=None, expected_url=URLBASE + "/-/body/log/weight/date/1992-05-12.json") # With end_date self._test_get_bodyweight( base_date=datetime.date(1992, 5, 12), user_id=user_id, period=None, end_date=datetime.date(1998, 12, 31), expected_url=URLBASE + "/BAR/body/log/weight/date/1992-05-12/1998-12-31.json") # With period self._test_get_bodyweight( base_date=datetime.date(1992, 5, 12), user_id=user_id, period="1d", end_date=None, expected_url=URLBASE + "/BAR/body/log/weight/date/1992-05-12/1d.json") # Date defaults to today today = datetime.date.today().strftime('%Y-%m-%d') self._test_get_bodyweight( base_date=None, user_id=None, period=None, end_date=None, expected_url=URLBASE + "/-/body/log/weight/date/%s.json" % today) def _test_get_bodyfat(self, base_date=None, user_id=None, period=None, end_date=None, expected_url=None): """ Helper method for testing getting bodyfat measurements """ with mock.patch.object(self.fb, 'make_request') as make_request: self.fb.get_bodyfat(base_date, user_id=user_id, period=period, end_date=end_date) args, kwargs = make_request.call_args self.assertEqual((expected_url,), args) def test_bodyfat(self): """ Tests for retrieving bodyfat measurements. https://dev.fitbit.com/docs/body/#get-body-fat-logs Tests the following methods/URLs: GET https://api.fitbit.com/1/user/-/body/log/fat/date/1992-05-12.json GET https://api.fitbit.com/1/user/BAR/body/log/fat/date/1992-05-12/1998-12-31.json GET https://api.fitbit.com/1/user/BAR/body/log/fat/date/1992-05-12/1d.json GET https://api.fitbit.com/1/user/-/body/log/fat/date/2015-02-26.json """ user_id = 'BAR' # No end_date or period self._test_get_bodyfat( base_date=datetime.date(1992, 5, 12), user_id=None, period=None, end_date=None, expected_url=URLBASE + "/-/body/log/fat/date/1992-05-12.json") # With end_date self._test_get_bodyfat( base_date=datetime.date(1992, 5, 12), user_id=user_id, period=None, end_date=datetime.date(1998, 12, 31), expected_url=URLBASE + "/BAR/body/log/fat/date/1992-05-12/1998-12-31.json") # With period self._test_get_bodyfat( base_date=datetime.date(1992, 5, 12), user_id=user_id, period="1d", end_date=None, expected_url=URLBASE + "/BAR/body/log/fat/date/1992-05-12/1d.json") # Date defaults to today today = datetime.date.today().strftime('%Y-%m-%d') self._test_get_bodyfat( base_date=None, user_id=None, period=None, end_date=None, expected_url=URLBASE + "/-/body/log/fat/date/%s.json" % today) def test_friends(self): url = URLBASE + "/-/friends.json" self.common_api_test('get_friends', (), {}, (url,), {}) url = URLBASE + "/FOOBAR/friends.json" self.common_api_test('get_friends', ("FOOBAR",), {}, (url,), {}) url = URLBASE + "/-/friends/leaders/7d.json" self.common_api_test('get_friends_leaderboard', ("7d",), {}, (url,), {}) url = URLBASE + "/-/friends/leaders/30d.json" self.common_api_test('get_friends_leaderboard', ("30d",), {}, (url,), {}) self.verify_raises('get_friends_leaderboard', ("xd",), {}, ValueError) def test_invitations(self): url = URLBASE + "/-/friends/invitations.json" self.common_api_test('invite_friend', ("FOO",), {}, (url,), {'data': "FOO"}) self.common_api_test('invite_friend_by_email', ("foo@bar",), {}, (url,), {'data':{'invitedUserEmail': "foo@bar"}}) self.common_api_test('invite_friend_by_userid', ("foo@bar",), {}, (url,), {'data':{'invitedUserId': "foo@bar"}}) url = URLBASE + "/-/friends/invitations/FOO.json" self.common_api_test('respond_to_invite', ("FOO", True), {}, (url,), {'data':{'accept': "true"}}) self.common_api_test('respond_to_invite', ("FOO", False), {}, (url,), {'data':{'accept': "false"}}) self.common_api_test('respond_to_invite', ("FOO", ), {}, (url,), {'data':{'accept': "true"}}) self.common_api_test('accept_invite', ("FOO",), {}, (url,), {'data':{'accept': "true"}}) self.common_api_test('reject_invite', ("FOO", ), {}, (url,), {'data':{'accept': "false"}}) def test_alarms(self): url = "%s/-/devices/tracker/%s/alarms.json" % (URLBASE, 'FOO') self.common_api_test('get_alarms', (), {'device_id': 'FOO'}, (url,), {}) url = "%s/-/devices/tracker/%s/alarms/%s.json" % (URLBASE, 'FOO', 'BAR') self.common_api_test('delete_alarm', (), {'device_id': 'FOO', 'alarm_id': 'BAR'}, (url,), {'method': 'DELETE'}) url = "%s/-/devices/tracker/%s/alarms.json" % (URLBASE, 'FOO') self.common_api_test('add_alarm', (), {'device_id': 'FOO', 'alarm_time': datetime.datetime(year=2013, month=11, day=13, hour=8, minute=16), 'week_days': ['MONDAY'] }, (url,), {'data': {'enabled': True, 'recurring': False, 'time': datetime.datetime(year=2013, month=11, day=13, hour=8, minute=16).strftime("%H:%M%z"), 'vibe': 'DEFAULT', 'weekDays': ['MONDAY'], }, 'method': 'POST' } ) self.common_api_test('add_alarm', (), {'device_id': 'FOO', 'alarm_time': datetime.datetime(year=2013, month=11, day=13, hour=8, minute=16), 'week_days': ['MONDAY'], 'recurring': True, 'enabled': False, 'label': 'ugh', 'snooze_length': 5, 'snooze_count': 5 }, (url,), {'data': {'enabled': False, 'recurring': True, 'label': 'ugh', 'snoozeLength': 5, 'snoozeCount': 5, 'time': datetime.datetime(year=2013, month=11, day=13, hour=8, minute=16).strftime("%H:%M%z"), 'vibe': 'DEFAULT', 'weekDays': ['MONDAY'], }, 'method': 'POST'} ) url = "%s/-/devices/tracker/%s/alarms/%s.json" % (URLBASE, 'FOO', 'BAR') self.common_api_test('update_alarm', (), {'device_id': 'FOO', 'alarm_id': 'BAR', 'alarm_time': datetime.datetime(year=2013, month=11, day=13, hour=8, minute=16), 'week_days': ['MONDAY'], 'recurring': True, 'enabled': False, 'label': 'ugh', 'snooze_length': 5, 'snooze_count': 5 }, (url,), {'data': {'enabled': False, 'recurring': True, 'label': 'ugh', 'snoozeLength': 5, 'snoozeCount': 5, 'time': datetime.datetime(year=2013, month=11, day=13, hour=8, minute=16).strftime("%H:%M%z"), 'vibe': 'DEFAULT', 'weekDays': ['MONDAY'], }, 'method': 'POST'} ) class SubscriptionsTest(TestBase): """ Class for testing the Fitbit Subscriptions API: https://dev.fitbit.com/docs/subscriptions/ """ def test_subscriptions(self): """ Subscriptions tests. Tests the following methods/URLs: GET https://api.fitbit.com/1/user/-/apiSubscriptions.json GET https://api.fitbit.com/1/user/-/FOO/apiSubscriptions.json POST https://api.fitbit.com/1/user/-/apiSubscriptions/SUBSCRIPTION_ID.json POST https://api.fitbit.com/1/user/-/apiSubscriptions/SUBSCRIPTION_ID.json POST https://api.fitbit.com/1/user/-/COLLECTION/apiSubscriptions/SUBSCRIPTION_ID-COLLECTION.json """ url = URLBASE + "/-/apiSubscriptions.json" self.common_api_test('list_subscriptions', (), {}, (url,), {}) url = URLBASE + "/-/FOO/apiSubscriptions.json" self.common_api_test('list_subscriptions', ("FOO",), {}, (url,), {}) url = URLBASE + "/-/apiSubscriptions/SUBSCRIPTION_ID.json" self.common_api_test('subscription', ("SUBSCRIPTION_ID", "SUBSCRIBER_ID"), {}, (url,), {'method': 'POST', 'headers': {'X-Fitbit-Subscriber-id': "SUBSCRIBER_ID"}}) self.common_api_test('subscription', ("SUBSCRIPTION_ID", "SUBSCRIBER_ID"), {'method': 'THROW'}, (url,), {'method': 'THROW', 'headers': {'X-Fitbit-Subscriber-id': "SUBSCRIBER_ID"}}) url = URLBASE + "/-/COLLECTION/apiSubscriptions/SUBSCRIPTION_ID-COLLECTION.json" self.common_api_test('subscription', ("SUBSCRIPTION_ID", "SUBSCRIBER_ID"), {'method': 'THROW', 'collection': "COLLECTION"}, (url,), {'method': 'THROW', 'headers': {'X-Fitbit-Subscriber-id': "SUBSCRIBER_ID"}}) class PartnerAPITest(TestBase): """ Class for testing the Fitbit Partner API: https://dev.fitbit.com/docs/ """ def _test_intraday_timeseries(self, resource, base_date, detail_level, start_time, end_time, expected_url): """ Helper method for intraday timeseries tests """ with mock.patch.object(self.fb, 'make_request') as make_request: retval = self.fb.intraday_time_series( resource, base_date, detail_level, start_time, end_time) args, kwargs = make_request.call_args self.assertEqual((expected_url,), args) def test_intraday_timeseries(self): """ Intraday Time Series tests: https://dev.fitbit.com/docs/activity/#get-activity-intraday-time-series Tests the following methods/URLs: GET https://api.fitbit.com/1/user/-/FOO/date/1918-05-11/1d/1min.json GET https://api.fitbit.com/1/user/-/FOO/date/1918-05-11/1d/1min.json GET https://api.fitbit.com/1/user/-/FOO/date/1918-05-11/1d/1min/time/03:56/15:07.json GET https://api.fitbit.com/1/user/-/FOO/date/1918-05-11/1d/1min/time/3:56/15:07.json """ resource = 'FOO' base_date = '1918-05-11' # detail_level must be valid self.assertRaises( ValueError, self.fb.intraday_time_series, resource, base_date, detail_level="xyz", start_time=None, end_time=None) # provide end_time if start_time provided self.assertRaises( TypeError, self.fb.intraday_time_series, resource, base_date, detail_level="1min", start_time='12:55', end_time=None) self.assertRaises( TypeError, self.fb.intraday_time_series, resource, base_date, detail_level="1min", start_time='12:55', end_time='') # provide start_time if end_time provided self.assertRaises( TypeError, self.fb.intraday_time_series, resource, base_date, detail_level="1min", start_time=None, end_time='12:55') self.assertRaises( TypeError, self.fb.intraday_time_series, resource, base_date, detail_level="1min", start_time='', end_time='12:55') # Default self._test_intraday_timeseries( resource, base_date=base_date, detail_level='1min', start_time=None, end_time=None, expected_url=URLBASE + "/-/FOO/date/1918-05-11/1d/1min.json") # start_date can be a date object self._test_intraday_timeseries( resource, base_date=datetime.date(1918, 5, 11), detail_level='1min', start_time=None, end_time=None, expected_url=URLBASE + "/-/FOO/date/1918-05-11/1d/1min.json") # start_time can be a datetime object self._test_intraday_timeseries( resource, base_date=base_date, detail_level='1min', start_time=datetime.time(3, 56), end_time='15:07', expected_url=URLBASE + "/-/FOO/date/1918-05-11/1d/1min/time/03:56/15:07.json") # end_time can be a datetime object self._test_intraday_timeseries( resource, base_date=base_date, detail_level='1min', start_time='3:56', end_time=datetime.time(15, 7), expected_url=URLBASE + "/-/FOO/date/1918-05-11/1d/1min/time/3:56/15:07.json") # start_time can be a midnight datetime object self._test_intraday_timeseries( resource, base_date=base_date, detail_level='1min', start_time=datetime.time(0, 0), end_time=datetime.time(15, 7), expected_url=URLBASE + "/-/FOO/date/1918-05-11/1d/1min/time/00:00/15:07.json") # end_time can be a midnight datetime object self._test_intraday_timeseries( resource, base_date=base_date, detail_level='1min', start_time=datetime.time(3, 56), end_time=datetime.time(0, 0), expected_url=URLBASE + "/-/FOO/date/1918-05-11/1d/1min/time/03:56/00:00.json") # start_time and end_time can be a midnight datetime object self._test_intraday_timeseries( resource, base_date=base_date, detail_level='1min', start_time=datetime.time(0, 0), end_time=datetime.time(0, 0), expected_url=URLBASE + "/-/FOO/date/1918-05-11/1d/1min/time/00:00/00:00.json") python-fitbit-0.3.0/fitbit_tests/test_auth.py000066400000000000000000000170221304170412400213650ustar00rootroot00000000000000import copy import json import mock import requests_mock from datetime import datetime from freezegun import freeze_time from oauthlib.oauth2.rfc6749.errors import InvalidGrantError from requests.auth import _basic_auth_str from unittest import TestCase from fitbit import Fitbit class Auth2Test(TestCase): """Add tests for auth part of API mock the oauth library calls to simulate various responses, make sure we call the right oauth calls, respond correctly based on the responses """ client_kwargs = { 'client_id': 'fake_id', 'client_secret': 'fake_secret', 'redirect_uri': 'http://127.0.0.1:8080', 'scope': ['fake_scope1'] } def test_authorize_token_url(self): # authorize_token_url calls oauth and returns a URL fb = Fitbit(**self.client_kwargs) retval = fb.client.authorize_token_url() self.assertEqual(retval[0], 'https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=fake_id&redirect_uri=http%3A%2F%2F127.0.0.1%3A8080&scope=activity+nutrition+heartrate+location+nutrition+profile+settings+sleep+social+weight&state='+retval[1]) def test_authorize_token_url_with_scope(self): # authorize_token_url calls oauth and returns a URL fb = Fitbit(**self.client_kwargs) retval = fb.client.authorize_token_url(scope=self.client_kwargs['scope']) self.assertEqual(retval[0], 'https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=fake_id&redirect_uri=http%3A%2F%2F127.0.0.1%3A8080&scope='+ str(self.client_kwargs['scope'][0])+ '&state='+retval[1]) def test_fetch_access_token(self): # tests the fetching of access token using code and redirect_URL fb = Fitbit(**self.client_kwargs) fake_code = "fake_code" with requests_mock.mock() as m: m.post(fb.client.access_token_url, text=json.dumps({ 'access_token': 'fake_return_access_token', 'refresh_token': 'fake_return_refresh_token' })) retval = fb.client.fetch_access_token(fake_code) self.assertEqual("fake_return_access_token", retval['access_token']) self.assertEqual("fake_return_refresh_token", retval['refresh_token']) def test_refresh_token(self): # test of refresh function kwargs = copy.copy(self.client_kwargs) kwargs['access_token'] = 'fake_access_token' kwargs['refresh_token'] = 'fake_refresh_token' kwargs['refresh_cb'] = lambda x: None fb = Fitbit(**kwargs) with requests_mock.mock() as m: m.post(fb.client.refresh_token_url, text=json.dumps({ 'access_token': 'fake_return_access_token', 'refresh_token': 'fake_return_refresh_token' })) retval = fb.client.refresh_token() self.assertEqual("fake_return_access_token", retval['access_token']) self.assertEqual("fake_return_refresh_token", retval['refresh_token']) @freeze_time(datetime.fromtimestamp(1483563319)) def test_auto_refresh_expires_at(self): """Test of auto_refresh with expired token""" # 1. first call to _request causes a HTTPUnauthorized # 2. the token_refresh call is faked # 3. the second call to _request returns a valid value refresh_cb = mock.MagicMock() kwargs = copy.copy(self.client_kwargs) kwargs.update({ 'access_token': 'fake_access_token', 'refresh_token': 'fake_refresh_token', 'expires_at': 1483530000, 'refresh_cb': refresh_cb, }) fb = Fitbit(**kwargs) profile_url = Fitbit.API_ENDPOINT + '/1/user/-/profile.json' with requests_mock.mock() as m: m.get( profile_url, text='{"user":{"aboutMe": "python-fitbit developer"}}', status_code=200 ) token = { 'access_token': 'fake_return_access_token', 'refresh_token': 'fake_return_refresh_token', 'expires_at': 1483570000, } m.post(fb.client.refresh_token_url, text=json.dumps(token)) retval = fb.make_request(profile_url) self.assertEqual(m.request_history[0].path, '/oauth2/token') self.assertEqual( m.request_history[0].headers['Authorization'], _basic_auth_str( self.client_kwargs['client_id'], self.client_kwargs['client_secret'] ) ) self.assertEqual(retval['user']['aboutMe'], "python-fitbit developer") self.assertEqual("fake_return_access_token", token['access_token']) self.assertEqual("fake_return_refresh_token", token['refresh_token']) refresh_cb.assert_called_once_with(token) def test_auto_refresh_token_exception(self): """Test of auto_refresh with Unauthorized exception""" # 1. first call to _request causes a HTTPUnauthorized # 2. the token_refresh call is faked # 3. the second call to _request returns a valid value refresh_cb = mock.MagicMock() kwargs = copy.copy(self.client_kwargs) kwargs.update({ 'access_token': 'fake_access_token', 'refresh_token': 'fake_refresh_token', 'refresh_cb': refresh_cb, }) fb = Fitbit(**kwargs) profile_url = Fitbit.API_ENDPOINT + '/1/user/-/profile.json' with requests_mock.mock() as m: m.get(profile_url, [{ 'text': json.dumps({ "errors": [{ "errorType": "expired_token", "message": "Access token expired:" }] }), 'status_code': 401 }, { 'text': '{"user":{"aboutMe": "python-fitbit developer"}}', 'status_code': 200 }]) token = { 'access_token': 'fake_return_access_token', 'refresh_token': 'fake_return_refresh_token' } m.post(fb.client.refresh_token_url, text=json.dumps(token)) retval = fb.make_request(profile_url) self.assertEqual(m.request_history[1].path, '/oauth2/token') self.assertEqual( m.request_history[1].headers['Authorization'], _basic_auth_str( self.client_kwargs['client_id'], self.client_kwargs['client_secret'] ) ) self.assertEqual(retval['user']['aboutMe'], "python-fitbit developer") self.assertEqual("fake_return_access_token", token['access_token']) self.assertEqual("fake_return_refresh_token", token['refresh_token']) refresh_cb.assert_called_once_with(token) def test_auto_refresh_error(self): """Test of auto_refresh with expired refresh token""" refresh_cb = mock.MagicMock() kwargs = copy.copy(self.client_kwargs) kwargs.update({ 'access_token': 'fake_access_token', 'refresh_token': 'fake_refresh_token', 'refresh_cb': refresh_cb, }) fb = Fitbit(**kwargs) with requests_mock.mock() as m: response = { "errors": [{"errorType": "invalid_grant"}], "success": False } m.post(fb.client.refresh_token_url, text=json.dumps(response)) self.assertRaises(InvalidGrantError, fb.client.refresh_token) class fake_response(object): def __init__(self, code, text): self.status_code = code self.text = text self.content = text python-fitbit-0.3.0/fitbit_tests/test_exceptions.py000066400000000000000000000100201304170412400225740ustar00rootroot00000000000000import unittest import json import mock import requests import sys from fitbit import Fitbit from fitbit import exceptions class ExceptionTest(unittest.TestCase): """ Tests that certain response codes raise certain exceptions """ client_kwargs = { "client_id": "", "client_secret": "", "access_token": None, "refresh_token": None } def test_response_ok(self): """ This mocks a pretty normal resource, that the request was authenticated, and data was returned. This test should just run and not raise any exceptions """ r = mock.Mock(spec=requests.Response) r.status_code = 200 r.content = b'{"normal": "resource"}' f = Fitbit(**self.client_kwargs) f.client._request = lambda *args, **kwargs: r f.user_profile_get() r.status_code = 202 f.user_profile_get() r.status_code = 204 f.user_profile_get() def test_response_auth(self): """ This test checks how the client handles different auth responses, and the exceptions raised by the client. """ r = mock.Mock(spec=requests.Response) r.status_code = 401 json_response = { "errors": [{ "errorType": "unauthorized", "message": "Unknown auth error"} ], "normal": "resource" } r.content = json.dumps(json_response).encode('utf8') f = Fitbit(**self.client_kwargs) f.client._request = lambda *args, **kwargs: r self.assertRaises(exceptions.HTTPUnauthorized, f.user_profile_get) r.status_code = 403 json_response['errors'][0].update({ "errorType": "forbidden", "message": "Forbidden" }) r.content = json.dumps(json_response).encode('utf8') self.assertRaises(exceptions.HTTPForbidden, f.user_profile_get) def test_response_error(self): """ Tests other HTTP errors """ r = mock.Mock(spec=requests.Response) r.content = b'{"normal": "resource"}' self.client_kwargs['oauth2'] = True f = Fitbit(**self.client_kwargs) f.client._request = lambda *args, **kwargs: r r.status_code = 404 self.assertRaises(exceptions.HTTPNotFound, f.user_profile_get) r.status_code = 409 self.assertRaises(exceptions.HTTPConflict, f.user_profile_get) r.status_code = 500 self.assertRaises(exceptions.HTTPServerError, f.user_profile_get) r.status_code = 499 self.assertRaises(exceptions.HTTPBadRequest, f.user_profile_get) def test_too_many_requests(self): """ Tests the 429 response, given in case of exceeding the rate limit """ r = mock.Mock(spec=requests.Response) r.content = b"{'normal': 'resource'}" r.headers = {'Retry-After': '10'} f = Fitbit(**self.client_kwargs) f.client._request = lambda *args, **kwargs: r r.status_code = 429 try: f.user_profile_get() self.assertEqual(True, False) # Won't run if an exception's raised except exceptions.HTTPTooManyRequests: e = sys.exc_info()[1] self.assertEqual(e.retry_after_secs, 10) def test_serialization(self): """ Tests non-json data returned """ r = mock.Mock(spec=requests.Response) r.status_code = 200 r.content = b"iyam not jason" f = Fitbit(**self.client_kwargs) f.client._request = lambda *args, **kwargs: r self.assertRaises(exceptions.BadResponse, f.user_profile_get) def test_delete_error(self): """ Delete requests should return 204 """ r = mock.Mock(spec=requests.Response) r.status_code = 201 r.content = b'{"it\'s all": "ok"}' f = Fitbit(**self.client_kwargs) f.client._request = lambda *args, **kwargs: r self.assertRaises(exceptions.DeleteError, f.delete_activities, 12345) python-fitbit-0.3.0/gather_keys_oauth2.py000077500000000000000000000060561304170412400204610ustar00rootroot00000000000000#!/usr/bin/env python import cherrypy import os import sys import threading import traceback import webbrowser from base64 import b64encode from fitbit.api import Fitbit from oauthlib.oauth2.rfc6749.errors import MismatchingStateError, MissingTokenError class OAuth2Server: def __init__(self, client_id, client_secret, redirect_uri='http://127.0.0.1:8080/'): """ Initialize the FitbitOauth2Client """ self.success_html = """

You are now authorized to access the Fitbit API!


You can close this window

""" self.failure_html = """

ERROR: %s


You can close this window

%s""" self.fitbit = Fitbit( client_id, client_secret, redirect_uri=redirect_uri, timeout=10, ) def browser_authorize(self): """ Open a browser to the authorization url and spool up a CherryPy server to accept the response """ url, _ = self.fitbit.client.authorize_token_url() # Open the web browser in a new thread for command-line browser support threading.Timer(1, webbrowser.open, args=(url,)).start() cherrypy.quickstart(self) @cherrypy.expose def index(self, state, code=None, error=None): """ Receive a Fitbit response containing a verification code. Use the code to fetch the access_token. """ error = None if code: try: self.fitbit.client.fetch_access_token(code) except MissingTokenError: error = self._fmt_failure( 'Missing access token parameter.
Please check that ' 'you are using the correct client_secret') except MismatchingStateError: error = self._fmt_failure('CSRF Warning! Mismatching state') else: error = self._fmt_failure('Unknown error while authenticating') # Use a thread to shutdown cherrypy so we can return HTML first self._shutdown_cherrypy() return error if error else self.success_html def _fmt_failure(self, message): tb = traceback.format_tb(sys.exc_info()[2]) tb_html = '
%s
' % ('\n'.join(tb)) if tb else '' return self.failure_html % (message, tb_html) def _shutdown_cherrypy(self): """ Shutdown cherrypy in one second, if it's running """ if cherrypy.engine.state == cherrypy.engine.states.STARTED: threading.Timer(1, cherrypy.engine.exit).start() if __name__ == '__main__': if not (len(sys.argv) == 3): print("Arguments: client_id and client_secret") sys.exit(1) server = OAuth2Server(*sys.argv[1:]) server.browser_authorize() profile = server.fitbit.user_profile_get() print('You are authorized to access data for the user: {}'.format( profile['user']['fullName'])) print('TOKEN\n=====\n') for key, value in server.fitbit.client.session.token.items(): print('{} = {}'.format(key, value)) python-fitbit-0.3.0/requirements/000077500000000000000000000000001304170412400170315ustar00rootroot00000000000000python-fitbit-0.3.0/requirements/base.txt000066400000000000000000000000541304170412400205030ustar00rootroot00000000000000python-dateutil>=1.5 requests-oauthlib>=0.7 python-fitbit-0.3.0/requirements/dev.txt000066400000000000000000000000721304170412400203470ustar00rootroot00000000000000-r base.txt -r test.txt cherrypy>=3.7,<3.9 tox>=1.8,<2.2 python-fitbit-0.3.0/requirements/test.txt000066400000000000000000000001241304170412400205460ustar00rootroot00000000000000coverage>=3.7,<4.0 freezegun>=0.3.8 mock>=1.0 requests-mock>=1.2.0 Sphinx>=1.2,<1.4 python-fitbit-0.3.0/setup.py000066400000000000000000000030221304170412400160150ustar00rootroot00000000000000#!/usr/bin/env python # -*- coding: utf-8 -*- import re from setuptools import setup required = [line for line in open('requirements/base.txt').read().split("\n") if line != ''] required_test = [line for line in open('requirements/test.txt').read().split("\n") if not line.startswith("-r") and line != ''] fbinit = open('fitbit/__init__.py').read() author = re.search("__author__ = '([^']+)'", fbinit).group(1) author_email = re.search("__author_email__ = '([^']+)'", fbinit).group(1) version = re.search("__version__ = '([^']+)'", fbinit).group(1) setup( name='fitbit', version=version, description='Fitbit API Wrapper.', long_description=open('README.rst').read(), author=author, author_email=author_email, url='https://github.com/orcasgit/python-fitbit', packages=['fitbit'], package_data={'': ['LICENSE']}, include_package_data=True, install_requires=["setuptools"] + required, license='Apache 2.0', test_suite='fitbit_tests.all_tests', tests_require=required_test, classifiers=( 'Intended Audience :: Developers', 'Natural Language :: English', 'License :: OSI Approved :: Apache Software License', 'Programming Language :: Python', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: Implementation :: PyPy' ), ) python-fitbit-0.3.0/tox.ini000066400000000000000000000006631304170412400156260ustar00rootroot00000000000000[tox] envlist = pypy,py35,py34,py33,py27,docs [testenv] commands = coverage run --source=fitbit setup.py test deps = -r{toxinidir}/requirements/test.txt [testenv:pypy] basepython = pypy [testenv:py35] basepython = python3.5 [testenv:py34] basepython = python3.4 [testenv:py33] basepython = python3.3 [testenv:py27] basepython = python2.7 [testenv:docs] basepython = python3.4 commands = sphinx-build -W -b html docs docs/_build