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

Forecast.png

Forecast - List all projects

Give Feedback | Bug report

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

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

Description: This notebook will list all projects 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

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

Optional

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

# 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" # Optional limit = 1000

Model

List all projects

This function will list all projects 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_projects( account_id, access_token, limit=-1, ): # Init data = [] df = pd.DataFrame() # Requests url = f"https://api.forecastapp.com/projects" headers = { "Accept": "application/json", "Forecast-Account-ID": account_id, "Authorization": "Bearer " + access_token, } res = requests.get(url=url, headers=headers) while True: if res.status_code == 200: # Get data res_json = res.json() projects = res_json.get("projects") for project in projects: data.append(project) # 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_projects = list_projects( account_id, get_access_token(), limit ) print("Row fetched:", len(df_projects)) df_projects.head()