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 Svix Api client for csharp #248

Merged
merged 47 commits into from
Apr 21, 2022
Merged

Add Svix Api client for csharp #248

merged 47 commits into from
Apr 21, 2022

Conversation

wyldmagic-joshua
Copy link
Contributor

Motivation

Add Svix Api client support for csharp developers.

Solution

Implemented OpenApi generator for csharp to add generated client to pre-existing csharp Project/Solution. Added a wrapper around generated client to create a facade for an Api consumers.

@svix-frank svix-frank self-requested a review February 15, 2022 19:33
Copy link
Contributor

@svix-frank svix-frank left a comment

Choose a reason for hiding this comment

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

@IAmArchaic, thanks so much for the contribution, so far looking great, just left a few comments/questions before you move forward with other API tags

regen_openapi.sh Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
csharp/Svix/Models/Webhook.cs Outdated Show resolved Hide resolved
csharp/Svix/Models/SvixClientOptions.cs Outdated Show resolved Hide resolved
csharp/Svix/SvixClient.cs Outdated Show resolved Hide resolved
csharp/Svix/SvixClient.cs Outdated Show resolved Hide resolved
@wyldmagic-joshua
Copy link
Contributor Author

I think I got everything you were looking for @svix-frank. If you agree, I'll get started implementing the remaining Api resources.

@svix-frank
Copy link
Contributor

I think I got everything you were looking for @svix-frank. If you agree, I'll get started implementing the remaining Api resources.

💯 @IAmArchaic that was quick! Looks great!

@wyldmagic-joshua
Copy link
Contributor Author

@svix-frank, I've added Mock and refactored the unit tests to correctly test the Api wrappers without requiring a valid access token/serverUrl.

I also started implementing the Application Api resource.

Just wanted to open it up to any further comments or recommendations before I continue.

regen_openapi.sh Outdated Show resolved Hide resolved
@svix-frank
Copy link
Contributor

@svix-frank, I've added Mock and refactored the unit tests to correctly test the Api wrappers without requiring a valid access token/serverUrl.

I also started implementing the Application Api resource.

Just wanted to open it up to any further comments or recommendations before I continue.

everything looks great so far!

@tasn tasn added the lib/csharp C# client library label Feb 27, 2022
@tasn
Copy link
Member

tasn commented Mar 4, 2022

Hey @IAmArchaic, any updates? Very much looking forward to having this! :)

@wyldmagic-joshua
Copy link
Contributor Author

Hey @tasn, I'm expecting to make progress this weekend. Shouldn't have to wait too much longer. lol

@tasn
Copy link
Member

tasn commented Mar 4, 2022

No rush, but also yay! :)

Thanks a lot!

@tasn
Copy link
Member

tasn commented Mar 29, 2022

Hey @IAmArchaic, again no rush, but really looking forward to having it. :)

@wyldmagic-joshua
Copy link
Contributor Author

Hey @tasn, I believe all Api resources have now been implemented. All that should remain are Unit Tests.

@tasn
Copy link
Member

tasn commented Mar 30, 2022

Yay! 😍

Ready to take a look, or do are you still changing things?

@wyldmagic-joshua
Copy link
Contributor Author

You can certainly take a look/try it out. I'm planning to do a second-pass through the API client classes just in case before moving to tests.

@tasn
Copy link
Member

tasn commented Mar 31, 2022

So I tried taking a look, and I don't understand C# at all. :)

I can take a look at the tests, but do you have may simple code samples so I can check out the API? Or should I just rely on the tests.

@wyldmagic-joshua
Copy link
Contributor Author

So I tried taking a look, and I don't understand C# at all. :)

I can take a look at the tests, but do you have may simple code samples so I can check out the API? Or should I just rely on the tests.

Right now there are only the tests. They exist to validate that the wrapper created around the generated client is passing the parameters correctly. I can certainly create additional code samples to help!

@tasn
Copy link
Member

tasn commented Apr 1, 2022

I think I can probably figure it out from the tests, but yeah examples always help!

@tasn
Copy link
Member

tasn commented Apr 3, 2022

From what I was able to tell it looks good! Can you please verify and add tests for the verification based on existing Svix signatures (or e.g. ones taken from other examples).

Also, can you please rebase on top of main?

Excited to get it in!

@tasn
Copy link
Member

tasn commented Apr 11, 2022

\o/

