|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\SupportedApps\Ghostfolio; |
| 4 | + |
| 5 | +use Exception; |
| 6 | + |
| 7 | +class Ghostfolio extends \App\SupportedApps implements \App\EnhancedApps |
| 8 | +{ |
| 9 | + public $config; |
| 10 | + |
| 11 | + public function __construct() |
| 12 | + { |
| 13 | + } |
| 14 | + |
| 15 | + public function test() |
| 16 | + { |
| 17 | + try { |
| 18 | + $this->auth(); |
| 19 | + $perf = $this->getPortfolioPerformance(); |
| 20 | + |
| 21 | + echo "Successfully communicated with the API"; |
| 22 | + } catch (Exception $err) { |
| 23 | + echo "Error connecting to Ghostfolio: " . $err->getMessage(); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public function url($endpoint) |
| 28 | + { |
| 29 | + $api_url = parent::normaliseurl($this->config->url) . $endpoint; |
| 30 | + return $api_url; |
| 31 | + } |
| 32 | + |
| 33 | + public function auth(): void |
| 34 | + { |
| 35 | + $body = json_encode(["accessToken" => $this->config->password]); |
| 36 | + |
| 37 | + $vars = [ |
| 38 | + "http_errors" => false, |
| 39 | + "timeout" => 5, |
| 40 | + "body" => $body, |
| 41 | + "headers" => [ |
| 42 | + "Content-Type" => "application/json", |
| 43 | + ], |
| 44 | + ]; |
| 45 | + |
| 46 | + $result = parent::execute( |
| 47 | + $this->url("api/v1/auth/anonymous"), |
| 48 | + [], |
| 49 | + $vars, |
| 50 | + "POST" |
| 51 | + ); |
| 52 | + |
| 53 | + if ($result === null) { |
| 54 | + throw new Exception("Could not connect to Ghostfolio"); |
| 55 | + } |
| 56 | + |
| 57 | + $responseBody = $result->getBody()->getContents(); |
| 58 | + $response = json_decode($responseBody, true); |
| 59 | + if (null === $response || $result->getStatusCode() !== 201 || !isset($response['authToken'])) { |
| 60 | + throw new Exception("Error logging in"); |
| 61 | + } |
| 62 | + |
| 63 | + $this->config->jwt = $response['authToken']; |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + public function livestats() |
| 68 | + { |
| 69 | + $this->auth(); |
| 70 | + $status = 'inactive'; |
| 71 | + |
| 72 | + $data = $this->getPortfolioPerformance(); |
| 73 | + foreach ($this->config->availablestats as $num => $key) { |
| 74 | + $stat = new \stdClass(); |
| 75 | + $stat->title = self::getAvailableStats()[$key]; |
| 76 | + $value = $data[$key] ?? null; |
| 77 | + $stat->value = is_numeric($value) |
| 78 | + ? rtrim( |
| 79 | + rtrim(number_format($value, 1, decimal_separator: '.', thousands_separator: ''), characters: '0'), |
| 80 | + characters: '.' |
| 81 | + ) |
| 82 | + : substr(string: $value, offset: 0, length: 1); |
| 83 | + $details["visiblestats"][] = $stat; |
| 84 | + } |
| 85 | + return parent::getLiveStats($status, $details); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + private function getPortfolioPerformance(): mixed |
| 91 | + { |
| 92 | + $attrs = [ |
| 93 | + "headers" => [ |
| 94 | + "Content-Type" => "application/json", |
| 95 | + "Authorization" => "Bearer " . $this->config->jwt, |
| 96 | + ], |
| 97 | + "query" => [ |
| 98 | + "range" => $this->config->selected_range, |
| 99 | + ], |
| 100 | + ]; |
| 101 | + $result = parent::execute( |
| 102 | + $this->url("api/v2/portfolio/performance"), |
| 103 | + $attrs, |
| 104 | + ); |
| 105 | + |
| 106 | + if (null === $result || $result->getStatusCode() !== 200) { |
| 107 | + throw new Exception("Error retrieving performance"); |
| 108 | + } |
| 109 | + $data = json_decode($result->getBody()->getContents(), true); |
| 110 | + return $data['performance']; |
| 111 | + } |
| 112 | + |
| 113 | + public static function getAvailableStats() |
| 114 | + { |
| 115 | + return [ |
| 116 | + "netPerformancePercentageWithCurrencyEffect" => "Net Perf (%)", |
| 117 | + "netPerformanceWithCurrencyEffect" => "Net Perf", |
| 118 | + "totalInvestment" => "Invested", |
| 119 | + "currentValueInBaseCurrency" => "Value", |
| 120 | + "currentNetWorth" => "Net Worth", |
| 121 | + ]; |
| 122 | + } |
| 123 | +} |
0 commit comments