adrian-soto / mock-request

Mock an API request and obtain a pre-saved response object
79 stars 27 forks source link

mock-request

CircleCI codecov

Mock API requests with and obtain a pre-saved response object.

With this package you can mock the behavior of the requests library. You can write code that looks identical to the one you'd be using in an actual request, but which in reality loads pre-saved response objects with matching request parameters.

Requirements

Installation

This package is not yet available in PyPy, but you can still install it using pip. First clone the repo, navigate into the repo directory, and then issue

$ pip install mock_request --user

in your terminal.

Usage

Until a more complete of examples is available, you can get a good sense of how to use this package by working through the mock-request/tests/files/yelp-api-request.ipynb notebook.

History

This package was originally created for online courses, where it became necessary for several reasons:

  1. hitting an API thousands of time a day from the same IP and/or with the same authentication will hit the daily download limit
  2. the online course will not rely on the API servers, and will continue to work even if they're down or if the API changes
  3. it shortens the execution time of online exercises

However, this package can be used for more generic situations. One good example would be to write unit tests for code performing API requests but without actually executing the requests.

Contributing

I'll be happy (and proud!) if you'd like to contribute to this package. Feel free to open an issue explaining the fix/improvement/expansion you'd like to develop and let's discuss it.