Connecting and Interacting with the Linnworks API
This guide provides detailed instructions on how to connect to the Linnworks API, a cloud-based online inventory management system that allows users to manage online selling processes. The provided Python code establishes a connection, manages authentication, and interacts with Linnworks' data and services.
The Python code for connecting to the Linnworks API primarily consists of the LinnApi class and its methods. Here's an overview of its structure and functionality:
The LinnApi class is the base class for connecting to the Linnworks API. It performs three main tasks:
- Initializes a session: This sets up a connection with the Linnworks API.
- Sets up headers: The headers contain required information such as authorization tokens.
- Handles token authorization: The class manages the process of obtaining and refreshing authorization tokens.
The get_new_token method is responsible for fetching and refreshing the authorization token required for API requests. It sends a POST request to the Linnworks API's authorization endpoint and updates the token when necessary. The token has a one-hour expiry time.
The get_instance function implements the Singleton design pattern, ensuring only one instance of the LinnApi class is created throughout the application’s lifecycle. This promotes efficient resource usage when making API requests.
The make_request method is used to send asynchronous HTTP requests to the Linnworks API. It handles token refresh, retries, and various HTTP status codes. It returns response data in dictionary format. This function is reused ion the whole pipeline to make connections.
- Ensure that all Pre-requisites are in order and that you have the API credentials in your .env file.
- Instantiate the LinnApi class:
- Use the linn_api instance to make API requests, such as fetching data from Linnworks.
This explains the usage of the Linns API class, it is used to help Interact with the Linnworks Api.