ryanwinchester

ryanwinchester

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?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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.

ryanwinchester

ryanwinchester

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

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement