-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #460 from OpenFn/http-template
New template using HTTP utils
- Loading branch information
Showing
9 changed files
with
229 additions
and
144 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,47 @@ | ||
// TODO: Determine how to authenticate for the target API | ||
function makeAuthHeader(auth) { | ||
const { username, password } = auth; | ||
const buff = Buffer.from(`${username}:${password}`); | ||
const credentials = buff.toString('base64'); | ||
return { Authorization: `Basic ${credentials}` }; | ||
} | ||
|
||
export async function request(url, options) { | ||
const { auth } = options; | ||
delete options.auth; | ||
|
||
const response = await fetch(url, { | ||
...options, | ||
/** | ||
* If you have any helper functions which are NOT operations, | ||
* you should add them here | ||
*/ | ||
|
||
import { | ||
request as commonRequest, | ||
makeBasicAuthHeader, | ||
} from '@openfn/language-common/util'; | ||
|
||
// This helper function will call out to the backend service | ||
// And add authorisation headers | ||
export const request = (state, method, path, data) => { | ||
// TODO This example adds basic auth from config data | ||
// you may need to support other auth strategies | ||
const { baseUrl, username, password } = state.configuration; | ||
const headers = makeBasicAuthHeader(username, password); | ||
|
||
// TODO You can define custom error messages here | ||
// The request function will throw if it receives | ||
// an error code (<=400), terminating the workflow | ||
const errors = { | ||
404: 'Page not found', | ||
}; | ||
|
||
const options = { | ||
// Append data to the request body | ||
body: data, | ||
|
||
// You can dd extra headers here if yout want to | ||
headers: { | ||
...headers, | ||
'content-type': 'application/json', | ||
...makeAuthHeader(auth), | ||
}, | ||
}); | ||
|
||
// TODO: Handle errors based on the behavior of the target API | ||
if (response.status === 404) { | ||
throw new Error('Page not found'); | ||
} else if (response.status === 500) { | ||
throw new Error('Server error'); | ||
} else if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
// TODO: Extract data from response based on the target API | ||
const data = await response.json(); | ||
return data; | ||
} | ||
|
||
// Force the response to be parsed as JSON | ||
parseAs: 'json', | ||
|
||
// Include the error map | ||
errors, | ||
}; | ||
|
||
// Build the url base on the baseUrl in config and the path provided | ||
const url = `${baseUrl}/api/${path}`; | ||
// Make the actual request | ||
return commonRequest(method, url, options); | ||
}; |
Oops, something went wrong.