|
10 | 10 | use Neo4j\QueryAPI\Exception\Neo4jException;
|
11 | 11 | use Psr\Http\Client\RequestExceptionInterface;
|
12 | 12 | use Psr\Http\Message\ResponseInterface;
|
| 13 | +use Neo4j\QueryAPI\loginConfig; |
13 | 14 |
|
14 | 15 | class Neo4jQueryAPI
|
15 | 16 | {
|
16 | 17 | private Client $client;
|
| 18 | + private LoginConfig $loginConfig; |
17 | 19 | private Configuration $config;
|
18 | 20 | private ResponseParser $responseParser;
|
19 | 21 |
|
20 |
| - public function __construct(Configuration $config, ResponseParser $responseParser) |
| 22 | + public function __construct(LoginConfig $loginConfig, ResponseParser $responseParser, Configuration $config) |
21 | 23 | {
|
22 |
| - $this->config = $config; |
| 24 | + $this->loginConfig = $loginConfig; |
23 | 25 | $this->responseParser = $responseParser;
|
| 26 | + $this->config = $config; |
24 | 27 |
|
25 | 28 | $this->client = new Client([
|
26 |
| - 'base_uri' => rtrim($this->config->getBaseUrl(), '/'), |
27 |
| - 'timeout' => 10.0, |
28 |
| - 'headers' => $this->config->getDefaultHeaders(), |
| 29 | + 'base_uri' => rtrim($this->loginConfig->baseUrl, '/'), |
| 30 | + 'timeout' => 10.0, |
| 31 | + 'headers' => [ |
| 32 | + 'Authorization' => 'Basic ' . $this->loginConfig->authToken, |
| 33 | + 'Accept' => 'application/vnd.neo4j.query', |
| 34 | + ], |
29 | 35 | ]);
|
30 | 36 | }
|
31 | 37 |
|
| 38 | + |
32 | 39 | /**
|
33 | 40 | * Static method to create an instance with login details.
|
34 | 41 | */
|
35 |
| - public static function login(string $address, string $username, string $password): self |
| 42 | + public static function login(): self |
36 | 43 | {
|
37 |
| - $authToken = base64_encode("$username:$password"); |
38 |
| - $config = (new Configuration()) |
39 |
| - ->setBaseUrl($address) |
40 |
| - ->setAuthToken($authToken); |
| 44 | + $loginConfig = loginConfig::fromEnv(); |
| 45 | + $config = new Configuration(); |
41 | 46 |
|
42 |
| - return new self($config, new ResponseParser(new OGM())); |
| 47 | + return new self($loginConfig, new ResponseParser(new OGM()), $config); |
43 | 48 | }
|
44 | 49 |
|
| 50 | + |
| 51 | + |
45 | 52 | /**
|
46 | 53 | * Executes a Cypher query.
|
47 | 54 | *
|
48 | 55 | * @throws Neo4jException|RequestExceptionInterface
|
49 | 56 | */
|
50 |
| - public function run(string $cypher, array $parameters = [], string $database = 'neo4j', Bookmarks $bookmark = null, ?string $impersonatedUser = null, AccessMode $accessMode = AccessMode::WRITE): ResultSet |
| 57 | + public function run(string $cypher, array $parameters = []): ResultSet |
51 | 58 | {
|
52 | 59 | try {
|
53 | 60 | $payload = [
|
54 |
| - 'statement' => $cypher, |
55 |
| - 'parameters' => empty($parameters) ? new \stdClass() : $parameters, |
56 |
| - 'includeCounters' => true, |
57 |
| - 'accessMode' => $accessMode->value, |
| 61 | + 'statement' => $cypher, |
| 62 | + 'parameters' => empty($parameters) ? new \stdClass() : $parameters, |
| 63 | + 'includeCounters' => $this->config->includeCounters, |
| 64 | + 'accessMode' => $this->config->accessMode->value, |
58 | 65 | ];
|
59 | 66 |
|
60 |
| - if ($bookmark !== null) { |
61 |
| - $payload['bookmarks'] = $bookmark->getBookmarks(); |
| 67 | + if (!empty($this->config->bookmark)) { |
| 68 | + $payload['bookmarks'] = $this->config->bookmark; |
62 | 69 | }
|
63 | 70 |
|
64 |
| - if ($impersonatedUser !== null) { |
65 |
| - $payload['impersonatedUser'] = $impersonatedUser; |
66 |
| - } |
67 | 71 |
|
68 |
| - $response = $this->client->post("/db/{$database}/query/v2", ['json' => $payload]); |
| 72 | + |
| 73 | +// if ($impersonatedUser !== null) { |
| 74 | +// $payload['impersonatedUser'] = $impersonatedUser; |
| 75 | +// } |
| 76 | + error_log('Neo4j Payload: ' . json_encode($payload)); |
| 77 | + |
| 78 | + $response = $this->client->post("/db/{$this->config->database}/query/v2", ['json' => $payload]); |
69 | 79 |
|
70 | 80 | return $this->responseParser->parseRunQueryResponse($response);
|
71 | 81 | } catch (RequestException $e) {
|
72 |
| - $this->handleRequestException($e); |
| 82 | + error_log('Neo4j Request Failed: ' . $e->getMessage()); |
| 83 | + |
| 84 | + $this->handleRequestException($e); |
73 | 85 | }
|
74 | 86 | }
|
75 | 87 |
|
|
0 commit comments