pyDataverse icon indicating copy to clipboard operation
pyDataverse copied to clipboard

Add logging functionality

Open skasberger opened this issue 5 years ago • 1 comments

Add logging functionality to all modules.

Prepare

  • [ ] Research
    • [ ] https://docs.python.org/3/library/logging.html
    • [ ] https://docs.python.org/3/howto/logging.html
    • [ ] https://www.youtube.com/watch?v=Pbz1fo7KlGg
    • [ ] https://www.youtube.com/watch?v=p0A4CV4MWd0
    • [ ] https://realpython.com/courses/logging-python/
    • [ ] see Wikipedia-API._query()
  • [ ] Which functionality should be logged?
  • [ ] Which data should be logged?
  • [ ] Should the data be stored? If yes, where and how?
  • [ ] Design
    • console outputs should tell better where the code exeution is and what is happening.
      • e.g. Update Dataset 10100 could not be created. to Dataset 10100 could not be created via API.
    • add all exceptions and errors
    • add module to output
    • Logging levels: INFO, WARNING, ERROR
    • use pre-commit plugin
  • [ ] Integrate with history functionality #43

Snippets:

import requests
import json

def pretty_print_request(request):
    print( '\n{}\n{}\n\n{}\n\n{}\n'.format(
        '-----------Request----------->',
        request.method + ' ' + request.url,
        '\n'.join('{}: {}'.format(k, v) for k, v in request.headers.items()),
        request.body)
    )

def pretty_print_response(response):
    print('\n{}\n{}\n\n{}\n\n{}\n'.format(
        '<-----------Response-----------',
        'Status code:' + str(response.status_code),
        '\n'.join('{}: {}'.format(k, v) for k, v in response.headers.items()),
        response.text)
    )

def test_post_headers_body_json():
    url = 'https://httpbin.org/post'

    # Additional headers.
    headers = {'Content-Type': 'application/json' }

    # Body
    payload = {'key1': 1, 'key2': 'value2'}

    # convert dict to json by json.dumps() for body data.
    resp = requests.post(url, headers=headers, data=json.dumps(payload,indent=4))

    # Validate response headers and body contents, e.g. status code.
    assert resp.status_code == 200
    resp_body = resp.json()
    assert resp_body['url'] == url

    # print full request and response
    pretty_print_request(resp.request)
    pretty_print_response(resp)

Implementation

  • [ ] Define requirements
    • [ ] no console output by default. maybe through argument (part of Api.init()??).
    • [ ]
  • [ ] Write tests
  • [ ] Write code
  • [ ] Update Docs
    • [ ] add to user guide
  • [ ] Update Docstrings
  • [ ] Run pytest
  • [ ] Run tox
  • [ ] Run pylint
  • [ ] Run mypy

Review

  • [ ] Docs

Follow-Ups

skasberger avatar Jun 26 '20 12:06 skasberger

As discussed during the 2024-02-14 meeting of the pyDataverse working group, we are closing old milestones in favor of a new project board at https://github.com/orgs/gdcc/projects/1 and removing issues (like this one) from those old milestones. Please feel free to join the working group! You can find us at https://py.gdcc.io and https://dataverse.zulipchat.com/#narrow/stream/377090-python

pdurbin avatar Feb 14 '24 20:02 pdurbin