Luka
Luka

Reputation: 57

Saving an entity with compressed client response and request in Symfony 7

Initial Situation

I have an application in Symfony 7. In this application, I am making external API calls, for which I have an abstract class HttpClient defined. In this class, I use guzzle Client to handle the request. I have an entity for saving external call related information and am using Doctrine for database interactions. The snippet of the entity is below:

#[ORM\Column(name: 'request',type: 'json')]
private array $request;

#[ORM\Column(name: 'response', type: 'json', nullable: true)]
private ?array $response = null;

Until now, I have been saving request and response as plain text. Compressing request does not seem to create any problems, the response compression has proven to be problematic. Below is the snippet of my HttpClient abstract class (I removed the parts which are not relevant to this question), where the entity is created and saved to the database:

$log = [
    'request' => $this->serializer->normalize($request),
];

try {
    $response = $this->client->request(
        $request->getRequestMethod(),
        $request->getUri(),
        $this->prepareGuzzleOptions($request)
    );
    $log['response'] = $this->serializer->decode((string) $response->getBody()->getContents(), 'json');
} catch (GuzzleException $e) {
    $log['response'] = $this->serializer->normalize([$e->getMessage(), $e->getTraceAsString()]);
} catch (\Throwable $e) {
    $log['response'] = ["exceptionMessage" => $e->getMessage()];
    throw $e;
} finally {
    $log = $this->serializer->denormalize($log, Log::class);
    $this->em->persist($apiLog);
    $this->em->flush();
    $this->em->clear();
}

Desired outcome

Due to space that they take up, I want to compress them and save them compressed. I want also to be able to uncompress them in database management tool in case I need to troubleshoot something.

Steps taken

I tried changing attribute type to both 'blob' and 'binary', also tried to play around with compressing the request but I can not get it to a 'resource' type and get errors like: The type of the "request" attribute for class must be one of "resource" ("array" given). or The type of the "request" attribute for class must be one of "resource" ("string" given).

I understand what the problem is, but cannot think of a solution of how I could convert the string to a resource in an elegant and technically meaningful way.

If any additional information is needed, I will provide it gladly. Any help will be appreciated.

Upvotes: 0

Views: 50

Answers (0)

Related Questions