Skip to content

Commit

Permalink
Merge pull request #224 from apitoolkit/php
Browse files Browse the repository at this point in the history
[WIP] PHP & Python SDKs test
  • Loading branch information
BolajiAyodeji authored Jun 20, 2024
2 parents 4b9fc6f + 11ddbbc commit 16629b2
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 79 deletions.
2 changes: 1 addition & 1 deletion docs/sdks/dotnet/dotnetcore/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ The `client.APIToolkitObservingHandler` handler accepts a required `context` fie
:::
| Option | Description |
| ------ | ----------- |
| `pathWildCard` | The `url_path` for URLs with path parameters. |
| `pathWildCard` | The `url_path` string for URLs with path parameters. |
| `RedactHeaders` | A list of HTTP header keys to redact. |
| `RedactResponseBody` | A list of JSONPaths from the request body to redact. |
| `RedactRequestBody` | A list of JSONPaths from the response body to redact. |
Expand Down
2 changes: 1 addition & 1 deletion docs/sdks/java/springboot/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public class DemoApplication {

Outgoing requests are external API calls you make from your API. By default, APItoolkit monitors all requests users make from your application and they will all appear in the [API Log Explorer](/docs/dashboard/dashboard-pages/api-log-explorer/){target="\_blank"} page. However, you can separate outgoing requests from others and explore them in the [Outgoing Integrations](/docs/dashboard/dashboard-pages/outgoing-integrations/){target="\_blank"} page, alongside the incoming request that triggered them.

The Springboot SDK provides the `ObserveRequest` class for monitoring outgoing requests using the Apache HTTP client. First, you will create an instance of the class, then use the instance to create a new HTTP client, passing in the current `request` context and an optional `url_path` (for URLs with path parameters), like so:
The Springboot SDK provides the `ObserveRequest` class for monitoring outgoing requests using the Apache HTTP client. First, you will create an instance of the class, then use the instance to create a new HTTP client, passing in the current `request` context and an optional `url_path` string (for URLs with path parameters), like so:

```java
package com.example.demo;
Expand Down
4 changes: 2 additions & 2 deletions docs/sdks/nodejs/adonisjs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,13 @@ Route.get("/observer", async () => {
});
```

The `observeAxios` function above accepts a **required `axios` instance** and the following optional fields:
The `observeAxios` function above accepts a **required `axios` instance** and the following optional arguments:

{class="docs-table"}
:::
| Option | Description |
| ------ | ----------- |
| `pathWildCard` | The `url_path` for URLs with path parameters. |
| `pathWildCard` | The `url_path` string for URLs with path parameters. |
| `redactHeaders` | A list of HTTP header keys to redact. |
| `redactResponseBody` | A list of JSONPaths from the request body to redact. |
| `redactRequestBody` | A list of JSONPaths from the response body to redact. |
Expand Down
4 changes: 2 additions & 2 deletions docs/sdks/nodejs/expressjs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,13 @@ const response = await apitoolkitClient
console.log(response.data);
```

The `observeAxios` function above accepts a **required `axios` instance** and the following optional fields:
The `observeAxios` function above accepts a **required `axios` instance** and the following optional arguments:

{class="docs-table"}
:::
| Option | Description |
| ------ | ----------- |
| `pathWildCard` | The `url_path` for URLs with path parameters. |
| `pathWildCard` | The `url_path` string for URLs with path parameters. |
| `redactHeaders` | A list of HTTP header keys to redact. |
| `redactResponseBody` | A list of JSONPaths from the request body to redact. |
| `redactRequestBody` | A list of JSONPaths from the response body to redact. |
Expand Down
4 changes: 2 additions & 2 deletions docs/sdks/nodejs/fastifyjs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ app.get("/", async (request, reply) => {
});
```

The `observeAxios` function above accepts a **required `axios` instance** and the following optional fields:
The `observeAxios` function above accepts a **required `axios` instance** and the following optional arguments:

{class="docs-table"}
:::
| Option | Description |
| ------ | ----------- |
| `pathWildCard` | The `url_path` for URLs with path parameters. |
| `pathWildCard` | The `url_path` string for URLs with path parameters. |
| `redactHeaders` | A list of HTTP header keys to redact. |
| `redactResponseBody` | A list of JSONPaths from the request body to redact. |
| `redactRequestBody` | A list of JSONPaths from the response body to redact. |
Expand Down
51 changes: 15 additions & 36 deletions docs/sdks/php/laravel/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,61 +46,43 @@ APITOOLKIT_SERVICE_VERSION=v2.0
Next, register the middleware in the `app/Http/Kernel.php` file under the correct middleware group (e.g., `api`) or at the root, like so:

```php
<?php
&lt;?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
...
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
...
'api' => [
...
// Other middleware here...
\APIToolkit\Http\Middleware\APIToolkit::class, // Initialize the APItoolkit client
...
],
];
...
}
```

Then you can use the `apitoolkit` middleware in your routes like so:

```php
Route::get('/', function () {
return response()->json([
'message' => 'Welcome to your new application!'
]);
})->middleware('apitoolkit');
```

</div>
<div id="tab2" class="tab-content">
Alternatively, if you want to monitor specific routes, you can register the middleware, like so:

```php
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
&lt;?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
protected $routeMiddleware = [
...
// Other middleware here...
'apitoolkit' => \APIToolkit\Http\Middleware\APIToolkit::class,
];
}
```

Then you can use the `apitoolkit` middleware in your routes like so:
Then you can use the `apitoolkit` middleware in your routes like so:

```php
Route::get('/', function () {
Expand Down Expand Up @@ -220,9 +202,6 @@ use Throwable;

class Handler extends ExceptionHandler
{
/**
* Register the exception handling callbacks for the application.
*/
public function register(): void
{
$this->reportable(function (Throwable $e) {
Expand Down Expand Up @@ -283,13 +262,13 @@ Route::get('/user', function (Request $request) {
})
```

The `$options` list accepts the following optional fields:
The `$options` associative array accepts the following optional fields:

{class="docs-table"}
:::
| Option | Description |
| ------ | ----------- |
| `pathWildCard` | The `url_path` for URLs with path parameters. |
| `pathWildCard` | The `url_path` string for URLs with path parameters. |
| `redactHeaders` | A list of HTTP header keys to redact. |
| `redactResponseBody` | A list of JSONPaths from the request body to redact. |
| `redactRequestBody` | A list of JSONPaths from the response body to redact. |
Expand Down
27 changes: 17 additions & 10 deletions docs/sdks/php/slim/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ require __DIR__ . '/vendor/autoload.php';

$app = AppFactory::create();

// Initialize the APItoolkit client
$apitoolkitMiddleware = new APIToolkitMiddleware(
"{ENTER_YOUR_API_KEY_HERE}",
$debug=false
$tags=["environment: production", "region: us-east-1"],
$serviceVersion="v2.0"
"{ENTER_YOUR_API_KEY_HERE}",
$rootUrl=null,
$redactHeaders=[],
$redactRequestBody=[],
$redactResponseBody=[],
$debug=false,
$serviceVersion="v2.0",
$tags=["environment: production", "region: us-east-1"],
);
// IMPORTANT: all the options above must be configured
// in that exact order to avoid a type error

// Initialize the APItoolkit client
$app->add($apitoolkitMiddleware);
// END Initialize the APItoolkit client

Expand Down Expand Up @@ -142,9 +148,10 @@ $app = AppFactory::create();

$apitoolkitMiddleware = new APIToolkitMiddleware(
"{ENTER_YOUR_API_KEY_HERE}",
redactHeaders = ["content-type", "Authorization", "HOST"],
redactRequestBody = ["$.user.email", "$.user.addresses"],
redactResponseBody = ["$.users[*].email", "$.users[*].credit_card"]
$rootUrl=null,
$redactHeaders=["content-type", "Authorization", "HOST"],
$redactRequestBody=["$.user.email", "$.user.addresses"],
$redactResponseBody=["$.users[*].email", "$.users[*].credit_card"]
);

$app->add($apitoolkitMiddleware);
Expand Down Expand Up @@ -236,13 +243,13 @@ $app->get('/user', function (Request $request, Response $response) {
$app->run();
```

The `$options` list accepts the following optional fields:
The `$options` associative array accepts the following optional fields:

{class="docs-table"}
:::
| Option | Description |
| ------ | ----------- |
| `pathWildCard` | The `url_path` for URLs with path parameters. |
| `pathWildCard` | The `url_path` string for URLs with path parameters. |
| `redactHeaders` | A list of HTTP header keys to redact. |
| `redactResponseBody` | A list of JSONPaths from the request body to redact. |
| `redactRequestBody` | A list of JSONPaths from the response body to redact. |
Expand Down
18 changes: 14 additions & 4 deletions docs/sdks/python/django/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,8 @@ from apitoolkit_django import observe_request, report_error

def hello_world(request, name):
try:
resp = observe_request(request).get(
"https://jsonplaceholder.typicode.com/todos/2")
resp.read()
return JsonResponse({"hello": "world"})
value = 1/0
return JsonResponse({"hello": value})
except Exception as e:
report_error(request, e)
return JsonResponse({"Error": "Something went wrong..."})
Expand All @@ -203,6 +201,18 @@ def hello_world(request, name):
return JsonResponse({"data": resp.read()})
```

The `observe_request()` function accepts a **required `request` argument**, and the following optional arguments:

{class="docs-table"}
:::
| Option | Description |
| ------ | ----------- |
| `url_wildcard` | The `url_path` string for URLs with path parameters. |
| `redact_headers` | A list of HTTP header keys to redact. |
| `redact_response_body` | A list of JSONPaths from the request body to redact. |
| `redact_request_body` | A list of JSONPaths from the response body to redact. |
:::

<div class="callout">
<p><i class="fa-regular fa-lightbulb"></i> <b>Tip</b></p>
<p>The `observe_request()` function wraps an [HTTPX](https://python-httpx.org?utm_source=apitoolkit){target="\_blank"} client and you can use it just like you would normally use HTTPX for any request.</p>
Expand Down
30 changes: 21 additions & 9 deletions docs/sdks/python/fastapi/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,19 @@ To report all uncaught errors and service exceptions that happened during a web

```python
from fastapi import FastAPI, Request
from apitoolkit_fastapi import observe_request, report_error
from apitoolkit_fastapi import report_error

app = FastAPI()

@app.get('/sample/{subject}')
async def sample_route(subject: str, request: Request):
@app.get('/')
async def sample_route(request: Request):
try:
resp = observe_request(request).get("https://jsonplaceholder.typicode.com/todos/2")
return resp.read()
v = 1/ 0
return {"zero_division": v}
except Exception as e:
# Report the error to APItoolkit
report_error(request, e)
return "Something went wrong"
return {"message": "Something went wrong"}
```

## Monitoring Outgoing Requests
Expand All @@ -190,16 +190,28 @@ To monitor outgoing HTTP requests from your application, use the `observe_reques

```python
from fastapi import FastAPI, Request
from apitoolkit_fastapi import observe_request, report_error
from apitoolkit_fastapi import observe_request

app = FastAPI()

@app.get('/sample/{subject}')
async def sample_route(subject: str, request: Request):
@app.get('/')
async def sample_route(request: Request):
resp = observe_request(request).get("https://jsonplaceholder.typicode.com/todos/2")
return resp.read()
```

The `observe_request()` function accepts a **required `request` argument**, and the following optional arguments:

{class="docs-table"}
:::
| Option | Description |
| ------ | ----------- |
| `url_wildcard` | The `url_path` string for URLs with path parameters. |
| `redact_headers` | A list of HTTP header keys to redact. |
| `redact_response_body` | A list of JSONPaths from the request body to redact. |
| `redact_request_body` | A list of JSONPaths from the response body to redact. |
:::

<div class="callout">
<p><i class="fa-regular fa-lightbulb"></i> <b>Tip</b></p>
<p>The `observe_request()` function wraps an [HTTPX](https://python-httpx.org?utm_source=apitoolkit){target="\_blank"} client and you can use it just like you would normally use HTTPX for any request.</p>
Expand Down
36 changes: 24 additions & 12 deletions docs/sdks/python/flask/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def after_request(response):
# END Initialize APItoolkit

@app.route('/hello', methods=['GET', 'POST'])
def sample_route(subject):
def sample_route():
return {"Hello": "World"}

app.run(debug=True)
Expand Down Expand Up @@ -164,7 +164,7 @@ def after_request(response):


@app.route('/hello', methods=['GET', 'POST'])
def sample_route(subject):
def sample_route():
return {"Hello": "World"}

app.run(debug=True)
Expand All @@ -186,18 +186,18 @@ APItoolkit automatically detects different unhandled errors, API issues, and ano
To report all uncaught errors and service exceptions that happened during a web request, use the `report_error()` function from the `apitoolkit_flask` module, passing in the `request` and `error`, like so:

```python
from flask import Flask, request
from apitoolkit_flask import observe_request, report_error
from flask import Flask
from apitoolkit_flask import APIToolkit, report_error

@app.route('/sample/<subject>', methods=['GET', 'POST'])
async def sample_route(subject):
@app.route('/', methods=['GET', 'POST'])
def sample_route():
try:
resp = observe_request(request).get("https://jsonplaceholder.typicode.com/todos/2")
return resp.read()
value = 1/ 0
return {"zero_division": value}
except Exception as e:
# Report the error to APItoolkit
report_error(request, e)
return "Something went wrong"
return {"message": "Something went wrong"}
```

## Monitoring Outgoing Requests
Expand All @@ -208,14 +208,26 @@ To monitor outgoing HTTP requests from your application, use the `observe_reques

```python
from flask import Flask, request
from apitoolkit_flask import observe_request
from apitoolkit_flask import APIToolkit, observe_request

@app.route('/sample/<subject>', methods=['GET', 'POST'])
async def sample_route(subject):
@app.route('/', methods=['GET', 'POST'])
async def sample_route():
resp = observe_request(request).get("https://jsonplaceholder.typicode.com/todos/2")
return resp.read()
```

The `observe_request()` function accepts a **required `request` argument**, and the following optional arguments:

{class="docs-table"}
:::
| Option | Description |
| ------ | ----------- |
| `url_wildcard` | The `url_path` string for URLs with path parameters. |
| `redact_headers` | A list of HTTP header keys to redact. |
| `redact_response_body` | A list of JSONPaths from the request body to redact. |
| `redact_request_body` | A list of JSONPaths from the response body to redact. |
:::

<div class="callout">
<p><i class="fa-regular fa-lightbulb"></i> <b>Tip</b></p>
<p>The `observe_request()` function wraps an [HTTPX](https://python-httpx.org?utm_source=apitoolkit){target="\_blank"} client and you can use it just like you would normally use HTTPX for any request.</p>
Expand Down
Loading

0 comments on commit 16629b2

Please sign in to comment.