Skip to content
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

Add X-Klaviyo- request headers and use Retry-After response header #149

Merged
merged 10 commits into from
Apr 10, 2024

Conversation

evan-masseau
Copy link
Contributor

@evan-masseau evan-masseau commented Mar 23, 2024

Description

Add new request headers, and make use of Retry-After response header on rate limited requests(429s)

Check List

  • Are you changing anything with the public API? NO
  • Are your changes backwards compatible with previous SDK Versions? YES
  • Have you tested this change on real device?
  • Have you added unit test coverage for your changes?
  • Have you verified that your changes are compatible with all Android versions the SDK currently supports?

Changelog / Code Overview

  • New request header to every API call that identifies SDK traffic by X-Klaviyo-Mobile=1
  • New request header to specify the attempt number by X-Klaviyo-Retry-Attempt=N/MAX
  • Check for Retry-After header on rate limited requests (previously unavailable for client/* 429s) and obey the server's retry timing, plus jitter.
  • Awaiting discussion: should we be persisting the attempt count to disk so that count does not reset on app re-launch.
    • Not adding persistence of attempt count at this time.

Sample of new request headers from test app:
Screenshot 2024-04-01 at 14 06 34

Test Plan

  • Added unit tests for the new headers
  • Consolidated and improved API Request tests
  • Added an API rate limit override for a test company to 1/min on the client/push_tokens endpoint and verified behavior in test app.

Related Issues/Tickets

CHNL-6842
CHNL-6835

@evan-masseau evan-masseau added do-not-merge Indicates a pull request is not yet ready for merging minor-release For issues/code that belong in a minor release per semantic versioning guidelines labels Mar 23, 2024
… of attempts.

Consolidated and improved API Request tests, added tests for new features.
@evan-masseau evan-masseau marked this pull request as ready for review March 23, 2024 17:16
@evan-masseau evan-masseau requested a review from a team as a code owner March 23, 2024 17:16
@evan-masseau evan-masseau marked this pull request as draft March 25, 2024 16:15
Evan Masseau added 2 commits April 1, 2024 13:18
# Conflicts:
#	sdk/analytics/src/main/java/com/klaviyo/analytics/networking/requests/KlaviyoApiRequest.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/KlaviyoTest.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/networking/KlaviyoApiClientTest.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/networking/requests/BaseRequestTest.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/networking/requests/EventApiRequestTest.kt
@@ -87,6 +90,17 @@ internal class KlaviyoApiClientTest : BaseRequestTest() {
}
}

@After
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Small thing, but I grouped setup and cleanup functions together for readability.

every { networkMonitorMock.isNetworkConnected() } returns true
every { configMock.networkTimeout } returns 1
every { configMock.networkFlushIntervals } returns longArrayOf(10_000L, 30_000L, 60_000L)
}

@After
Copy link
Contributor Author

Choose a reason for hiding this comment

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

for clarity, I annotated all setup/cleanup, even though overrides technically inherit it

import org.junit.Before
import org.junit.Test

internal abstract class BaseApiRequestTest<T> : BaseTest() where T : KlaviyoApiRequest {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Abstract base class for API request tests. The tests in this class get applied to all API Request tests that inherit from here.

Assert.assertEquals(expectedQuery, makeTestRequest().query)
}

inline fun <reified T> testJsonInterop(request: T) where T : KlaviyoApiRequest {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Couldn't quite get this to work as a @test with generics alone, so made a reusable function and call it in one-liner tests in the subclasses.

@evan-masseau evan-masseau changed the title [WIP] Add x-klaviyo headers [WIP] Add x-klaviyo headers and use Retry-After header for 429s if present Apr 1, 2024
@evan-masseau evan-masseau changed the title [WIP] Add x-klaviyo headers and use Retry-After header for 429s if present Add x-klaviyo request headers and use Retry-After response header Apr 1, 2024
@evan-masseau evan-masseau marked this pull request as ready for review April 1, 2024 21:04
@evan-masseau evan-masseau changed the title Add x-klaviyo request headers and use Retry-After response header Add X-Klaviyo- request headers and use Retry-After response header Apr 2, 2024
Use header name "X-Klaviyo-Attempt-Count"
Moved retry interval computation into Api Request since it is mainly driven by the header now
Additional tests
@evan-masseau evan-masseau removed the do-not-merge Indicates a pull request is not yet ready for merging label Apr 2, 2024
Evan Masseau and others added 2 commits April 8, 2024 10:18
- Add explicit test that API attempts count increments on send
- Use val instead of var for request headers, now that its a mutable map.
- Other cleanup
Copy link
Contributor

@ajaysubra ajaysubra left a comment

Choose a reason for hiding this comment

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

LGTM, just one question.

@evan-masseau evan-masseau added the do-not-merge Indicates a pull request is not yet ready for merging label Apr 9, 2024
@evan-masseau evan-masseau merged commit 9eca270 into master Apr 10, 2024
2 checks passed
@evan-masseau evan-masseau deleted the ecm/CHNL-6842-new-headers branch April 10, 2024 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge Indicates a pull request is not yet ready for merging minor-release For issues/code that belong in a minor release per semantic versioning guidelines
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants