What is the best convention to generate multiple payload for different endpoints in python code?

user8882700

My project requires different request payload for different endpoints. I am generating each one of them in python code and pass the generated payload to the python requests library. It's working but I am looking for a more elegant/cleaner way to do it. I was thinking about having yaml files and read them to generate my payload. Looking for more ideas and better way to generate request payload.

def abc(self):
    payload = {
             'key1' :'1',
             'key2' : '2'
               }
     return payload

def call_abc(self):
    request_payload = self.abc()
    requests.post(url, json=request_payload, headers)
Anwar Hossain

It doesn't matter if you use YAML or JSON. Look at this code:

request_map = {
    "http://some/end/point": [
        {
            'key1': '1',
            'key2': '2'
        },
        {
            'key3': '4',
            'key4': '5'
        }
    ],
    "http://some/other/end/point": [
        {
            'key1': '1',
            'key2': '2'
        },
        {
            'key3': '4',
            'key4': '5'
        }
    ]
}

def generate_call():
    for endpoint, payloads in request_map.items():
        for payload in payloads:
            yield endpoint, payload


def call_endpoint(url, payload):
    requests.post(url, data=payload)


if __name__ == "__main__":
    for url, payload in generate_call():
        call_endpoint(url, payload)  

this will generate 4 calls like this:

http://some/end/point {'key1': '1', 'key2': '2'}
http://some/end/point {'key3': '4', 'key4': '5'}
http://some/other/end/point {'key1': '1', 'key2': '2'}
http://some/other/end/point {'key3': '4', 'key4': '5'}

and if you want to use put them in a yaml or json file and load it in the request_map variable. You don't need do any other operation on them. like:

request_map = yaml.safe_load(open('request_map.yaml'))

or:

request_map = json.load(open('request_map.json'))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

what the best tune for generate code in python to mariadb?

From Dev

What is the python docstring convention for returning multiple values?

From Dev

What is the Best Practice to Generate Sequential Code in PostgreSQL

From Dev

Using multiple databases for different endpoints

From Dev

Best practice for calling two different endpoints for a widget?

From Dev

How to manage payload during interaction with multiple endpoints in a SI flow

From Dev

What is the best practice for creating payload for a request?

From Dev

What is the best approach in python: multiple OR or IN in if statement?

From Dev

Best practices for multiple inheritance in this Python code

From Dev

Naming convention of API endpoints

From Java

What's the best way to generate random strings of a specific length in Python?

From Java

What's the best way to generate random strings of a specific length in Python?

From Python

What's the best way to generate unique permutations in Python?

From Dev

How to generate payload with python for buffer overflow?

From Java

Multiple endpoints with the same path but with different @RequestParam values

From Dev

Multiple endpoints to expose different views of the same resource

From Java

Multiple user details services for different endpoints

From Dev

Multiple Endpoints in same controller with different parameter

From Dev

What is the convention for separating multiple words on a flask route?

From Dev

What is the best way to code HTML table with multiple headers in between rows?

From Dev

Can you import the same module in multiple classes (What is the accepted convention in Python)?

From Dev

Best way/practices to generate multiple result sets in stored procedure to different tabs in Excel

From Dev

python 3, what is the best way to run different loops simultaneously?

From Dev

Python code to multiple JSON files with different names

From Dev

Best practice for plotting multiple streams of data on different graphs in Python & pyqtgraph

From Dev

Why does same code in C++ and Python generate different outputs?

From Dev

What is the best convention for splitting up screens in React Native?

From Dev

What is the best convention to store colors and icons in my Flutter app?

From Dev

What is the best way to have different configurations compile with different preprocessor flags on the same source code?

Related Related

  1. 1

    what the best tune for generate code in python to mariadb?

  2. 2

    What is the python docstring convention for returning multiple values?

  3. 3

    What is the Best Practice to Generate Sequential Code in PostgreSQL

  4. 4

    Using multiple databases for different endpoints

  5. 5

    Best practice for calling two different endpoints for a widget?

  6. 6

    How to manage payload during interaction with multiple endpoints in a SI flow

  7. 7

    What is the best practice for creating payload for a request?

  8. 8

    What is the best approach in python: multiple OR or IN in if statement?

  9. 9

    Best practices for multiple inheritance in this Python code

  10. 10

    Naming convention of API endpoints

  11. 11

    What's the best way to generate random strings of a specific length in Python?

  12. 12

    What's the best way to generate random strings of a specific length in Python?

  13. 13

    What's the best way to generate unique permutations in Python?

  14. 14

    How to generate payload with python for buffer overflow?

  15. 15

    Multiple endpoints with the same path but with different @RequestParam values

  16. 16

    Multiple endpoints to expose different views of the same resource

  17. 17

    Multiple user details services for different endpoints

  18. 18

    Multiple Endpoints in same controller with different parameter

  19. 19

    What is the convention for separating multiple words on a flask route?

  20. 20

    What is the best way to code HTML table with multiple headers in between rows?

  21. 21

    Can you import the same module in multiple classes (What is the accepted convention in Python)?

  22. 22

    Best way/practices to generate multiple result sets in stored procedure to different tabs in Excel

  23. 23

    python 3, what is the best way to run different loops simultaneously?

  24. 24

    Python code to multiple JSON files with different names

  25. 25

    Best practice for plotting multiple streams of data on different graphs in Python & pyqtgraph

  26. 26

    Why does same code in C++ and Python generate different outputs?

  27. 27

    What is the best convention for splitting up screens in React Native?

  28. 28

    What is the best convention to store colors and icons in my Flutter app?

  29. 29

    What is the best way to have different configurations compile with different preprocessor flags on the same source code?

HotTag

Archive