Skip to content

Commit

Permalink
Merge pull request #60 from LibreSign/feature/add-alias-to-fetch-fiel…
Browse files Browse the repository at this point in the history
…d-from-response

Add alias when fetch field from response
  • Loading branch information
vitormattos authored Mar 22, 2024
2 parents 172923c + 44be5f0 commit 0835252
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ To send a value as array, you can set a json string and the json string will be
```gherkin
When sending "post" to ocs "/apps/libresign/api/v1/request-signature"
| status | 1 |
| file | {"base64":""} |
| file | {"base64":""} |
```

## Parse response using jq
Expand Down Expand Up @@ -112,7 +112,9 @@ If you need to:
- Get values from a request, store and use in other request
- Parse the response of a request

Implement a method `parseText` like the follow code and remember to call parent method:
Implement a method `parseText` like the follow code and remember to call parent method.

This methods can works together with `fetch field :path from prevous JSON response`
```php
protected function parseText(string $text): string {
$patterns = [
Expand Down
4 changes: 2 additions & 2 deletions features/test.feature
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ Feature: Test this extension
}
"""
And sending "POST" to "/"
And fetch field "data.0.foo" from prevous JSON response
And fetch field "(foo)data.0.foo" from prevous JSON response
# After fetch the field, you can use the value of field like this:
And sending "POST" to "/?foo=<data.0.foo>"
And sending "POST" to "/?foo=<foo>"
| field | <data.0.foo> |

Scenario: Test initial state with string
Expand Down
7 changes: 7 additions & 0 deletions src/NextcloudApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,19 @@ private function validateAsJsonQuery(string $expected, string $actual): void {
public function fetchFieldFromPreviousJsonResponse(string $path): void {
$this->response->getBody()->seek(0);
$responseArray = json_decode($this->response->getBody()->getContents(), true);
if (preg_match('/(?<alias>\([^)]*\))(?<patch>.*)/', $path, $matches)) {
$alias = $matches['alias'];
$path = $matches['patch'];
}
$keys = explode('.', $path);
$value = $responseArray;
foreach ($keys as $key) {
Assert::assertArrayHasKey($key, $value, 'Key [' . $key . '] of path [' . $path . '] not found.');
$value = $value[$key];
}
if (isset($alias)) {
$this->fields[$alias] = $value;
}
$this->fields[$path] = $value;
}

Expand Down

0 comments on commit 0835252

Please sign in to comment.