{"id":19413094,"url":"https://github.com/joknarf/fakeapi","last_synced_at":"2026-03-02T07:31:58.083Z","repository":{"id":65189670,"uuid":"586649955","full_name":"joknarf/fakeapi","owner":"joknarf","description":"Faking/Mocking API Rest Call requests","archived":false,"fork":false,"pushed_at":"2024-09-19T16:30:29.000Z","size":83,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-25T11:03:56.794Z","etag":null,"topics":["api","fakeapi","http","mock","python","rest","server","testing","unittest"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joknarf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-08T21:26:43.000Z","updated_at":"2024-09-19T16:30:32.000Z","dependencies_parsed_at":"2024-11-10T12:42:07.698Z","dependency_job_id":null,"html_url":"https://github.com/joknarf/fakeapi","commit_stats":{"total_commits":66,"total_committers":2,"mean_commits":33.0,"dds":0.2727272727272727,"last_synced_commit":"f838731ac227ef70895ef96177e0bfb1cd1c1639"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/joknarf/fakeapi","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joknarf%2Ffakeapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joknarf%2Ffakeapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joknarf%2Ffakeapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joknarf%2Ffakeapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joknarf","download_url":"https://codeload.github.com/joknarf/fakeapi/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joknarf%2Ffakeapi/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29994997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-02T01:47:34.672Z","status":"online","status_checked_at":"2026-03-02T02:00:07.342Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","fakeapi","http","mock","python","rest","server","testing","unittest"],"created_at":"2024-11-10T12:30:45.354Z","updated_at":"2026-03-02T07:31:58.069Z","avatar_url":"https://github.com/joknarf.png","language":"Python","readme":"[![Travis CI](https://app.travis-ci.com/joknarf/fakeapi.svg?token=ERTmZfyqzDZTSfQwkAfa\u0026branch=main)](https://travis-ci.com/github/joknarf/fakeapi)\n[![Codecov](https://codecov.io/github/joknarf/fakeapi/coverage.svg?branch=main)](https://codecov.io/gh/joknarf/fakeapi)\n[![Upload Python Package](https://github.com/joknarf/fakeapi/actions/workflows/python-publish.yml/badge.svg)](https://github.com/joknarf/fakeapi/actions?query=workflow%3A%22Upload+Python+Package%22)\n[![Pypi version](https://img.shields.io/pypi/v/fakeapi.svg)](https://pypi.org/project/fakeapi/)\n[![Downloads](https://pepy.tech/badge/fakeapi)](https://pepy.tech/project/fakeapi)\n[![Python versions](https://img.shields.io/badge/python-3.6-blue.svg)](https://shields.io/)\n[![Licence](https://img.shields.io/badge/licence-MIT-blue.svg)](https://shields.io/)\n\n# fakeapi\nFaking/Mocking API Rest Call requests\n\nFaking API calls using static fixtures with FakeAPI class.  \nMocking API calls using FakeAPI get/post/patch/delete methods.  \nCreate HTTP server Rest API with a single json response file.\n\n# FakeAPI class\n\nFakes http requests calls (get/post/put/patch/delete).  \nInstead of doing http calls to urls, FakeAPI class will returns response with data from url dict data or json file.  \nCan be used during development of Application that must use 3rd party API without actually calling the API, but using static tests sets data for url calls.\n\nAnother purpose is to use FakeAPI class to mock http requests when doing Unit testing of application that is using 3rd party API calls (the tests won't actually call the 3rd party API that is not to be tested)\n\nFakeAPI class is also able to act as a HTTP Rest API server using a single json description of responses to calls.\n\n## Quick examples:\n\n### Start http server using `python -m fakeapi` responding to 'GET http://localhost:8080/api'\n```shell\n$ python -m fakeapi \u003c\u003c\u003c '{ \"GET http://localhost:8080/api\": { \"data\": { \"message\": \"Call successfull\" }}}'\nStarting http server : http://localhost:8080\n127.0.0.1 - - [15/Jan/2023 13:00:20] GET localhost:8080/api\nfakeapi: Calling: GET http://localhost:8080/api\n127.0.0.1 - - [15/Jan/2023 13:00:20] \"GET /api HTTP/1.1\" 200 -\n```\n\nOn Client side:  \n```shell\n$ curl http://localhost:8080/api\n{\"message\": \"Call successfull\"}\n```\n\n### Using FakeAPI class\n```python\n\u003e\u003e\u003e from fakeapi import FakeAPI\n\u003e\u003e\u003e api = FakeAPI({\n  'GET http://localhost/api': {\n    'status_code': 200,\n    'data': {\n      'message': 'Call successfull'\n    }\n  }\n})\n\u003e\u003e\u003e response = api.get('http://localhost/api')\n\u003e\u003e\u003e response.status_code\n200\n\u003e\u003e\u003e response.json()\n{'message': 'Call successfull'}\n\u003e\u003e\u003e api.http_server()\nStarting http server : http://localhost:8080\n...\n```\n\nFakeAPI class can easily mock requests calls in unittest.  \nUsefull to test Application that is calling 3rd party API that is not to be tested.\n\u003e```python\n\u003emycli.py:\n\u003eimport requests\n\u003e\n\u003edef call_api():\n\u003e  response = requests.get('http://localhost/api')\n\u003e  return response.json()\n\u003e```\n\u003e```python\n\u003etest_mycli.py:\n\u003eimport unittest, mycli\n\u003efrom fakeapi import FakeAPI\n\u003e\n\u003eclass TestMyCLI(unittest.TestCase):\n\u003e  fakeapi = FakeAPI({'GET http://localhost/api': {'data': {'message': 'Call successfull'}}})\n\u003e  def setUp(self):\n\u003e    # mock 'mycli.requests' get/post/patch/put/delete calls to fakeapi\n\u003e    self.mocks = self.fakeapi.mock_module(self, 'mycli.requests')\n\u003e  \n\u003e  def test_mycli(self):\n\u003e    data = mycli.call_api()   # requests calls are mocked to fakeAPI\n\u003e    self.mocks.get.assert_called_with('http://localhost/api')\n\u003e    print(data)\n\u003e\n\u003eif __name__ == \"__main__\":\n\u003e    unittest.main(failfast=True, verbosity=2)\n\u003e```\n\u003e```python\n\u003e$ python test_mycli.py\n\u003etest_mycli (__main__.TestMyCLI) ... {'message': 'Call successfull'}\n\u003eok\n\u003e\n\u003e----------------------------------------------------------------------\n\u003eRan 1 test in 0.002s\n\u003e\n\u003eOK\n\u003e```\n\n## fakeapi server usage\n\n`python -m fakeapi` is starting an http server responding to http calls defined in json description.\njson url description :\n\n```json\n{\n  \"\u003cMETHOD\u003e \u003curl\u003e\": {\n    \"status_code\": \u003cstatus_code\u003e,\n    \"data\": \u003curl_data\u003e\n  },...\n}\n```\n\n```\n$ python -m fakeapi -h\nusage: python -m fakeapi [-h] [-s SERVER] [-p PORT] [-P PREFIX] [jsonfile]\n\npositional arguments:\n  jsonfile              Json file for FakeAPI\n\noptions:\n  -h, --help            show this help message and exit\n  -s SERVER, --server SERVER\n                        HTTP server address\n  -p PORT, --port PORT  HTTP server port\n  -P PREFIX, --prefix PREFIX\n                        HTTP prefix (http://server:port)\n```\n\n## FakeAPI class Usage\n\nFakeAPI class defines the 5 methods:\n* get\n* post\n* put\n* patch\n* delete\n\nFakeAPI provides the mocking methods to be used in unittest.TestCase.setUp():\n* mock_module(test_case: TestCase, module: str)\n* mock_class(apicli: Object)\n\n## Mapping Static data to urls calls\n\nInstead of calling 3rd party API, FakeAPI will use static data (from dict or json files). \nstatic data can be defined several ways :\n* passing FakeAPI url_config parameter with data: \n  * `api = FakeAPI(url_config={'METHOD url':{'data':{...}},...})`\n* passing FakeAPI url_json parameter with json file containing url_config data: \n  * `api = FakeAPI(url_json='url_config.json')`\n* FakeAPI.url_config property can be modified after creation\n\n## Using url_config\n\nEach different url calls can be configured in url_config to provide specific status_code or data.\n\nProviding data in url_config for url\n```json\n{\n  \"\u003cMETHOD\u003e \u003curl\u003e\": {\n    \"status_code\": \u003cstatus_code\u003e,\n    \"data\": \u003curl_data\u003e\n  },...\n}\n```\n* `\u003cMETHOD\u003e      `: http method : GET/POST/PUT/PATCH/DELETE\n* `\u003curl\u003e         `: full url that is called (with query string)\n* `\u003cstatus_code\u003e `: http code to return in repsonse (200/201/404/500...)\n* `\u003curl_data\u003e    `: data to retrieve for url call on method.\n\nWhen a request method occurs on `\u003curl\u003e` if the key `\u003cMETHOD\u003e \u003curl\u003e` has a entry in url_config, returns 'data'/'status_code' if defined.  \n\n## FakeAPI returns FakeResponse or json\n\nFakeAPI methods by default returns `FakeResponse` with following :\n* status_code = 200/201 (for post) or status_code defined in url_config\n* json() : return json from json file corresponding to METHOD url\n* url : url called\n* content : byte text\n* text : text\n* ok : True if status_code \u003c400\n\n`fakeapi = FakeAPI(returns='json')` is to be used to return directly 'json', instead of response.  \nTo be used with api-client module class APIClient(response_handler=JsonResponseHandler) as get/post/patch/delete returns directly json() from response.\n\n\n## Mocking http requests using FakeAPI\n\nMocking can be done using mock_module or mock_class methods in unittest.TestCase.setUp() method.\n\nExample to mock requests with api-client APIClient():\n```python\nmycli.py:\nfrom apiclient import APIClient\nclass MyClient(APIClient):\n  def call_api(self):\n    return self.get('http://localhost/api').json()\n```\n\n```python\nimport unittest\nfrom fakeapi import FakeAPI\nfrom mycli import MyClient\nclass UnitTest(unittest.TestCase):\n    \"\"\" Unit Testing mocking MyClient get/post/put/patch \"\"\"\n    fakeapi = FakeAPI({'GET http://localhost/api': {'data': {'message': 'Call successfull'}}})\n    apicli = MyClient()\n\n    def setUp(self):\n        \"\"\" Mock API calls \"\"\"\n        self.apicli = self.fakeapi.mock_class(self.apicli)\n\n    def test_call_api(self):\n        \"\"\" test_call_api \"\"\"\n        data = self.apicli.call_api()\n        self.apicli.get.assert_called_with('http://localhost/api')\n        print(data)\n```\n\n## Generating test sets \n\nTo have url_config corresponding to API calls, you can generate url_config from real calls to API, \nthen use the result in your tests.\n\nThe urlconfighelper module can help, as can create a class derived from your class,\nsupercharging the get/post/put/pach/delete method to generate url_config for all calls.\n\nYou can then save the url_config containing all calls you made to a json file to be used as url_config in tests.\n\nExample:\n```python\n\"\"\" Generate url_config for tests from MyClient real API calls \"\"\"\nimport json\nfrom mycli import MyClient\nfrom fakeapi import UrlConfigHelper\n\napi = UrlConfigHelper(MyClient)\napi.call_api()    # make calls to the API and updates api.url_config\napi.save_urlconfig('mytests.json')\nprint(json.dumps(api.url_config, indent=2))\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoknarf%2Ffakeapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoknarf%2Ffakeapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoknarf%2Ffakeapi/lists"}