Skip to content

Commit

Permalink
Support for the sandbox environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinta365 committed Aug 4, 2024
1 parent 835ea8f commit 8f9c085
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ npx jsr add @pinta365/oura-api
```javascript
import { Oura, DateFormat } from "@pinta365/oura-api";

// Option 1 - Instantiate with a token in string format.
// Replace 'YOUR_ACCESS_TOKEN' with your actual access token
const accessToken = "YOUR_ACCESS_TOKEN";
const ouraClient = new Oura(accessToken);

// Option 2 - Instantiate with a options object.
// If useSandbox is set to true the accessToken will not be used. This is also the only way to opt into the sandbox environment. See more details about the sandbox further down.
const options = {
accessToken: "YOUR_ACCESS_TOKEN",
useSandbox: false, // Set to true for the sandbox environment
};
const ouraClient = new Oura(options);

const startDate: DateFormat = "2023-01-01";
const endDate: DateFormat = "2023-01-10";

Expand All @@ -58,10 +67,20 @@ Code example.

```javascript
const Api = require("oura_api");

// Option 1 - Instantiate with a token in string format.
// Replace 'YOUR_ACCESS_TOKEN' with your actual access token
const accessToken = "YOUR_ACCESS_TOKEN";
const ouraClient = new Api.Oura(accessToken);

// Option 2 - Instantiate with a options object.
// If useSandbox is set to true the accessToken will not be used. This is also the only way to opt into the sandbox environment. See more details about the sandbox further down.
const options = {
accessToken: "YOUR_ACCESS_TOKEN",
useSandbox: false, // Set to true for the sandbox environment
};
const ouraClient = new Api.Oura(options);

const startDate = "2023-01-01";
const endDate = "2023-01-10";

Expand Down Expand Up @@ -110,7 +129,25 @@ Library documentation can be found at the [JSR documentation](https://jsr.io/@pi
| Delete subscription | Implemented |
| Renew subscription | Implemented |

### Additional info concerning the webhook API
## Using the Sandbox Environment (For Testing)

The Oura API provides a sandbox environment ([Sandbox docs](https://cloud.ouraring.com/v2/docs#tag/Sandbox-Routes)) for
testing your application with fake user data that you can access without an Oura account. To use the sandbox, follow
these steps:

1. **Create a Sandbox Client:** Opt in for the sandbox endpoints by using the optional useSandbox option when you
instantiate the Oura class.
```javascript
const ouraSandboxClient = new Oura({ useSandbox: true });
```
2. **Make API Calls:** Use the `ouraSandboxClient` object to make API calls, just like you would with the regular
client.\
The API will automatically route your requests to the sandbox environment.
```javascript
const dailyActivityData = await ouraSandboxClient.getDailyActivityDocuments(startDate, endDate);
```

## Additional info concerning the webhook API

According to the API docs the webhooks enable you to receive near real-time Oura data updates and are the preferred way
to receive the latest data from the Oura API.
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pinta365/oura-api",
"version": "0.5.8",
"version": "0.5.9",
"exports": "./mod.ts",
"publish": {
"exclude": ["scripts"]
Expand Down
41 changes: 34 additions & 7 deletions src/Oura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ import type {
export * from "./types/oura.ts";
import { APIError, isValidDate, MissingTokenError, ValidationError } from "./utils.ts";

/**
* Options for configuring the Oura API client.
*/
interface ApiOptions {
/** A personal access token generated at the Oura Cloud website. */
accessToken?: string;
/**
* Set to `true` to use the Oura sandbox environment. accessToken will be ignored.
* The sandbox provides a simulated environment for testing your API integration.
*/
useSandbox?: boolean;
}

/**
* Base class for the Oura API.
* Class containing all the methods to access the Oura API with an access token.
Expand All @@ -53,15 +66,29 @@ class Oura {
* Creates a new Oura API client.
*
* @constructor
* @param {string} accessToken - A personal access token generated at the Oura Cloud website.
* @throws {Error} Throws an MissingTokenError-error if the access token is missing.
* @param {string | ApiOptions} accessTokenOrOptions - Either a personal access token (string) generated at the Oura Cloud website, or an options object containing the configuration settings.
* @throws {Error} Throws a MissingTokenError error if the access token is missing.
*/
constructor(accessToken: string) {
if (!accessToken) {
constructor(accessTokenOrOptions: string | ApiOptions) {
let options: ApiOptions = {};

if (typeof accessTokenOrOptions === "string") {
options = { accessToken: accessTokenOrOptions };
} else {
options = accessTokenOrOptions;
}

if (!options.accessToken && !options.useSandbox) {
throw new MissingTokenError();
}
this.#accessToken = accessToken;
this.#baseUrlv2 = "https://api.ouraring.com/v2/usercollection/";

if (options.useSandbox) {
this.#accessToken = "";
this.#baseUrlv2 = "https://api.ouraring.com/v2/sandbox/usercollection/";
} else {
this.#accessToken = options.accessToken!;
this.#baseUrlv2 = "https://api.ouraring.com/v2/usercollection/";
}
}

/**
Expand All @@ -84,7 +111,7 @@ class Oura {
}

const params = new URLSearchParams(qs);

const response = await fetch(this.#baseUrlv2 + encodeURI(url) + (qs ? "?" + params.toString() : ""), {
method: "GET",
headers: {
Expand Down

0 comments on commit 8f9c085

Please sign in to comment.