-
Notifications
You must be signed in to change notification settings - Fork 83
feat: Add ODP GraphQL AP Interface #778
New issue
Have a question about this project? Sign up for a free GitHub a 10000 ccount 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
Merged
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
b5fb7e6
WIP: ODP client shell
mikechu-optimizely 5dfb285
Initial implementation of GraphQLManager
mikechu-optimizely f0e4af8
Rename files
mikechu-optimizely d32b9ca
WIP schema definition for ODP
mikechu-optimizely eba32d6
Merge branch 'master' into mike/ats-graphql
mikechu-optimizely 9e7cbe7
Remove renamed files
mikechu-optimizely 1dedf29
ODP response json schema
mikechu-optimizely 68327ec
Add missing props to ODP response schema
mikechu-optimizely 0d3818d
Adjust schema validator to handle injected schemas
mikechu-optimizely d6ce18a
Implement schema validation for ODP JSON response
mikechu-optimizely 6ee6cb9
WIP Building ODP clients for browser and node
mikechu-optimizely c132826
Use Axios to support browser and node ODP calls
mikechu-optimizely a903a70
WIP GraphQLManager testing
mikechu-optimizely 2c7b898
Corrected ODP response schema
mikechu-optimizely 4162506
Handle all non-200 HTTP statuses
mikechu-optimizely 3700344
WIP ODP client test starts
mikechu-optimizely 562581b
Add ODP tests
mikechu-optimizely 3ffd5b1
Switch to using toHaveLength
mikechu-optimizely f76e131
Fix tests
mikechu-optimizely 2f5af44
Exchange Axios for fetch
mikechu-optimizely 808daac
Resolve code review requests
mikechu-optimizely 5737d21
More code review changes
mikechu-optimizely fa576b2
Refactors to remove warns
mikechu-optimizely 8e409a8
Pull out fetch & put axios back in
mikechu-optimizely 99e644b
Remove Axios again
mikechu-optimizely 3c201ab
Remove axios from code (WIP tests failing)
mikechu-optimizely 766eb87
WIP (tests failing) Browser vs Node request handling
mikechu-optimizely 45d4ce1
WIP still: cleaned code a bit; tests need attn
mikechu-optimizely f8557f8
Fix enum string
mikechu-optimizely 4214b91
Merge branch 'master' into mike/ats-graphql
mikechu-optimizely c4a873b
Trying to get timeout test working
mikechu-optimizely dba12f3
ODP tests
mikechu-optimizely 738f9ec
WIP nodeRequestHandler tests
mikechu-optimizely 807e257
Handle node request timeouts with tests
mikechu-optimizely c3c8540
BroswerRequestHandler tests
mikechu-optimizely c0e427b
Document RequestHandlers
mikechu-optimizely b8ba347
ODP Client & GraphQL jsdoc
mikechu-optimizely ef8dc52
Remove `any` in `catch`
mikechu-optimizely e825064
Revert "Remove `any` in `catch`"
mikechu-optimizely a07c905
Update packages/optimizely-sdk/lib/plugins/odp/graphql_manager.ts
mikechu-optimizely 6134d84
Update packages/optimizely-sdk/lib/plugins/odp/graphql_manager.ts
mikechu-optimizely 5d0ebb2
Update packages/optimizely-sdk/lib/plugins/odp/query_segments_paramet…
mikechu-optimizely 8308a53
Code review changes
mikechu-optimizely b4ed7bd
Remove throwError
mikechu-optimizely File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
10000
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
137 changes: 137 additions & 0 deletions
137
packages/optimizely-sdk/lib/plugins/odp/graphql_manager.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/** | ||
* Copyright 2022, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { ErrorHandler, LogHandler, LogLevel } from '../../modules/logging'; | ||
import { Response } from './odp_types'; | ||
import { IOdpClient, OdpClient } from './odp_client'; | ||
import { validate } from '../../utils/json_schema_validator'; | ||
import { OdpResponseSchema } from './odp_response_schema'; | ||
import { QuerySegmentsParameters } from './query_segments_parameters'; | ||
import { RequestHandlerFactory } from '../../utils/http_request_handler/request_handler_factory'; | ||
|
||
/** | ||
* Expected value for a qualified/valid segment | ||
*/ | ||
const QUALIFIED = 'qualified'; | ||
/** | ||
* Return value when no valid segments found | ||
*/ | ||
const EMPTY_SEGMENTS_COLLECTION: string[] = []; | ||
/** | ||
* Return value for scenarios with no valid JSON | ||
*/ | ||
const EMPTY_JSON_RESPONSE = null; | ||
|
||
/** | ||
* Manager for communicating with the Optimizely Data Platform GraphQL endpoint | ||
*/ | ||
export interface IGraphQLManager { | ||
fetchSegments(apiKey: string, apiHost: string, userKey: string, userValue: string, segmentsToCheck: string[]): Promise<string[]>; | ||
} | ||
|
||
/** | ||
* Concrete implementation for communicating with the Optimizely Data Platform GraphQL endpoint | ||
*/ | ||
export class GraphqlManager implements IGraphQLManager { | ||
private readonly _errorHandler: ErrorHandler; | ||
private readonly _logger: LogHandler; | ||
private readonly _odpClient: IOdpClient; | ||
|
||
/** | ||
* Retrieves the audience segments from the Optimizely Data Platform (ODP) | ||
* @param errorHandler Handler to record exceptions | ||
* @param logger Collect and record events/errors for this GraphQL implementation | ||
* @param client Client to use to send queries to ODP | ||
*/ | ||
constructor(errorHandler: ErrorHandler, logger: LogHandler, client?: IOdpClient) { | ||
this._errorHandler = errorHandler; | ||
this._logger = logger; | ||
|
||
this._odpClient = client ?? new OdpClient(this._errorHandler, | ||
this._logger, | ||
RequestHandlerFactory.createHandler(this._logger)); | ||
} | ||
|
||
/** | ||
* Retrieves the audience segments from ODP | ||
* @param apiKey ODP public key | ||
* @param apiHost Fully-qualified URL of ODP | ||
* @param userKey 'vuid' or 'fs_user_id key' | ||
* @param userValue Associated value to query for the user key | ||
* @param segmentsToCheck Audience segments to check for experiment inclusion | ||
*/ | ||
public async fetchSegments(apiKey: string, apiHost: string, userKey: string, userValue: string, segmentsToCheck: string[]): Promise<string[]> { | ||
const parameters = new QuerySegmentsParameters({ | ||
apiKey, | ||
apiHost, | ||
userKey, | ||
userValue, | ||
1C6A segmentsToCheck, | ||
}); | ||
const segmentsResponse = await this._odpClient.querySegments(parameters); | ||
if (!segmentsResponse) { | ||
this._logger.log(LogLevel.ERROR, 'Audience segments fetch failed (network error)'); | ||
return EMPTY_SEGMENTS_COLLECTION; | ||
} | ||
|
||
const parsedSegments = this.parseSegmentsResponseJson(segmentsResponse); | ||
if (!parsedSegments) { | ||
this._logger.log(LogLevel.ERROR, 'Audience segments fetch failed (decode error)'); | ||
return EMPTY_SEGMENTS_COLLECTION; | ||
} | ||
|
||
if (parsedSegments.errors?.length > 0) { | ||
const errors = parsedSegments.errors.map((e) => e.message).join('; '); | ||
|
||
this._logger.log(LogLevel.WARNING, `Audience segments fetch failed (${errors})`); | ||
|
||
return EMPTY_SEGMENTS_COLLECTION; | ||
} | ||
|
||
const edges = parsedSegments?.data?.customer?.audiences?.edges; | ||
if (!edges) { | ||
this._logger.log(LogLevel.WARNING, 'Audience segments fetch failed (decode error)'); | ||
return EMPTY_SEGMENTS_COLLECTION; | ||
} | ||
|
||
return edges.filter(edge => edge.node.state == QUALIFIED).map(edge => edge.node.name); | ||
} | ||
|
||
/** | ||
* Parses JSON response | ||
* @param jsonResponse JSON response from ODP | ||
* @private | ||
* @returns Response Strongly-typed ODP Response object | ||
*/ | ||
private parseSegmentsResponseJson(jsonResponse: string): Response | null { | ||
let jsonObject = {}; | ||
|
||
try { | ||
jsonObject = JSON.parse(jsonResponse); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (error: any) { | ||
this._errorHandler.handleError(error); | ||
this._logger.log(LogLevel.ERROR, 'Attempted to parse invalid segment response JSON.'); | ||
return EMPTY_JSON_RESPONSE; | ||
} | ||
|
||
if (validate(jsonObject, OdpResponseSchema, false)) { | ||
return jsonObject as Response; | ||
} | ||
|
||
return EMPTY_JSON_RESPONSE; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.