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

Forecast.png

Forecast - List all people

Give Feedback | Bug report

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

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

Description: This notebook will list all people 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: people limit, to get all people 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 people

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