Package 'hRvstAPI'

Title: This is a wrapper for the Harvest REST API v2.
Description: The foremost function of this package is to act as a *convenient* wrapper around the Harvest REST API v2. To that end, the package handles credentials either through the system keyring using the `keyring` package (preferred), or through environment variables written to a local `.Renviron` file. Credential handling can be initiated explicitly, but otherwise happens automatically upon making an API request. This package tries to make accessing the Harvest API as easy as possible. While it does allow some tweaking of the actual HTTP request via function arguments, the primary method for accessing API data is to simply select which resource to query (e.g. "users" will download all users data). *Note, that the default is to only download "active" records.* The reason for this mode of access is that a request usually isn't very big and the typical user will probably want to manipulate the data in a more sophisticated manner for some downstream visualization or other analysis. Building on this premise, the secondary function of this package is to manage the long term storage of API data to minimize remote requests. This is achieved by storing API data in a local SQLite database, for which this package also provides a straightforward way of accessing and updating with new data. There is a companion Shiny application (hRvst) which utilizes this wrapper for Harvest API data access, and perhaps slightly enhances this package.
Authors: Eric Hunt [aut, cre]
Maintainer: Eric Hunt <[email protected]>
License: MIT + file LICENSE
Version: 0.0.0.9000
Built: 2025-03-08 04:16:20 UTC
Source: https://github.com/eric-hunt/hRvstAPI

Help Index


Add user credentials to the keyring.

Description

This is intended to be a helper function for setting credentials.

Usage

add_creds(.report = NULL)

Arguments

.report

A list – report returned from hRvstAPI::check_creds()


See if and where user credentials exist.

Description

Useful for informing subsequent user action within an application.

Usage

check_creds()

Value

A list – variables related to credential existence.


Delete all hRvstAPI Harvest credentials from the environment.

Description

This is intended to be a helper function for setting credentials.

Usage

clear_environment_creds()

Delete all hRvstAPI Harvest credentials from the keyring.

Description

This is intended to be a helper function for setting credentials.

Usage

clear_keyring_creds()

Create a SQLite database to hold Harvest API v2 request data.

Description

Create a SQLite database to hold Harvest API v2 request data.

Usage

create_db(db_path = NULL, rds_path = NULL)

Arguments

db_path

A string – file path where a local .sqlite file containing Harvest API data should be created.

rds_path

A string – file path where a local .rds file containing Harvest API data should exist.


Download Harvest API v2 requests into a local file.

Description

Download Harvest API v2 requests into a local file.

Usage

create_rds(
  ...,
  is_active = NULL,
  updated_since = NULL,
  weeks_ago = NULL,
  from = NULL,
  to = NULL,
  rds_path = NULL,
  sql_colnames = TRUE,
  .extra_params = NULL
)

Arguments

...

Resources as "named" key/value pairs to be requested. (hrvst_req())

is_active

A string – 'true' or 'false' passed to hrvst_GET().

updated_since

A string – passed to hrvst_GET().

weeks_ago

An integer – overrides updated_since argument; the number of weeks prior to the current week to convert to a date and pass to the 'updated_since' query parameter.

from

TODO

to

TODO

rds_path

A string – file path where a local .rds file containing Harvest API data should be created.

sql_colnames

A Boolean – automatically convert '.' to '_' in column names for easier SQL query construction.

.extra_params

Key-value pairs – optional additional query parameters passed to ... of hrvst_GET().

See Also

hrvst_req()


Get the unique set of key values that already exist in a table.

Description

Get the unique set of key values that already exist in a table.

Usage

get_keys(db_connection, tbl, key = NULL)

Arguments

db_connection

A DBI compatible connection object – the database containing Harvest API v2 request data.

tbl

A string – name of the table from which to gather primary keys.

key

A string – name of the primary key column in tbl.

Value

A numeric vector – a vector of integer primary keys which currently exist in tbl.


Return the user's Harvest API v2 account ID.

Description

Return the user's Harvest API v2 account ID.

Usage

hrvst_acct_id()

Value

A string – the user account ID.

See Also

Harvest API V2 Documentation | Authentication


Create an authenticated GET request with headers and queries.

Description

The basic headers that are automatically set in this function are Harvest-Account-Id, Authorization, and User-Agent. Other headers can be added by the user and will be concatenated, but currently these important headers are set by the hRvstAPI::hrvst_acct_id(), hRvstAPI::hrvst_token(), and hRvstAPI::.agent objects.

Usage

hrvst_GET(
  url = NULL,
  headers = NULL,
  is_active = NULL,
  updated_since = NULL,
  from = NULL,
  to = NULL,
  ...
)