Btw, please rebase when you have a moment (it looks like there aren't any real conflicts).

@wyldmagic-joshua
Copy link
Contributor Author

Hey @tasn , only a few more test classes to complete. Planning to finish them today! I'll rebase once I'm code complete.

@tasn
Copy link
Member

tasn commented Apr 11, 2022

Thanks a lot! Looking forward to having this in!! 🔥 🔥 🔥

@wyldmagic-joshua
Copy link
Contributor Author

Hey @tasn and @svix-frank, I've completed all Api client wrappers and unit tests for each one. There are currently 103 unit tests that validate the Api Wrappers are calling the generated open-api client methods correctly. These changes have also been rebased on main.

I haven't had the time to add additional examples, but the unit tests to demonstrate how to use the Csharp lib in its entirety. let me know what you think.

@tasn
Copy link
Member

tasn commented Apr 13, 2022

Looks amazing, I'll take a better look either later today or tomorrow morning! \o/

@tasn
Copy link
Member

tasn commented Apr 13, 2022

There are some lint errors it seems.

@tasn
Copy link
Member

tasn commented Apr 19, 2022

Want me to fix them for you?

@wyldmagic-joshua
Copy link
Contributor Author

Hey @tasn , I'm so sorry for missing your messages. I've been busy with work and newborn classes that they slipped through.

I can look at fixing them today.

@tasn
Copy link
Member

tasn commented Apr 20, 2022

No rush, just wanted to check on you and see if you need help!

@wyldmagic-joshua
Copy link
Contributor Author

@tasn , I ran the formatter used in the linter across the project. I think we should be good now.

@tasn
Copy link
Member

tasn commented Apr 21, 2022

Cool, thanks! Letting CI run, let's see!

@tasn tasn merged commit 6279b90 into svix:main Apr 21, 2022
@tasn
Copy link
Member

tasn commented Apr 21, 2022

It's in! Yay, thanks a lot!

@wyldmagic-joshua
Copy link
Contributor Author

You're most welcome! Reach out to me if I can be of further assistance.

@tasn
Copy link
Member

tasn commented Apr 24, 2022

Will do, thanks!

@tasn
Copy link
Member

tasn commented May 23, 2022

@IAmArchaic, I just realized we forgot to update the docs with the C# examples!

What's the C# equivalent of this JS snippet?

import { Svix } from "svix";

const svix = new Svix("AUTH_TOKEN");
const app = await svix.application.create({ name: "Application name" });

We just need this to make sure that we update the docs correctly.

@wyldmagic-joshua
Copy link
Contributor Author

wyldmagic-joshua commented May 23, 2022

@tasn, no worry! The equivalent in a .Net 6 console app would be

using Svix;
using Svix.Model;
using Svix.Models;

var svix = new SvixClient("AUTH_TOKEN", new SvixOptions("SERVER_URL"), null);

var app = await svix.Application.CreateAsync(
    new ApplicationIn("Application Name"), 
    new ApplicationCreateOptions {GetIfExists = true},
    "Idempotency Key");

@tasn
Copy link
Member

tasn commented May 23, 2022

Yay, thanks a lot! :)

@wyldmagic-joshua
Copy link
Contributor Author

You're welcome. Ping me again if you need something else.

@tasn
Copy link
Member

tasn commented May 23, 2022

I'll probably ping you once there's a PR out. :)

@tasn
Copy link
Member

tasn commented May 23, 2022

Creating a simple message (updated the www example):

using Svix;
using Svix.Model;
using Svix.Models;

var svix = new SvixClient("sk_IrlFPEh3VYctuyHhKTCxamGV", new SvixOptions("https://api.svix.com"));

// Send an event to Rock Inc's webhook endpoints
await svix.Message.CreateAsync(
    "app_Xzx8bQeOB1D1XEYmAJaRGoj0",
    new MessageIn(
        eventType: "user.created",
        payload: new {
            username = "new_user",
            email = "new_user@example.com",
        }
    ),
    new MessageCreateOptions {});

@tasn
Copy link
Member

tasn commented May 24, 2022

I just realized something: the functions require passing Options, is it possible to make them have a default value? Can you show me one example of how to change them and I'll change them everywhere? 🙏🏻

@tasn
Copy link
Member

tasn commented May 24, 2022

I also added svix/svix-docs#82 I hope it's correct. :)

@tasn
Copy link
Member

tasn commented May 25, 2022

Also added C# examples to https://api.svix.com/docs I hope those are correct too. :P

Please let me know if you have any comments.

@wyldmagic-joshua
Copy link
Contributor Author

@tasn the interface and classes can be updated to assign a default value for the options parameter. Here is an example for the Application Create.

Application.cs

public ApplicationOut Create(ApplicationIn application, ApplicationCreateOptions options = null, string idempotencyKey = default)

IApplication.cs

public ApplicationOut Create(ApplicationIn application, ApplicationCreateOptions options = null, string idempotencyKey = default);

@tasn
Copy link
Member

tasn commented May 25, 2022

Would null work, or would it break the code that uses options inside? That was my main thing. Should we have a default ApplicationCreateOptions created? If so, how do we do it?

@wyldmagic-joshua
Copy link
Contributor Author

The csharp live was designed to work with nulll and should not cause any trouble. For example, I've written every Api method with null propagation, here are some examples from the lib.

public ApplicationOut Create(ApplicationIn application, ApplicationCreateOptions options = null, string idempotencyKey = default)
        {
            try
            {
                application = application ?? throw new ArgumentNullException(nameof(application));

                var lApplication = _applicationApi.CreateApplicationApiV1AppPost(
                    application,
                    options?.GetIfExists ?? false,
                    idempotencyKey);

                return lApplication;
            }
            catch (ApiException e)
            {
                Logger?.LogError(e, $"{nameof(Create)} failed");

                if (Throw)
                    throw;

                return null;
            }
        }
public async Task<List<MessageOut>> ListAsync(string appId, MessageListOptions options, string idempotencyKey = default, CancellationToken cancellationToken = default)
        {
            try
            {
                var lResponse = await _messageApi.ListMessagesApiV1AppAppIdMsgGetAsync(
                    appId,
                    options?.Iterator,
                    options?.Limit,
                    options?.EventTypes,
                    options?.Channel,
                    options?.Before,
                    options?.After,
                    idempotencyKey,
                    cancellationToken);

                return lResponse?.Data;
            }
            catch (ApiException e)
            {
                Logger?.LogError(e, $"{nameof(ListAsync)} failed");

                if (Throw)
                    throw;

                return new List<MessageOut>();
            }
        }

@tasn
Copy link
Member

tasn commented May 25, 2022

OK, I'll open a PR to add nulls there, thanks!

@wyldmagic-joshua
Copy link
Contributor Author

If the given options is null the field off the options will not be evaluated and null will be passed into the Api method where the param will be omitted by auto-gen client.

@wyldmagic-joshua
Copy link
Contributor Author

OK, I'll open a PR to add nulls there, thanks!

Sorry for the trouble!

@tasn
Copy link
Member

tasn commented May 25, 2022

No trouble at all, I missed it at review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib/csharp C# client library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants