8000 feat(segment-manager): Adds implementation for ODPSegmentManager by yasirfolio3 · Pull Request #353 · optimizely/go-sdk · GitHub
[go: up one dir, main page]

Skip to content

feat(segment-manager): Adds implementation for ODPSegmentManager #353

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

Merged
merged 24 commits into from
Nov 16, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixes.
  • Loading branch information
yasirfolio3 committed Oct 18, 2022
commit d76e74c76abe2ed5eb31a179920a9d9dd3880f69
49 changes: 11 additions & 38 deletions pkg/odp/segment_api_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ func NewSegmentAPIManager(sdkKey string, requester utils.Requester, logger loggi
func (s *SegmentAPIManager) FetchSegments(apiKey, apiHost, userKey, userValue string, segmentsToCheck []string, handler func([]string, error)) {

// Creating query for odp request
query := s.createRequestQuery(userKey, userValue, segmentsToCheck)
requestQuery := s.createRequestQuery(userKey, userValue, segmentsToCheck)

// Creating request
apiEndpoint := apiHost + "/v3/graphql"
headers := []utils.Header{{Name: "Content-Type", Value: "application/json"}, {Name: "x-api-key", Value: apiKey}}

// handling edge cases
response, _, statusCode, err := s.requester.Post(apiEndpoint, map[string]string{"query": query}, headers...)
response, _, statusCode, err := s.requester.Post(apiEndpoint, requestQuery, headers...)
if response == nil {
if err != nil {
handler(nil, fmt.Errorf(fetchSegmentsFailedError, "invalid response"))
Expand Down Expand Up @@ -206,43 +206,16 @@ func (s *SegmentAPIManager) FetchSegments(apiKey, apiHost, userKey, userValue st
}

// Creates graphql query
func (s SegmentAPIManager) createRequestQuery(userKey, userValue string, segmentsToCheck []string) string {
return fmt.Sprintf(`query %s`, s.getCustomerFilter(userKey, userValue, segmentsToCheck))
}

// Creates filter for customer
func (s SegmentAPIManager) getCustomerFilter(userKey, userValue string, segmentsToCheck []string) string {
return fmt.Sprintf(`{customer(%s: %q) %s}`, userKey, userValue, s.getAudiencesFilter(segmentsToCheck))
}

// Creates filter for audiences
func (s SegmentAPIManager) getAudiencesFilter(segmentsToCheck []string) string {
subsetFilter := s.makeSubsetFilter(segmentsToCheck)
return fmt.Sprintf(`{audiences%s %s}`, subsetFilter, s.getEdgesFilter())
}

// Creates filter for edges
func (s SegmentAPIManager) getEdgesFilter() string {
return fmt.Sprintf(`{edges %s}`, s.getNodesFilter())
}

// Creates filter for nodes
func (s SegmentAPIManager) getNodesFilter() string {
return `{node {name state}}`
}

// Creates filter for subset
func (s SegmentAPIManager) makeSubsetFilter(segmentsToCheck []string) string {
// segments = []: (fetch none)
// --> subsetFilter = "(subset:[])"
// segments = ["a"]: (fetch one segment)
// --> subsetFilter = "(subset:[\"a\"])"

escapedSegments := []string{}
for _, v := range segmentsToCheck {
escapedSegments = append(escapedSegments, fmt.Sprintf(`%q`, v))
func (s SegmentAPIManager) createRequestQuery(userKey, userValue string, segmentsToCheck []string) map[string]interface{} {
query := fmt.Sprintf(`query($userId: String, $audiences: [String]) {customer(%s: $userId){audiences(subset: $audiences) {edges {node {name state}}}}}`, userKey)
requestQuery := map[string]interface{}{
"query": query,
"variables": map[string]interface{}{
"userId": userValue,
"audiences": segmentsToCheck,
},
}
return fmt.Sprintf("(subset:[%s])", strings.Join(escapedSegments, ","))
return requestQuery
}

// Extract deep-json contents with keypath "a.b.c"
Expand Down
3089
0