Configure your APIs¶
It configures the detail settings of PyFake-API-Server by YAML syntax, and must have either a .yml
or .yaml
file
extension. If you're new to YAML and want to learn more, see "Learn YAML in Y minutes."
What settings are necessary?¶
This question also means what an API need? In generally, the necessary conditions it needs as following:
Therefore, above properties also are the options we must configure by YAML syntax for mocking API.
Configure API¶
First of all, create a file with YAML extension .yaml
.
All the API settings need to be configured under key mocked_apis
and it would be a list of key-value map type elements.
The key is the API name and the value is the detail settings of the API.
Before start to configure, let's give a usage scenario:
Fake an API which accepts GET method without any parameter, and it would return string value
This is Foo home API.
. We could sort out the requirement as 3 conditions as below:
- URL path: /foo
- HTTP request method: GET
- HTTP response data: This is Foo home API.
Let's name the API as foo_home
:
URL¶
In the API setting section, it uses key url
to configure the URL path. So we could set /foo
directly here:
We have done the first condition! Let's quickly set the next one setting about HTTP.
- URL path: /foo
- HTTP request method: GET
- HTTP response data: This is Foo home API.
HTTP¶
About the HTTP settings, it has multiple options could configure. So key http
manages all settings about HTTP settings
and more details under it.
In basically, there are 2 options we must configure: HTTP request and HTTP response. It also has 2 keys request
and response
to manage them.
Request¶
All the HTTP request settings would be managed under key http.request
. And right now, we just need to set one attribute
method
about the HTTP method:
We have done the second condition! It leaves only one condition about HTTP response.
- URL path: /foo
- HTTP request method: GET
- HTTP response data: This is Foo home API.
Response¶
All the HTTP response settings would be managed under key http.response
. About the HTTP response configuration, it has
multiple strategies for setting the return
value format. For easily and quickly demonstrating the HTTP response setting, let's use string
strategy right now.
mocked_apis:
foo_home:
url: '/foo'
http:
request:
method: 'GET'
response:
strategy: string
value: 'This is Foo home API.'
Congratulation! We finish the configuration for mocking API, and we could try to set up the web server to provide the mocking service!
- URL path: /foo
- HTTP request method: GET
- HTTP response data: This is Foo home API.
Check configuration validation¶
If you're meticulous in configuring and developing, PyFake-API-Server also provide a command line to help you check your configuration validation:
It would check everywhere of configuration and make sure your configuration is valid for running.