Coder Social home page Coder Social logo

py-frappe-client's Introduction

Py-Frappe-Client

Install

pip install py-frappe-client

View on PyPi

Usage

Token based authentication

>>> from frappe_client import FrappeRequest
>>> client = FrappeRequest(url="http://localhost:8002", api_key="API_KEY", api_secret="API_SECRET")
>>> client.get("method_name")
>>> client.post("method_name")

Session based authentication (Legacy)

Initialize

>>> from frappe_client import FrappeRequest
>>> client = FrappeRequest(url="http://localhost:8002", username="user", password="password")

>>> client.__dict__

{
   "frappe_session":<requests.sessions.Session at 0x1073fc978>,
   "url":"http://localhost:8002",
   "usr":"Administrator",
   "pwd":"pass",
   "session_data":{
      "full_name":"Administrator",
      "sid":"6684ed4e173094199b1cc0913d4044fdcec1d87d5d6a74a3728f229b",
      "system_user":"yes",
      "user_id":"Administrator",
      "user_image":""
   },
   "callback": None
}

Using Callback

The wrapper checks for a HTTP 403 Response from the server for the first time on every API call. If the server returns 403, the wrapper attempts to login again. During this login, if a callback function is passed by the user the new session_data is passed on to this callback parameter.

# Example Callback function
 def store_session_data(data):
    print ("Callback function invoked with arg {}".format(data))

>>> client = FrappeRequest(url="http://localhost:8002", username="Administrator", password="pwd", callback=store_session_data)
Callback function invoked with arg {'full_name': 'Administrator', 'sid': '427f585e17c551b9bf70ff64a22a6998064d0415914aaf91beb981f2', 'system_user': 'yes', 'user_id': 'Administrator', 'user_image': ''}

Get Session Data

client.session_data

# Example Output
>>> client.session_data

>>> {
   "full_name":"Administrator",
   "sid":"427f585e17c551b9bf70ff64a22a6998064d0415914aaf91beb981f2",
   "system_user":"yes",
   "user_id":"Administrator",
   "user_image":""
}

Storing Session Data

session_data is a dict object which is returned by client.session_data field. Session Data needs to be stored locally on the client's end (database/redis/file/etc).

On further initializations of the FrappeRequest() class, you should pass session_data field to prevent multiple sessions for the same user.

If session_data field is passed, all further API calls will use the same session.

Invoke GET Endpoint

client.get(method, params)

>>> # Example Request
>>> response = client.get(method="acop_process.lead_integration.ping")
>>> response.json()
{'message': 'PONG'}

Invoke POST Endpoint

client.post(method, data)

>>> # Example Request
>>> response = client.post(method="acop_process.lead_integration.post_ping", data={'test_param':123})
>>> response.json()
{'message': 'POST PONG success 123'}

Documents API

# Fetch a single resource
>>> response = client.get_doc("Custom Doctype", name="record1")
# Fetch multiple resources
>>> response = client.get_doc("Custom Doctype")
# With filters, fields
>>> response = client.get_doc("Custom Doctype", fields=["field1", "field2"], filters={"field3", ["in", [1, 2, 3]]})
# With page size, fetch 50 records at once
>>> response = client.get_doc("Custom Doctype", limit_page_length=50)
# fetch 50 records starting from offset 50
>>> response = client.get_doc("Custom Doctype", limit_start=50, limit_page_length=50)
# paginated generator
>>> fetch_iter = client.get_doc("Custom Doctype", limit_page_length=10, pagination=True)
>>> for records in fetch_iter:
      print(records["data"])

Pagination API

# Fetch in batches of 10 records from API using a generator
>>> fetch_iter = client.get_paginated_doc("Custom Doctype", limit_page_lenth=10)
>>> for records in fetch_iter:
      print(records["data"])

py-frappe-client's People

Contributors

joeirimpan avatar josejibin avatar mr-karan avatar

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.