-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: set is_bff_enabled
custom attribute via logging service; create abstraction to make requests to BFF API endpoints with logError
/logInfo
#1234
Merged
adamstankiewicz
merged 7 commits into
master
from
ags/bff-request-abstraction-observability
Dec 10, 2024
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2f5ee79
feat: set custom logging attribute for is_bff_enabled
adamstankiewicz b8b2ccf
feat: create helper function to make BFF requests with logError and l…
adamstankiewicz fb57c12
chore: docstring update
adamstankiewicz 71d9685
refactor: move bffs service file to TS
adamstankiewicz 7d890ab
chore: tests
adamstankiewicz af2ad79
chore: cover addtl case in tests
adamstankiewicz 93ebcc7
refactor: annotate enterpriseCustomerFactory with Types.EnterpriseCus…
adamstankiewicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { getConfig } from '@edx/frontend-platform/config'; | ||
import { logError, logInfo } from '@edx/frontend-platform/logging'; | ||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
import { camelCaseObject, snakeCaseObject } from '@edx/frontend-platform/utils'; | ||
|
||
export const baseLearnerBFFResponse = { | ||
enterpriseCustomerUserSubsidies: { | ||
subscriptions: { | ||
customerAgreement: {}, | ||
subscriptionLicenses: [], | ||
subscriptionLicensesByStatus: {}, | ||
}, | ||
}, | ||
errors: [], | ||
warnings: [], | ||
}; | ||
|
||
export const learnerDashboardBFFResponse = { | ||
...baseLearnerBFFResponse, | ||
enterpriseCourseEnrollments: [], | ||
}; | ||
|
||
/** | ||
* Log any errors and warnings from the BFF response. | ||
* @param {Object} args | ||
* @param {String} args.url - The URL of the BFF API endpoint. | ||
* @param {Object} args.response - The camelCased response from the BFF API endpoint. | ||
*/ | ||
export function logErrorsAndWarningsFromBFFResponse({ url, response }) { | ||
response.errors.forEach((error) => { | ||
logError(`BFF Error (${url}): ${error.developerMessage}`); | ||
}); | ||
response.warnings.forEach((warning) => { | ||
logInfo(`BFF Warning (${url}): ${warning.developerMessage}`); | ||
}); | ||
} | ||
|
||
/** | ||
* Make a request to the specified BFF API endpoint. | ||
* @param {Object} args | ||
* @param {String} args.url - The URL of the BFF API endpoint. | ||
* @param {Object} args.defaultResponse - The default response to return if unable to resolve the request. | ||
* @param {Object} args.options - The options to pass to the BFF API endpoint. | ||
* @param {String} [args.options.enterpriseId] - The UUID of the enterprise customer. | ||
* @param {String} [args.options.enterpriseSlug] - The slug of the enterprise customer. | ||
* @returns {Promise<Object>} - The response from the BFF. | ||
*/ | ||
export async function makeBFFRequest({ | ||
url, | ||
defaultResponse, | ||
options = {} as Types.BFFRequestOptions, | ||
}) { | ||
const { enterpriseId, enterpriseSlug, ...optionsRest } = options; | ||
const snakeCaseOptionsRest = optionsRest ? snakeCaseObject(optionsRest) : {}; | ||
|
||
// If neither enterpriseId or enterpriseSlug is provided, return the default response. | ||
if (!enterpriseId && !enterpriseSlug) { | ||
return defaultResponse; | ||
} | ||
|
||
try { | ||
const params = { | ||
enterprise_customer_uuid: enterpriseId, | ||
enterprise_customer_slug: enterpriseSlug, | ||
...snakeCaseOptionsRest, | ||
}; | ||
|
||
// Make request to BFF. | ||
const result = await getAuthenticatedHttpClient().post(url, params); | ||
const response = camelCaseObject(result.data); | ||
|
||
// Log any errors and warnings from the BFF response. | ||
logErrorsAndWarningsFromBFFResponse({ url, response }); | ||
|
||
// Return the response from the BFF. | ||
return response; | ||
} catch (error) { | ||
logError(error); | ||
return defaultResponse; | ||
} | ||
} | ||
|
||
export interface EnterpriseLearnerDashboardOptions { | ||
enterpriseId?: string; | ||
enterpriseSlug?: string; | ||
} | ||
|
||
/** | ||
* Fetch the learner dashboard BFF API for the specified enterprise customer. | ||
* @param {Object} args | ||
* @param {String} [args.enterpriseId] - The UUID of the enterprise customer. | ||
* @param {String} [args.enterpriseSlug] - The slug of the enterprise customer. | ||
* @returns {Promise<Object>} - The learner dashboard metadata. | ||
*/ | ||
export async function fetchEnterpriseLearnerDashboard({ | ||
enterpriseId, | ||
enterpriseSlug, | ||
}: EnterpriseLearnerDashboardOptions) { | ||
const options = {} as Types.BFFRequestOptions; | ||
if (enterpriseId) { | ||
options.enterpriseId = enterpriseId; | ||
} | ||
if (enterpriseSlug) { | ||
options.enterpriseSlug = enterpriseSlug; | ||
} | ||
return makeBFFRequest({ | ||
url: `${getConfig().ENTERPRISE_ACCESS_BASE_URL}/api/v1/bffs/learner/dashboard/`, | ||
defaultResponse: learnerDashboardBFFResponse, | ||
options, | ||
}); | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[inform] Moved from
bffs.js
->bffs.ts