Pipelines

Connecting and Interacting with the Linnworks API

6min

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.

Code Overview

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:

LinnApi Class

The LinnApi class is the base class for connecting to the Linnworks API. It performs three main tasks:

  1. Initializes a session: This sets up a connection with the Linnworks API.
  2. Sets up headers: The headers contain required information such as authorization tokens.
  3. Handles token authorization: The class manages the process of obtaining and refreshing authorization tokens.

Acquiring a New Token

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.

Python


Singleton Design Pattern

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.

Python


Asynchronous 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.

Python


Usage

  1. Ensure that all Pre-requisites are in order and that you have the API credentials in your .env file.
  2. Instantiate the LinnApi class:
  3. 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.