Created
July 12, 2022 20:03
-
-
Save Crocoblock/5d6c1cdfedc9594b709d7e0c751eba9c to your computer and use it in GitHub Desktop.
JetEngine REST API modify request arguments (e. g. when you need to set more than one header for the request)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Some APIs required more than one header to be sent, | |
* or they need to give out some token first, and only after that to get the actual info | |
* Here we show only example with setting additional header, | |
* but you may run some code to do what you need in a specific case | |
* | |
* Here we have an API, which requires setting not pnly Authorization token (which may be set in REST API endpoiny options) | |
* but also a header with the version - that was a real-life example from our user | |
* | |
* To add header - add element to $args['headers'], but only when the request goes to a certain API | |
* (you may check it by URL) | |
* | |
* $request is a Request object, which has the following useful in similar cases methods: | |
* get_url() - get the URL to which the request is sent | |
* get_endpoint() - to get edpoint settings, which is an array of: | |
* name - endpoint name | |
* url - endpoint url | |
* other endpoint settings, choosed on its setup | |
* | |
* Here, get_url() is used to check if the request goes to Notion API | |
*/ | |
add_filter( 'jet-engine/rest-api-listings/request/args', 'modify_rest_api_args', 0, 2 ); | |
function modify_rest_api_args( $args, $request ) { | |
if ( strpos( $request->get_url(), 'api.notion.com' ) !== false ) { | |
$args['headers']['Notion-Version'] = '2021-08-16'; | |
} | |
return $args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment