Connect Library
FairFood-Connect
The fairfood-connect library provides a set of classes and methods to interact with the FairFood Connect API for managing and accessing data related to buyers, farmers, products, companies, transactions and catalogs. This library allows users to interact with the API endpoints and perform operations like retrieving, creating, updating and deleting data.
Installing Fairfood-Connect and Supported Versions
Fairfood-Connect is available on PyPI:
Fairfood-Connect officially supports Python 3.9+.
Supported Features & Best–Practices
- Create Buyer
- List country and provinces
- Get details of company
- Create, get and update details of farmer
- List Products
- Create and delete Transactions
Example Usage
Initialization
Initialize the connect object with the required parameters: base_url, client_id, client_secret, and entity_id. This will automatically handle the authentication and setup for API requests.
Parameters
Name | Description |
---|---|
base_url | The root URL of the API (e.g., {API_ROOT_URL} ) |
client_id | Your client ID provided by FairFood Connect (e.g., {CLIENT_ID} ) |
client_secret | Your client secret provided by FairFood Connect (e.g., {CLIENT_SECRET} ) |
entity_id | The entity ID for your account (e.g., {ENTITY_ID} ) |
Once initialized, the connect object is ready to make API requests with the authentication token automatically included in the headers.
Get Access Token
The Get Access Token is automatically handled during the initialization process. When you create an instance of the Connect class, the access token is automatically generated and included in the request headers for subsequent API calls. You don’t need to manually fetch or pass the token.
We can obtain token by below code
Company Module
The Company Module allows you to interact with the company-related API endpoints. The get_details method is used to retrieve details of a specific company based on its unique identifier (company_id).
To get company details, you must provide the company_id of the company you want to retrieve information for. This ID is typically the unique identifier for the company in the system.
Arguments: company_id (str): The unique identifier for the company you want to fetch details for. Replace "" with the actual company ID.
Returns: A dictionary containing the company’s details, such as name, address, contact information, and more, based on the available data for the provided company ID.
Product Module
The Product Module allows you to interact with the product-related API endpoints. The list method is used to retrieve a list of products. You can specify the number of products to retrieve by using limit and offset, although these are optional parameters. If not provided, the default limit is 10, and the default offset is 0.
limit (int)(optional): The maximum number of products to retrieve (default is 10). offset (int)(optional): The number of products to skip before starting to return results (default is 0)
Returns: A dictionary containing a list of products, including details such as product name, ID, description, etc.
Catalog Module
The Catalog Module provides methods to interact with catalog-related API endpoints, specifically for retrieving lists of countries and provinces.
List Countries
The list_countries method retrieves a list of all available countries. Returns a dictionary containing a list of countries with relevant details.
List Countries with Provinces
The list_countries_with_provinces method retrieves a list of countries along with their associated provinces. Returns a dictionary containing a list of countries, each with its respective provinces.
List Provinces by Country The list_provinces_by_country method retrieves the list of provinces for a specific country. You must pass the country_name as an argument to specify which country’s provinces to retrieve.
Arguments: country_name (str): The name of the country whose provinces you want to list. Replace "" with the actual country name.
Returns: A dictionary containing a list of provinces for the specified country.
Farmer Module
The Farmer Module allows you to manage farmer-related data through methods that create, retrieve, and update farmer information.
Create Farmer The create method allows you to create a new farmer by passing in a data dictionary containing all the necessary information about the farmer.
Arguments: data (dict): A dictionary containing all the necessary details for the farmer, including personal information, location, plots, and other meta data.
Returns: A dictionary with the newly created farmer’s details, including the unique farmer_id.
Get Farmer Details The get_details method retrieves information about a specific farmer using their farmer_id.
Arguments: farmer_id (str): The unique identifier of the farmer whose details you want to retrieve.
Returns: A dictionary containing the farmer’s details.
Update Farmer Details
The update_details method allows you to update specific details of an existing farmer. You need to pass both the farmer_id and the data containing the fields you wish to update.
Arguments: farmer_id (str): The unique identifier of the farmer whose details you want to update. data (dict): A dictionary containing the fields to be updated, such as last_name, phone, address, etc
Returns: A dictionary with the updated farmer details.
Buyer Module
The Buyer Module allows you to manage buyer-related data. The primary functionality provided is the creation of a buyer.
Create Buyer The create method allows you to create a new buyer by passing in a data dictionary that contains all the necessary information about the buyer.
Arguments: data (dict): A dictionary containing all the necessary details for the buyer, such as name, contact information, address, and any other relevant metadata.
Returns: A dictionary containing the newly created buyer’s details.
Transaction Module
The Transaction Module allows you to manage transaction-related data, including creating and deleting transactions.
Create Transaction The create method allows you to create a new transaction by passing in a data dictionary containing the transaction details.
Arguments: data (dict): A dictionary containing the transaction details, such as payment information, amount, currency, source, destination, and any additional metadata.
Returns: A dictionary containing the created transaction details, including the unique transaction identifier.
Delete Transaction The delete method allows you to delete a transaction by providing the transaction_id.
Arguments: transaction_id (str): The unique identifier of the transaction you wish to delete
Returns: A dictionary containing the status of the transaction deletion (typically includes a success message).