Code Monkey home page Code Monkey logo

fdk-client-python's Introduction

FDK Python

GitHub requirements.txt version GitHub Workflow Status GitHub Coverage Status

FDK client for python

Getting Started

Get started with the python Development SDK for Fynd Platform

Usage

pip install "git+https://github.com/gofynd/[email protected]#egg=fdk_client"

Using this method, you can import fdk-client-python like so:

from fdk_client.application.ApplicationClient import ApplicationClient
from fdk_client.application.ApplicationConfig import ApplicationConfig

Log Curl

To print the curl command in the console for all network calls made using applicationClient or platformClient, set the logger level to "DEBUG".

config = ApplicationConfig({
    "applicationID": "YOUR_APPLICATION_ID",
    "applicationToken": "YOUR_APPLICATION_TOKEN",
    "logLevel": "DEBUG"
})

applicationClient = ApplicationClient(config)

async def apiCall():
    try:
        response = await applicationClient.user.loginWithEmailAndPassword(body = {
              "password": "value",
              "username": "value"
            })
        print(response)
    except Exception as e:
        print(e)

apiCall()

The above code will log the curl command in the console

curl --request POST 'https://api.fynd.com/service/application/user/authentication/v1.0/login/password' --header 'x-fp-date: 20240308T171355Z' --header 'x-fp-signature: v1.1:aad5df1ad58fc87e74b040f5b0394be6cdd2d687ec7681b200bb3e20d48a458a' --header 'Authorization: Bearer <authorization-token>' --header 'Content-Type: application/json' --data-raw '{"password": "value", "username": "value"}'

Sample Usage - ApplicationClient

config = ApplicationConfig({
    "applicationID": "YOUR_APPLICATION_ID",
    "applicationToken": "YOUR_APPLICATION_TOKEN",
    "domain": "YOUR_DOMAIN",
    "locationDetails": "LOCATION_DETAILS_OBJECT"
})

applicationClient = ApplicationClient(config)
applicationClient.setLocationDetails(
    { 
        "pincode":"385001",
        "country": "India",
        "city":  "Ahmedabad",
        "location": {
            "longitude": "72.585022", 
            "latitude": "23.033863"
        }
    }
)

async def getProductDetails():
    try:
        product = await applicationClient.catalog.getProductDetailBySlug(slug="product-slug")
        print(product)
    except Exception as e:
        print(e)

getProductDetails()

Persisting cookies across requests

Some APIs require a login to proceed ahead. For this, we have several login options mentioned in these User methods. Using any of these methods, you can get a cookie. All you need to do is store the cookie in application config. Consider an example with mobile OTP:

send_otp_response = applicationClient.user.loginWithOTP(
    platform=YOUR_APPLICATION_ID,
    body={
        "countryCode": "<your country code without the + sign>",
        "captchaCode": "<your captcha code>",
        "mobile": "<your mobile number>"
    }
)

login_response = applicationClient.user.verifyMobileOTP(
    platform=YOUR_APPLICATION_ID,
    body={
        "requestId": send_otp_response["json"]["request_id"],
        "otp": <your OTP>
    }
)

applicationClient.config.cookies = login_response["cookies"]

This will make sure the cookies are passed in all subsequent API calls.


Sample Usage - PlatformClient

from fdk_client.common.aiohttp_helper import AiohttpHelper
from fdk_client.platform.PlatformConfig import PlatformConfig
from fdk_client.platform.PlatformClient import PlatformClient
from fdk_client.common.utils import create_url_without_domain, get_headers_with_signature


async def setAccessToken(platformConfig, cookies):
    reqData = {
        "grant_type": "client_credentials",
        "client_id": platformConfig.apiKey,
        "client_secret": platformConfig.apiSecret
    }
    url = f"{platformConfig.domain}/service/panel/authentication/v1.0/company/{platformConfig.companyId}/oauth/token"
    url_without_domain = await create_url_without_domain(f"/service/panel/authentication/v1.0/company/{platformConfig.companyId}/oauth/token")
    headers = get_headers_with_signature(platformConfig.domain, "post", url_without_domain, "", {}, reqData)
    res = await AiohttpHelper().aiohttp_request("POST", url, reqData, headers, cookies=cookies)
    return res["json"]

async def loginUser(platformConfig):
    skywarpURL = f"{platformConfig.domain}/service/panel/authentication/v1.0/auth/login/password"
    userData = {
        "username": "YOUR_USERNAME",
        "password": "YOUR_PASSWORD",
        "g-recaptcha-response": "_skip_"
    }
    url_without_domain = "/service/panel/authentication/v1.0/auth/login/password"
    headers = get_headers_with_signature(platformConfig.domain, "post", url_without_domain, "", {}, userData)
    res = await AiohttpHelper().aiohttp_request("POST", skywarpURL, userData, headers)
    return res

try:
    platformConfig = PlatformConfig({
        "companyId": "YOUR_COMPANY_ID",
        "domain": "YOUR_DOMAIN",
        "apiKey": "YOUR_APIKEY",
        "apiSecret": "YOUR_APISECRET"
    })
    loginResponse = await loginUser(platformConfig)
    # print(loginResponse)
    tokenResponse = await setAccessToken(platformConfig, loginResponse["cookies"])
    # print(tokenResponse)
    await platformConfig.oauthClient.setToken(tokenResponse)
    platformClient = PlatformClient(platformConfig)
    res = await platformClient.lead.getTicket(id="YOUR_TICKET_ID")
    # use res
except Exception as e:
    print(e)

Headers

When calling method, custom request headers can be included by passing a dictionary of headers as the request_headers argument in the method signature

request_headers = {
    "x-api-version": "1.0"
}

response = await platform_client.application("<APPLICATION_ID>").theme.getAllPages(
    theme_id="<THEME_ID>",
    request_headers=request_headers
)

fdk-client-python's People

Contributors

bhargavprajapatifynd avatar brijeshgajjarfynd avatar jigardafda avatar meetkoriya13 avatar nikhilmanapure avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fdk-client-python's Issues

pip install doesn't work

terminal> pip install fdk-client-python

ERROR: Could not find a version that satisfies the requirement fdk-client-python (from versions: none)
ERROR: No matching distribution found for fdk-client-python

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.