Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

List time entries #4

Closed
1 task
zmoog opened this issue Jan 24, 2023 · 6 comments · Fixed by #5
Closed
1 task

List time entries #4

zmoog opened this issue Jan 24, 2023 · 6 comments · Fixed by #5
Assignees
Labels
enhancement New feature or request

Comments

@zmoog
Copy link
Owner

zmoog commented Jan 24, 2023

$ tgl entries list
                                                      Time Entries

  Date         Description                                                Start      Stop       Duration   Tags
 ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  2023-01-25   community: https://github.com/elastic/beats/issues/34330   11:04 PM   11:27 PM   0:22:59    type:support

@zmoog zmoog self-assigned this Jan 24, 2023
@zmoog zmoog added the enhancement New feature or request label Jan 24, 2023
@zmoog
Copy link
Owner Author

zmoog commented Jan 24, 2023

The Time Entries API seems a good fit.

Here's a sample document:

$ curl -u "${TOGGL_API_TOKEN}:api_token" https://api.track.toggl.com/api/v9/me/time_entries | jq
[
    {
        "id": 2815158339,
        "workspace_id": 1815018,
        "project_id": 178435728,
        "task_id": null,
        "billable": false,
        "start": "2023-01-24T17:54:00+00:00",
        "stop": "2023-01-24T18:30:21Z",
        "duration": 2181,
        "description": "community: https://github.com/elastic/beats/issues/34330",
        "tags": [
            "type:support"
        ],
        "tag_ids": [
            12078060
        ],
        "duronly": true,
        "at": "2023-01-24T19:30:36+00:00",
        "server_deleted_at": null,
        "user_id": 2621333,
        "uid": 2621333,
        "wid": 1815018,
        "pid": 178435728
    }
]

Weird. The stop field has a different format than start and at.

@zmoog
Copy link
Owner Author

zmoog commented Jan 24, 2023

I am designing the table output with some hard-coded data:

@entries.command(name="list")
def list_entries():
    "entries list command description"
    table = Table(title="Time Entries", box=box.SIMPLE)
    table.add_column("Description")
    table.add_column("Duration")
    table.add_column("Tags")

    table.add_row(
        "community: https://github.com/elastic/beats/issues/34330",
        "2181",
        "type:support"
    )

    # turn table into a string using the Console
    console = Console(file=io.StringIO())
    console.print(table)

    click.echo(console.file.getvalue())

The result is:

$ tgl entries list
                                     Time Entries

  Description                                                Duration   Tags
 ────────────────────────────────────────────────────────────────────────────────────
  community: https://github.com/elastic/beats/issues/34330   2181       type:support

zmoog added a commit that referenced this issue Jan 25, 2023
@zmoog
Copy link
Owner Author

zmoog commented Jan 25, 2023

Updating the table layout with more data:

$ tgl entries list
                                                      Time Entries

  Date         Description                                                Start      Stop       Duration   Tags
 ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  2023-01-25   community: https://github.com/elastic/beats/issues/34330   11:04 PM   11:27 PM   0:22:59    type:support

zmoog added a commit that referenced this issue Jan 26, 2023
@zmoog
Copy link
Owner Author

zmoog commented Jan 26, 2023

Now we have a real Toggl client for the time entries endpoint. The next step is to update the CLI to use the client instead of the hard-coded value.

I need to control the API response, so I plan to use the pytest-recording package to disable network access and use recorded responses like I did in https://github.com/zmoog/axios/blob/1e5ee3e241a0ae4fe4138a30b628c96173fd6c6f/tests/test_axios.py#L25-L35

@zmoog
Copy link
Owner Author

zmoog commented Jan 27, 2023

With pytest-recording, we need to collect the actual network traffic into 'vcr cassettes' for a later use during tests.

$ pytest --record-mode=once tests/test_entries.py 

To avoid capturing credentials, I added the following configuration:

# tests/conftest.py
import pytest

@pytest.fixture(scope="module")
def vcr_config():
    return {"filter_headers": ["authorization"]}

If you're making changes to the code, you can re-capture the network traffic again and again by using the record mode rewrite:

$ pytest --record-mode=rewrite tests/test_entries.py 

zmoog added a commit that referenced this issue Jan 27, 2023
- We use the actual TimeEntries API to fetch data from Toggl
- Record and replay network traffic using pytest-recording

refs: #4
zmoog added a commit that referenced this issue Jan 27, 2023
- We use the actual TimeEntries API to fetch data from Toggl
- Record and replay network traffic using pytest-recording

refs: #4
zmoog added a commit that referenced this issue Jan 27, 2023
- We use the actual TimeEntries API to fetch data from Toggl
- Record and replay network traffic using pytest-recording

refs: #4
@zmoog
Copy link
Owner Author

zmoog commented Jan 28, 2023

The cli module contains too much code for formatting data, I wanna try to move all the formatting logic in a dedicated component.

zmoog added a commit that referenced this issue Jan 28, 2023
zmoog added a commit that referenced this issue Jan 28, 2023
Simple demonstration of the only available command :)

refs: #4
@zmoog zmoog mentioned this issue Jan 28, 2023
7 tasks
@zmoog zmoog closed this as completed in #5 Jan 28, 2023
zmoog added a commit that referenced this issue Jan 28, 2023
* Mock a simple list of time entries

refs #4

* Add TimeEntries client

Ref: #4

* Switch from hard-coded to actual Toggl API calls

- We use the actual TimeEntries API to fetch data from Toggl
- Record and replay network traffic using pytest-recording

refs: #4

* Add missing TOGGL_API_TOKEN environment variable

* Move output formatting into a result module

refs: #4

* Add a 'entries list' command usage example

Simple demonstration of the only available command :)

refs: #4

* Improve help messages add clean up default options

Default values for date-related options were a little messy. I hope
this change makes the date logic a little more readable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant