How would you do this the "Elixir Way?"

Like a recent previous poster, I like to write API clients. (because I consume a lot of APIs)

I have one I use a lot, written in PHP, and I’m trying to rewrite it in Elixir to help me learn. I am very new to Elixir and have only read the Introduction to Elixir book.

My question is this:

As a user of Elixir, how would you expect to interact with an API client like this?

This is a contrived example of how it is done in my PHP library:

<?php

$hubspot = new Hubspot($config);


# Use API client to get all contacts
$response = $hubspot->contacts()->all();


foreach ($response->contacts as $contact) {
    # Use API client to update contact, changing all firstnames to Joe
    $hubspot->contacts()->update($contact->vid, [
        [
            'name' => 'firstname',
            'value' => 'Joe'
        ]
    ]);
}

Using that example, is there an easy comparison?

2 Likes

Here’s an example of pulling some records from AWS’s DynamoDB, and updating each.

Dynamo.scan("users")
|> ExAws.stream!(config)
|> Stream.map(&Dynamo.put_object("users", %{&1 | name: "Joe"} ))
|> Stream.map(&ExAws.request!/1)

One issue with direct comparisons like this though is that I don’t know enough about what’s implied by updating each contact in your case to know at what level of abstraction we’re working. For example, you’ll notice that I explicitly have to mention the users table that we’re using from Dynamo. However I could also wrap that inside some User module, and abstract away explicit ExAws calls so that it looked more like

User
|> DB.stream
|> Stream.map(&User.update(&1, name: "Joe"))
|> DB.perform

There are endless possibilities.

5 Likes

Thanks.

For my example, all the update really does is send an HTTP request using another HTTP client library behind the scenes and returns the response:

https://github.com/ryanwinchester/hubspot-php/blob/master/src/Resources/Contacts.php#L21-L33

1 Like

So you need example of usage http client.
You can check:

3 Likes