Skip to content

Feature/new request handling logic inter 767 #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 37 commits into from
Jul 30, 2024

Conversation

Orkuncakilkaya
Copy link
Contributor

No description provided.

@Orkuncakilkaya Orkuncakilkaya marked this pull request as draft July 10, 2024 14:10
@Orkuncakilkaya Orkuncakilkaya force-pushed the feature/new-request-handling-logic-inter-767 branch from b5f338d to 26e984f Compare July 10, 2024 14:24
@Orkuncakilkaya Orkuncakilkaya requested a review from ilfa July 16, 2024 10:30
@Orkuncakilkaya Orkuncakilkaya marked this pull request as ready for review July 16, 2024 10:32
@TheUnderScorer
Copy link
Contributor

Question: when exception is thrown, do we include a HTTP Response in it (if it is available)?

@Orkuncakilkaya
Copy link
Contributor Author

Hey @TheUnderScorer

For your question:

Question: when exception is thrown, do we include a HTTP Response in it (if it is available)?

Yes, you can use an optional method called xWithHttpInfo. For Example: getVisitsWithHttpInfo for errors with HTTP information.

Copy link
Contributor

@TheUnderScorer TheUnderScorer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for these changes @Orkuncakilkaya! It looks a lot better now.
We can still improve the request handling code by a bit. I have couple of suggestions:

  1. $responseBody = $response->getBody();
    if ($returnType === '\SplFileObject') {
    $content = $responseBody; //stream goes to serializer
    } else {
    $content = $responseBody->getContents();
    if (!in_array($returnType, ['string','integer','bool'])) {
    $content = json_decode($content);
    }
    }
    Here we have some unnecessary checks for specific return types that are never returned from our API, let's remove that.
  2. function ($exception) {
    $response = $exception->getResponse();
    $statusCode = $response->getStatusCode();
    throw new ApiException(
    sprintf(
    '[%d] Error connecting to the API (%s)',
    $statusCode,
    $exception->getRequest()->getUri()
    ),
    $statusCode,
    $response->getHeaders(),
    $response->getBody()
    );
    }
    );
    In async methods we don't map returned status code to specific error model as we do in sync methods.
  3. if ($multipart) {
    $headers = $this->headerSelector->selectHeadersForMultipart(
    ['application/json']
    );
    } else {
    $headers = $this->headerSelector->selectHeaders(
    ['application/json'],
    []
    );
    }
    This is also unnecessary, as far as I'm aware we don't support multipart requests in our API, let's remove the HeaderSelector all together
  4. if (isset($_tempBody)) {
    // $_tempBody is the method argument, if present
    $httpBody = $_tempBody;
    // \stdClass has no __toString(), so we should encode it manually
    if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
    $httpBody = \GuzzleHttp\json_encode($httpBody);
    }
    } elseif (count($formParams) > 0) {
    if ($multipart) {
    $multipartContents = [];
    foreach ($formParams as $formParamName => $formParamValue) {
    $multipartContents[] = [
    'name' => $formParamName,
    'contents' => $formParamValue
    ];
    }
    // for HTTP post (form)
    $httpBody = new MultipartStream($multipartContents);
    } elseif ($headers['Content-Type'] === 'application/json') {
    $httpBody = \GuzzleHttp\json_encode($formParams);
    } else {
    // for HTTP post (form)
    $httpBody = http_build_query($formParams);
    }
    }
    This part is also too complex, including unnecessary handling of multipart requests, that again are not used in our API as well as handling for \stdClass that is not needed.

These code snippets are from getEvent operation, but this applies to all other request as well. Overall, please double-check if there is more code that we can simplify/remove 🙂

Copy link
Contributor

@TheUnderScorer TheUnderScorer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for these changes @Orkuncakilkaya. The code looks much better now!
I have couple of more suggestions/notes:

if ($request_id !== null) {
$resourcePath = str_replace(
'{' . 'request_id' . '}',
ObjectSerializer::toPathValue($request_id),
$resourcePath
);
}

This check is reduntant, because we perform the same check couple of lines above. Applies to all API methods.

Also, pls take a look at the async methods. It seems we still don't map status code to correct error response there as we do in the sync methods.

Copy link
Contributor

@TheUnderScorer TheUnderScorer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more suggestion:

$discriminator = $class::DISCRIMINATOR;
if (!empty($discriminator) && isset($data->{$discriminator}) && is_string($data->{$discriminator})) {
$subclass = 'Fingerprint\ServerAPI\Model\\' . $data->{$discriminator};
if (is_subclass_of($subclass, $class)) {
$class = $subclass;
}
}
$instance = new $class();
foreach ($instance::swaggerTypes() as $property => $type) {
$propertySetter = $instance::setters()[$property];
if (!isset($propertySetter) || !isset($data->{$instance::attributeMap()[$property]})) {
continue;
}
$propertyValue = $data->{$instance::attributeMap()[$property]};
if (isset($propertyValue)) {
$instance->$propertySetter(self::deserialize($propertyValue, $type, null));
}
}

From what I see, in all models DISCRIMINATOR is set to null, can we remove it from models and from here?

BREAKING CHANGE: API Methods returns tuple instead of models
BREAKING CHANGE: API Methods removed other than `getModel`
@Orkuncakilkaya Orkuncakilkaya force-pushed the feature/new-request-handling-logic-inter-767 branch from 0037c70 to 62e4ad3 Compare July 24, 2024 10:57
Copy link
Contributor

@TheUnderScorer TheUnderScorer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of great changes, thanks @Orkuncakilkaya! We're getting close to the finish 🙂

Orkuncakilkaya and others added 2 commits July 25, 2024 11:58
@Orkuncakilkaya Orkuncakilkaya requested a review from ilfa July 26, 2024 11:40
Copy link
Contributor

This PR will create a major release 🚀

5.0.0 (2024-07-26)

⚠ BREAKING CHANGES

  • API Methods now throws SerializationException
  • API Methods returns tuple instead of models
  • API Methods removed other than getModel
  • Upgraded minimum php version to 8.1
  • Request logic is rewritten, Upgraded minimum php version to 8.1

Features

  • change api to return tuple instead of serialized model (62e4ad3)
  • introduce rawResponse for getVisits and getEvent methods (9b01ba6)
  • introduce serialization exception (bfea23a)
  • only generate models and docs from swagger codegen (26e984f)
  • remove raw response and introduce with http info (ce2fedf)
  • rewrite request logic (0016822)
  • upgrade min php version to 8 (5698871)

@Orkuncakilkaya Orkuncakilkaya merged commit de05eb7 into develop Jul 30, 2024
9 checks passed
@Orkuncakilkaya Orkuncakilkaya deleted the feature/new-request-handling-logic-inter-767 branch July 30, 2024 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants