| 1 | package WebService::GrowthBook::FeatureRepository; | ||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | no indirect; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Object::Pad; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Digest::MD5 qw(md5_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use JSON::MaybeUTF8 qw(decode_json_utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use WebService::GrowthBook::InMemoryFeatureCache; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 5 | our $VERSION = '0.003'; ## VERSION | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | class WebService::GrowthBook::FeatureRepository { | ||||||||||||||||||||||||||||
| 17 | 0 | field $http :param :writer //= HTTP::Tiny->new(); | |||||||||||||||||||||||||||
| 18 | 0 | ▶ | field $cache :param //= WebService::GrowthBook::InMemoryFeatureCache->singleton(); | ||||||||||||||||||||||||||
|
Subroutine
set_http: not called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | method set_cache($new_cache){ | ||||||||||||||||||||||||||
|
Subroutine
set_cache: not called CC=3 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | die "Invalid cache object $new_cache" unless blessed($new_cache) && $new_cache->isa('WebService::GrowthBook::AbstractFeatureCache'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($new_cache) and $new_cache->isa("WebService::GrowthBook::AbstractFeatureCache")
MC/DC0%0/2
builtin::blessed($new_cache) and $new_cache->isa("WebService::GrowthBook::AbstractFeatureCache")
builtin::blessed($new_cache)
$new_cache->isa("WebService::GrowthBook::AbstractFeatureCache")
Truth table0%0/3
A and B
A= builtin::blessed($new_cache)B= $new_cache->isa("WebService::GrowthBook::AbstractFeatureCache")
| |||||||||||||||||||||||||||||
| 21 | 0 | $cache = $new_cache; | |||||||||||||||||||||||||||
| 22 | 5 | } | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | method clear_cache(){ | ||||||||||||||||||||||||||
|
Subroutine
clear_cache: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 0 | $cache->clear(); | |||||||||||||||||||||||||||
| 26 | 5 | } | |||||||||||||||||||||||||||
| 27 | 8 | ▶ | method load_features($api_host, $client_key, $ttl = 60) { | ||||||||||||||||||||||||||
|
Subroutine
load_features: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 8 | my $key = get_cache_key($api_host, $client_key); | |||||||||||||||||||||||||||
| 29 | 8 | my $features = $cache->get($key); | |||||||||||||||||||||||||||
| 30 | 8 | ▶ | if($features){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 1 | $log->debug("Features loaded from cache"); | |||||||||||||||||||||||||||
| 32 | 1 | return $features; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 7 | $features = $self->_fetch_features($api_host, $client_key); | |||||||||||||||||||||||||||
| 35 | 7 | ▶ | if($features){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 2 | $cache->set($key, $features, $ttl); | |||||||||||||||||||||||||||
| 37 | 2 | $log->debug("Features loaded from GrowthBook API, set in cache"); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 7 | return $features; | |||||||||||||||||||||||||||
| 41 | 5 | } | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 7 | ▶ | method _fetch_features($api_host, $client_key){ | ||||||||||||||||||||||||||
|
Subroutine
_fetch_features: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 44 | 7 | my $decoded = $self->_fetch_and_decode($api_host, $client_key); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # TODO decrypt here | ||||||||||||||||||||||||||||
| 47 | 7 | ▶ | if(exists $decoded->{features}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 2 | return $decoded->{features}; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | else { | ||||||||||||||||||||||||||||
| 51 | 5 | $log->warn("GrowthBook API response missing features"); | |||||||||||||||||||||||||||
| 52 | return | ||||||||||||||||||||||||||||
| 53 | 5 | } | |||||||||||||||||||||||||||
| 54 | 5 | } | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 7 | ▶ | method _fetch_and_decode($api_host, $client_key){ | ||||||||||||||||||||||||||
|
Subroutine
_fetch_and_decode: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 57 | try { | ||||||||||||||||||||||||||||
| 58 | 7 | my $r = $self->_get($self->_get_features_url($api_host, $client_key)); | |||||||||||||||||||||||||||
| 59 | 7 | ▶ | if($r->{status} >= 400){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 3 | $log->warnf("Failed to fetch features, received status code %d", $r->{status}); | |||||||||||||||||||||||||||
| 61 | 3 | return; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 4 | my $decoded = decode_json_utf8($r->{content}); | |||||||||||||||||||||||||||
| 64 | 2 | return $decoded; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 7 | catch ($e){ | |||||||||||||||||||||||||||
| 67 | 2 | $log->warnf("Failed to decode feature JSON from GrowthBook API: %s", $e); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 2 | return; | |||||||||||||||||||||||||||
| 70 | 5 | } | |||||||||||||||||||||||||||
| 71 | 7 | ▶ | method _get($url){ | ||||||||||||||||||||||||||
|
Subroutine
_get: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 7 | my $headers = { | |||||||||||||||||||||||||||
| 73 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 7 | return $http->get($url, {headers => $headers}); | |||||||||||||||||||||||||||
| 77 | 5 | } | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 7 | ▶ | method _get_features_url($api_host, $client_key){ | ||||||||||||||||||||||||||
|
Subroutine
_get_features_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | 7 | return "$api_host/api/features/$client_key"; | |||||||||||||||||||||||||||
| 81 | 5 | } | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 5 | ▶ | sub get_cache_key($api_host, $client_key){ | ||||||||||||||||||||||||||
|
Subroutine
get_cache_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 8 | return md5_base64($api_host . '::' . $client_key); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 5 | 1; | |||||||||||||||||||||||||||