Skip to content

Commit 940a0a8

Browse files
p123-stackPratiksha
andauthored
Implement request factory 1963795518 (#13)
* refactor to request factory * allow manual dispatching of workflows --------- Co-authored-by: Pratiksha <pratiksha@Pratiksha-Nagels>
1 parent cdd7ff9 commit 940a0a8

21 files changed

+877
-100
lines changed

.github/workflows/cs-fixer.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
pull_request:
1010
paths:
1111
- '**/*.php'
12+
workflow_dispatch:
1213

1314
jobs:
1415
php-cs-fixer:

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
branches:
66
- main
77
pull_request:
8+
workflow_dispatch:
89

910
concurrency:
1011
group: ${{ github.ref }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
#IDE
23
.idea/
34

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"guzzlehttp/guzzle": "^7.9",
77
"psr/http-client": "^1.0",
88
"ext-json": "*",
9-
"php": "^8.1"
9+
"php": "^8.1",
10+
"nyholm/psr7": "^1.8"
1011
},
1112
"require-dev": {
1213
"phpunit/phpunit": "^11.0",
@@ -39,4 +40,4 @@
3940
"cs:fix": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
4041
}
4142

42-
}
43+
}

composer.lock

Lines changed: 79 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BasicAuthentication.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@
33
namespace Neo4j\QueryAPI;
44

55
use Psr\Http\Message\RequestInterface;
6-
use Psr\Http\Message\ResponseInterface;
76

87
class BasicAuthentication implements AuthenticateInterface
98
{
10-
public function __construct(private string $username, private string $password)
9+
private string $username;
10+
private string $password;
11+
12+
public function __construct(?string $username = null, ?string $password = null)
1113
{
12-
$this->username = $username;
13-
$this->password = $password;
14+
// Use provided values or fallback to environment variables
15+
$this->username = $username ?? getenv("NEO4J_USERNAME") ?: '';
16+
$this->password = $password ?? getenv("NEO4J_PASSWORD") ?: '';
1417
}
1518

1619
public function authenticate(RequestInterface $request): RequestInterface
1720
{
18-
$authHeader = 'Basic ' . base64_encode($this->username . ':' . $this->password);
21+
$authHeader = $this->getHeader();
1922
return $request->withHeader('Authorization', $authHeader);
2023
}
24+
2125
public function getHeader(): string
2226
{
2327
return 'Basic ' . base64_encode($this->username . ':' . $this->password);
@@ -27,5 +31,4 @@ public function getType(): string
2731
{
2832
return 'Basic';
2933
}
30-
3134
}

src/BearerAuthentication.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function authenticate(RequestInterface $request): RequestInterface
1515
$authHeader = 'Bearer ' . $this->token;
1616
return $request->withHeader('Authorization', $authHeader);
1717
}
18+
1819
public function getHeader(): string
1920
{
2021
return 'Bearer ' . $this->token;

src/Neo4jPhp.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
require __DIR__.'/../vendor/autoload.php'; // Assuming you have Composer installed and dependencies loaded
4+
5+
use Neo4j\QueryAPI\Neo4jQueryAPI;
6+
use Neo4j\QueryAPI\Objects\Authentication;
7+
use Neo4j\QueryAPI\Objects\Bookmarks;
8+
use GuzzleHttp\Client;
9+
use GuzzleHttp\Exception\RequestException;
10+
use stdClass;
11+
12+
try {
13+
// Step 1: Initialize the connection to Neo4j with login credentials (authentication)
14+
$neo4jAddress = '***REMOVED***'; // Replace with your Neo4j database address
15+
$username = 'neo4j';
16+
$password = '***REMOVED***';
17+
18+
// Base64 encode the username and password for Basic Authentication
19+
$credentials = base64_encode("$username:$password");
20+
21+
// Set up the authentication header with base64-encoded credentials
22+
$headers = [
23+
'Authorization' => 'Basic ' . $credentials,
24+
'Content-Type' => 'application/json',
25+
];
26+
27+
// Initialize the client with the authorization header
28+
$client = new \GuzzleHttp\Client([
29+
'base_uri' => rtrim($neo4jAddress, '/'),
30+
'timeout' => 10.0,
31+
'headers' => $headers,
32+
]);
33+
34+
// Step 2: Create the Cypher query
35+
$cypherQuery = 'MATCH (n) RETURN n LIMIT 10';
36+
$parameters = []; // No parameters in this query
37+
$database = 'neo4j'; // Default Neo4j database
38+
39+
echo "Running Cypher Query: $cypherQuery\n";
40+
41+
// Prepare the payload for the Cypher query
42+
$payload = [
43+
'statement' => $cypherQuery,
44+
'parameters' => new stdClass(), // No parameters
45+
'includeCounters' => true,
46+
];
47+
48+
// Step 3: Send the request to Neo4j
49+
$response = $client->post("/db/{$database}/query/v2", [
50+
'json' => $payload,
51+
]);
52+
53+
// Parse the response body as JSON
54+
$responseData = json_decode($response->getBody()->getContents(), true);
55+
56+
// Check for errors in the response
57+
if (isset($responseData['errors']) && count($responseData['errors']) > 0) {
58+
echo "Error: " . $responseData['errors'][0]['message'] . "\n";
59+
} else {
60+
// Step 4: Output the result of the query
61+
echo "Query Results:\n";
62+
foreach ($responseData['data'] as $row) {
63+
print_r($row); // Print each row's data
64+
}
65+
}
66+
67+
// Step 5: Begin a new transaction
68+
$transactionResponse = $client->post("/db/neo4j/query/v2/tx");
69+
$transactionData = json_decode($transactionResponse->getBody()->getContents(), true);
70+
$transactionId = $transactionData['transaction']['id']; // Retrieve the transaction ID
71+
72+
echo "Transaction started successfully.\n";
73+
echo "Transaction ID: $transactionId\n";
74+
75+
// You can also fetch additional transaction details if available in the response
76+
// Example: transaction metadata or counters
77+
if (isset($transactionData['transaction']['metadata'])) {
78+
echo "Transaction Metadata: \n";
79+
print_r($transactionData['transaction']['metadata']);
80+
}
81+
82+
// Step 6: Execute a query within the transaction
83+
$cypherTransactionQuery = 'MATCH (n) SET n.modified = true RETURN n LIMIT 5';
84+
$transactionPayload = [
85+
'statement' => $cypherTransactionQuery,
86+
'parameters' => new stdClass(), // No parameters
87+
];
88+
89+
// Execute the transaction query
90+
$transactionQueryResponse = $client->post("/db/neo4j/query/v2/tx/{$transactionId}/commit", [
91+
'json' => $transactionPayload,
92+
]);
93+
94+
$transactionQueryData = json_decode($transactionQueryResponse->getBody()->getContents(), true);
95+
96+
// Check for any errors in the transaction query
97+
if (isset($transactionQueryData['errors']) && count($transactionQueryData['errors']) > 0) {
98+
echo "Transaction Error: " . $transactionQueryData['errors'][0]['message'] . "\n";
99+
} else {
100+
echo "Transaction Query Results:\n";
101+
print_r($transactionQueryData['data']); // Print transaction results
102+
}
103+
104+
} catch (RequestException $e) {
105+
echo "Request Error: " . $e->getMessage() . "\n";
106+
} catch (Exception $e) {
107+
echo "Error: " . $e->getMessage() . "\n";
108+
}

0 commit comments

Comments
 (0)