Skip to content

feat: cleaning up http client interface and adding the ability to use custom http clients #237

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 9 commits into from
Feb 11, 2025

Conversation

izaaz
Copy link
Collaborator

@izaaz izaaz commented Feb 5, 2025

Summary

  1. Moving http related classes into a package
  2. Refactoring the http client class to expose a single method called upload
  3. Creating an interface
  4. Testing it out with an OkHttp custom client

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?:

@izaaz izaaz changed the title Cleaning up http client interface and adding the ability to use custom http clients feat: Cleaning up http client interface and adding the ability to use custom http clients Feb 6, 2025
@izaaz izaaz changed the title feat: Cleaning up http client interface and adding the ability to use custom http clients feat: cleaning up http client interface and adding the ability to use custom http clients Feb 6, 2025
@izaaz izaaz marked this pull request as ready for review February 10, 2025 22:00
Copy link
Contributor

@igor-amp igor-amp left a comment

Choose a reason for hiding this comment

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

lgtm

@izaaz izaaz merged commit bddd9d7 into main Feb 11, 2025
4 checks passed
@izaaz izaaz deleted the AMP-100183/cleanup_http_client branch February 11, 2025 00:50
github-actions bot pushed a commit that referenced this pull request Feb 11, 2025
# [1.20.0](v1.19.4...v1.20.0) (2025-02-11)

### Features

* cleaning up http client interface and adding the ability to use custom http clients ([#237](#237)) ([bddd9d7](bddd9d7))
Copy link

🎉 This PR is included in version 1.20.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

val ampRequest = AnalyticsRequest(apiKey, events, diagnostics = diagnostics)
val formBody: RequestBody = RequestBody.create(mediaType, ampRequest.getBodyStr())
val request: Request =
Request.Builder().url("https://api.amplitude.com/2/httpapi").post(formBody).build()
Copy link

Choose a reason for hiding this comment

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

How are customers supposed to implement a custom API client properly without having access to getServerUrl() or minIdLength from the internal configuration? Hardcoding these values is definitely not a good option.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The initial plan was to make our http client an open class which can be extended. But we were running into a circular dependency.

The http client needs a configuration to be initialized and the custom http client needed to be passed into the configuration so that the Amplitude client can be configured to use a custom http client.

Since the HttpClient is an interface, you can define your class that takes in the Amplitude configuration to fetch all the properties from the configuration directly.

For example

class CustomClient() : HttpClientInterface {
    fun initialize(configuration: Configuration) {
        ....
    }

    // override upload here 
}

And in init

val customClient = CustomClient()
val config = Configuration(...)
customClient.initialize(config)
val amplitude = Amplitude(configuration)

I can update the example to show this and also open to suggestions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Something like this: #240

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants