Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/Forecast/Forecast_List_all_assignments.ipynb
2973 views
Kernel: Python 3

Forecast.png

Forecast - List all assignments

Give Feedback | Bug report

Tags: #forecast #assignments #api #list #python #get

Last update: 2023-08-08 (Created: 2023-08-08)

Description: This notebook will list all assignments from the forecast API. Forecast is a service that connects to harvest and allows you to plan for allocations to harvest projects.

Disclaimer:
Kindly be aware that the Forecast API is currently not available for public use or documentation. If you wish to customize and utilize this template for your specific needs, please reach out to the author.

Input

Import libraries

import requests import pandas as pd import naas import datetime

Setup variables

Mandatory

  • account_id: Account ID from Forecast

  • client_id: Client ID from Forecast

  • client_secret: Client Secret from Forecast

  • refresh_token: Refresh Token from Forecast

  • limit: assignments limit, to get all assignments enter -1

Optional

This notebook will filter all of the assignments that include any date going from start_date to end_date. Maximum timeframe of 180 days from start_date to end_date. Default start date is 90 days before current date, and default end date is 90 days after current date.

  • start_date: Starting date to filter from (ISO 8601 Format), Example date '2023-08-04'

  • end_date: Ending date to filter from (ISO 8601 Format), Example date '2023-08-04'

# Mandatory account_id = naas.secret.get("FORECAST_ACCOUNT_ID") or "YOUR_FORECAST_ACCOUNT_ID" client_id = naas.secret.get("FORECAST_CLIENT_ID") or "YOUR_FORECAST_CLIENT_ID" client_secret = naas.secret.get("FORECAST_CLIENT_SECRET") or "YOUR_FORECAST_CLIENT_SECRET" refresh_token = naas.secret.get("FORECAST_REFRESH_TOKEN") or "YOUR_FORECAST_REFRESH_TOKEN" limit = 1000 # Optional start_date = None end_date = None

Model

List all assignments

This function will list all assignments from the Forecast API

# Get Harvest Access token def get_access_token(): res = requests.post( url="https://id.getharvest.com/api/v2/oauth2/token", data={ "client_id": client_id, "client_secret": client_secret, "refresh_token": refresh_token, "grant_type": "refresh_token", }, headers={"User-Agent": 'Harvest-Scraper'}, ) res_json = res.json() if res.status_code == 201: return res_json["access_token"] else: print(res.status_code, res.json().get("message")) return "" def list_assignments( account_id, access_token, limit=-1, start_date=None, end_date=None ): # Init data = [] df = pd.DataFrame() # Requests url = f"https://api.forecastapp.com/assignments" headers = { "Accept": "application/json", "Forecast-Account-ID": account_id, "Authorization": "Bearer " + access_token, } today = datetime.date.today() if start_date: start_date_param = datetime.date.fromisoformat(start_date) else: start_date_param = today - datetime.timedelta(days=90) if end_date: end_date_param = datetime.date.fromisoformat(end_date) else: end_date_param = today + datetime.timedelta(days=90) params = {"start_date": start_date_param, "end_date": end_date_param} res = requests.get(url=url, params=params, headers=headers) while True: if res.status_code == 200: # Get data res_json = res.json() assignments = res_json.get("assignments") for assignment in assignments: data.append(assignment) # Manage limit if limit != -1 and len(data) >= limit: break break else: print(res.status_code, res.json().get("message")) break # Transform in dataframes df = pd.DataFrame(data) return df

Output

Display result

df_assignments = list_assignments( account_id, get_access_token(), limit, start_date, end_date ) print("Row fetched:", len(df_assignments)) df_assignments.head()