Arguments

url

A string – the URL component for Harvest API v2 requests (NULL will default to .url).

headers

A list – the headers required for authentication of each Harvest API v2 request: 'Harvest-Account-Id', 'Authorization' (PAT), and 'User-Agent' (this package GitHub repo, .agent).

is_active

A string – 'true' or 'false' (Boolean per Harvest API requirement) should only active ('true') or inactive ('false') records be requested; default NULL downloads all records.

updated_since

A string – a date or date/time value (in ISO8601 character format) indicated how far back in time to retrieve records. Not supported by all of the Harvest v2 APIs.

from

TODO

to

TODO

...

Key-value pairs – optional additional query parameters.

Value

An HTTP response: an S3 list with class httr2_request.

See Also

httr2::request()


Perform a request for a Harvest API resource.

Description

This is the primary function used to gather resources from the Harvest API v2. It uses hrvst_GET() to construct an httr2::request() and then performs that request after modifying the URL.

Requests are automatically throttled to comply with Harvest API v2 constraints.

Usage

hrvst_req(
  resource = NULL,
  all_pages = TRUE,
  base_url = NULL,
  headers = NULL,
  is_active = "true",
  updated_since = NULL,
  weeks_ago = NULL,
  from = NULL,
  to = NULL,
  ...
)

Arguments

resource

A string – modifies the URL path to point to a specific Harvest API v2 resource. Currently available options:

all_pages

A Boolean – should all pages be gathered for a requested resource? (default value TRUE)

base_url

– A string – the common URL component for Harvest API v2 requests, passed to hrvst_GET().

headers

A list – passed to hrvst_GET().

is_active

A string – 'true' or 'false' passed to hrvst_GET().

updated_since

A string – passed to hrvst_GET().

weeks_ago

An integer – overrides updated_since argument; the number of weeks prior to the current week to convert to a date and pass to the 'updated_since' query parameter.

from

TODO

to

TODO

...

Key-value pairs – passed to hrvst_GET().

Details

If only a specific page is required, the ⁠page = [n]⁠ parameter can be passed to ... to be added to the query parameters. Adding this query parameter will automatically set all_pages to FALSE.

Value

A tibble::tibble() of all response content.

See Also

Harvest API V2 Documentation | Rate Limiting


Return the user's Harvest API v2 personal access token.

Description

Return the user's Harvest API v2 personal access token.

Usage

hrvst_token()

Value

A string – the user personal access token.

See Also

Harvest API V2 Documentation | Authentication


Get the name of the primary key column for a table.

Description

Get the name of the primary key column for a table.

Usage

key_col(db_connection, tbl)

Arguments

db_connection

A DBI compatible connection object – the database containing Harvest API v2 request data.

tbl

A string – name of the table for which to determine the primary key column.

Value

A string – the primary key column of tbl.


hRvstAPI parameters

Description

These are common values used by hRvstAPI functions like the base URL for the Harvest API v2, and the name of the hRvstAPI GitHub repo for a user agent to be logged with each GET request made to the Harvest API. In the future these may be set as options to give the user even more control over this wrapper.

Usage

.service

.url

.agent

.rds_path

.db_path

Format

An object of class character of length 1.

An object of class character of length 1.

An object of class character of length 1.

An object of class fs_path (inherits from character) of length 1.

An object of class fs_path (inherits from character) of length 1.


Query the local database and collect/return the result.

Description

Query the local database and collect/return the result.

Usage

query_db(query_string, ..., .is_active = TRUE, .db_loc = hRvstAPI::.db_path)

Arguments

query_string

A string – the query to be executed.

...

Key-value pairs – additional arguments for interpolation passed to glue::glue_sql().

.is_active

A Boolean – restrict results to active entries; defaults to TRUE

.db_loc

A string – file path where a local .sqlite file containing Harvest API data should exist.

Value

A tibble::tibble() of data collected from the query.


Retrieve user credentials from the keyring to environment.

Description

This is intended to be a helper function for setting credentials.

Usage

retrieve_creds(.report = NULL)

Arguments

.report

A list – report returned from hRvstAPI::check_creds()


Set user credentials as environment variables.

Description

Set user credentials as environment variables.

Usage

set_creds()

Update the local SQLite database with new Harvest API v2 request data.

Description

Update the local SQLite database with new Harvest API v2 request data.

Usage

update_db(db_path = NULL, rds_path = NULL)

Arguments

db_path

A string – file path where a local .sqlite file containing Harvest API data should be updated.

rds_path

A string – file path where a local .rds file containing Harvest API data should exist.