How to get Enum.reduce until list get empty ElixirWhy are there two kinds of functions in Elixir?How to join strings in Elixir?How do you check for the type of variable in ElixirElixir: use vs importGetting the current date and or time in ElixirIn Elixir, is there any way to get a module to list its functions?How to run Elixir application?How to check if an item exists in an Elixir list or tuple?assert the size of the list in elixirElixir: How to get last n items in a list?

What prevents the use of a multi-segment ILS for non-straight approaches?

Why should universal income be universal?

What should you do if you miss a job interview (deliberately)?

What was the exact wording from Ivanhoe of this advice on how to free yourself from slavery?

GraphicsGrid with a Label for each Column and Row

Pre-mixing cryogenic fuels and using only one fuel tank

How can "mimic phobia" be cured or prevented?

Approximating irrational number to rational number

250 Floor Tower

Why did the Mercure fail?

Intuition of generalized eigenvector.

Open a doc from terminal, but not by its name

How do I color the graph in datavisualization?

"Spoil" vs "Ruin"

Why did the HMS Bounty go back to a time when whales are already rare?

How do you make your own symbol when Detexify fails?

Delivering sarcasm

How much character growth crosses the line into breaking the character

why `nmap 192.168.1.97` returns less services than `nmap 127.0.0.1`?

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?

How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?

Why does the Sun have different day lengths, but not the gas giants?

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

Creepy dinosaur pc game identification



How to get Enum.reduce until list get empty Elixir


Why are there two kinds of functions in Elixir?How to join strings in Elixir?How do you check for the type of variable in ElixirElixir: use vs importGetting the current date and or time in ElixirIn Elixir, is there any way to get a module to list its functions?How to run Elixir application?How to check if an item exists in an Elixir list or tuple?assert the size of the list in elixirElixir: How to get last n items in a list?













1















I am trying to get all companies list from Intercom. with per_page it doesn't give more than 60 at once. There is another endpoint as
https://api.intercom.io/companies/scroll, this gets scroll_param to get other companies and so on.



The scroll params come with the first request and then you can use it until the companies will become [].



I have created this method as



 defp companies(scroll_param) do
url = if scroll_param, do: "#@intercom_url/companies/scroll?scroll_param=#scroll_param", else: "#@intercom_url/companies/scroll"
headers = ["Authorization": "Bearer #@intercom_token", "Accept": "Accept:application/json"]
response = HTTPoison.get(url, headers) |> elem(1)
case response.status_code do
200 -> :ok, response
_ -> :error, response
end
end


And I am trying do such as



companies = companies(nil)
scroll_param = companies["scroll_param"]
all_companies =
Enum.reduce(companies["companies"], [], fn company, acc ->
if companies["companies"] == [] || is_nil(companies["companies"]) do
:halt, acc
else
acc ++ company["companies"]
end
end)


First, get all companies with nil scroll_param and then Do Enum.reduce until companies become [].



when there is no company left to show, the request gives



%"type":"company.list","companies":[],"scroll_param":"b85a7745-d423-49bf-91b2-72a513b781e4"


My problem is I don't know how to use recursion here, So I can get all the companies in on variable as all_companies



The first first request give such values



% "companies": [ "type":"company",
"company_id":"smithbrothersltd.co.uk",
"id":"5c7817185170c3ed1cf9d07a",
"app_id":"f9c1fd60de50d31bcbc3f4d8d74c9c6dbc40e95a",
"name":"Smith Brothers",
"remote_created_at":1551374104,
"created_at":1551374104,
"updated_at":1551795637,
"last_request_at":1551795636,
"monthly_spend":0,
"session_count":3,
"user_count":6,
"tags":"type":"tag.list","tags":[],
"segments":"type":"segment.list","segments":["type":"segment","id":"53834904c1bbf82df800b256"],"plan":,"custom_attributes":"creation_source":"api"
],
"scroll_param":"b85a7745-d423-49bf-91b2-72a513b781e4"










share|improve this question


























    1















    I am trying to get all companies list from Intercom. with per_page it doesn't give more than 60 at once. There is another endpoint as
    https://api.intercom.io/companies/scroll, this gets scroll_param to get other companies and so on.



    The scroll params come with the first request and then you can use it until the companies will become [].



    I have created this method as



     defp companies(scroll_param) do
    url = if scroll_param, do: "#@intercom_url/companies/scroll?scroll_param=#scroll_param", else: "#@intercom_url/companies/scroll"
    headers = ["Authorization": "Bearer #@intercom_token", "Accept": "Accept:application/json"]
    response = HTTPoison.get(url, headers) |> elem(1)
    case response.status_code do
    200 -> :ok, response
    _ -> :error, response
    end
    end


    And I am trying do such as



    companies = companies(nil)
    scroll_param = companies["scroll_param"]
    all_companies =
    Enum.reduce(companies["companies"], [], fn company, acc ->
    if companies["companies"] == [] || is_nil(companies["companies"]) do
    :halt, acc
    else
    acc ++ company["companies"]
    end
    end)


    First, get all companies with nil scroll_param and then Do Enum.reduce until companies become [].



    when there is no company left to show, the request gives



    %"type":"company.list","companies":[],"scroll_param":"b85a7745-d423-49bf-91b2-72a513b781e4"


    My problem is I don't know how to use recursion here, So I can get all the companies in on variable as all_companies



    The first first request give such values



    % "companies": [ "type":"company",
    "company_id":"smithbrothersltd.co.uk",
    "id":"5c7817185170c3ed1cf9d07a",
    "app_id":"f9c1fd60de50d31bcbc3f4d8d74c9c6dbc40e95a",
    "name":"Smith Brothers",
    "remote_created_at":1551374104,
    "created_at":1551374104,
    "updated_at":1551795637,
    "last_request_at":1551795636,
    "monthly_spend":0,
    "session_count":3,
    "user_count":6,
    "tags":"type":"tag.list","tags":[],
    "segments":"type":"segment.list","segments":["type":"segment","id":"53834904c1bbf82df800b256"],"plan":,"custom_attributes":"creation_source":"api"
    ],
    "scroll_param":"b85a7745-d423-49bf-91b2-72a513b781e4"










    share|improve this question
























      1












      1








      1








      I am trying to get all companies list from Intercom. with per_page it doesn't give more than 60 at once. There is another endpoint as
      https://api.intercom.io/companies/scroll, this gets scroll_param to get other companies and so on.



      The scroll params come with the first request and then you can use it until the companies will become [].



      I have created this method as



       defp companies(scroll_param) do
      url = if scroll_param, do: "#@intercom_url/companies/scroll?scroll_param=#scroll_param", else: "#@intercom_url/companies/scroll"
      headers = ["Authorization": "Bearer #@intercom_token", "Accept": "Accept:application/json"]
      response = HTTPoison.get(url, headers) |> elem(1)
      case response.status_code do
      200 -> :ok, response
      _ -> :error, response
      end
      end


      And I am trying do such as



      companies = companies(nil)
      scroll_param = companies["scroll_param"]
      all_companies =
      Enum.reduce(companies["companies"], [], fn company, acc ->
      if companies["companies"] == [] || is_nil(companies["companies"]) do
      :halt, acc
      else
      acc ++ company["companies"]
      end
      end)


      First, get all companies with nil scroll_param and then Do Enum.reduce until companies become [].



      when there is no company left to show, the request gives



      %"type":"company.list","companies":[],"scroll_param":"b85a7745-d423-49bf-91b2-72a513b781e4"


      My problem is I don't know how to use recursion here, So I can get all the companies in on variable as all_companies



      The first first request give such values



      % "companies": [ "type":"company",
      "company_id":"smithbrothersltd.co.uk",
      "id":"5c7817185170c3ed1cf9d07a",
      "app_id":"f9c1fd60de50d31bcbc3f4d8d74c9c6dbc40e95a",
      "name":"Smith Brothers",
      "remote_created_at":1551374104,
      "created_at":1551374104,
      "updated_at":1551795637,
      "last_request_at":1551795636,
      "monthly_spend":0,
      "session_count":3,
      "user_count":6,
      "tags":"type":"tag.list","tags":[],
      "segments":"type":"segment.list","segments":["type":"segment","id":"53834904c1bbf82df800b256"],"plan":,"custom_attributes":"creation_source":"api"
      ],
      "scroll_param":"b85a7745-d423-49bf-91b2-72a513b781e4"










      share|improve this question














      I am trying to get all companies list from Intercom. with per_page it doesn't give more than 60 at once. There is another endpoint as
      https://api.intercom.io/companies/scroll, this gets scroll_param to get other companies and so on.



      The scroll params come with the first request and then you can use it until the companies will become [].



      I have created this method as



       defp companies(scroll_param) do
      url = if scroll_param, do: "#@intercom_url/companies/scroll?scroll_param=#scroll_param", else: "#@intercom_url/companies/scroll"
      headers = ["Authorization": "Bearer #@intercom_token", "Accept": "Accept:application/json"]
      response = HTTPoison.get(url, headers) |> elem(1)
      case response.status_code do
      200 -> :ok, response
      _ -> :error, response
      end
      end


      And I am trying do such as



      companies = companies(nil)
      scroll_param = companies["scroll_param"]
      all_companies =
      Enum.reduce(companies["companies"], [], fn company, acc ->
      if companies["companies"] == [] || is_nil(companies["companies"]) do
      :halt, acc
      else
      acc ++ company["companies"]
      end
      end)


      First, get all companies with nil scroll_param and then Do Enum.reduce until companies become [].



      when there is no company left to show, the request gives



      %"type":"company.list","companies":[],"scroll_param":"b85a7745-d423-49bf-91b2-72a513b781e4"


      My problem is I don't know how to use recursion here, So I can get all the companies in on variable as all_companies



      The first first request give such values



      % "companies": [ "type":"company",
      "company_id":"smithbrothersltd.co.uk",
      "id":"5c7817185170c3ed1cf9d07a",
      "app_id":"f9c1fd60de50d31bcbc3f4d8d74c9c6dbc40e95a",
      "name":"Smith Brothers",
      "remote_created_at":1551374104,
      "created_at":1551374104,
      "updated_at":1551795637,
      "last_request_at":1551795636,
      "monthly_spend":0,
      "session_count":3,
      "user_count":6,
      "tags":"type":"tag.list","tags":[],
      "segments":"type":"segment.list","segments":["type":"segment","id":"53834904c1bbf82df800b256"],"plan":,"custom_attributes":"creation_source":"api"
      ],
      "scroll_param":"b85a7745-d423-49bf-91b2-72a513b781e4"







      elixir






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 7 at 7:54









      Junaid FarooqJunaid Farooq

      7201724




      7201724






















          2 Answers
          2






          active

          oldest

          votes


















          0














          This would probably be better done as a gen_server that could control the requests flow in case of errors (backoff, retries, etc), but that would mean knowing where it's being used and how. I don't think using Enum.reduce has any benefit over just declaring a module that does this. You would probably want to discern on the errors and status code other than 200 and explicitly decide if it's worth retrying, or logging the errors/status codes, etc. As it is it will machine gun 5 times in case of errors, fail on 5th consecutive error or non 200 response code. I'm assuming HTTPoison decodes the body as JSON when provided with the headers, otherwise you'll need to add a decoding step for the response too.



          defmodule MyApp.Intercom do

          @intercom_scroll_url @intercom_url <> "/companies/scroll?scroll_param="
          @intercom_base_url @intercom_url <> "/companies/scroll"
          @intercom_headers ["Authorization": "Bearer #@intercom_token", "Accept": "Accept:application/json"]

          @max_retries 5

          alias HTTPoison.Response, as: Resp

          def get_companies(acc \ [], scroll_param \ nil, errors \ [], retries \ 0)
          def get_companies(acc, scroll_param, errors, retries) when retries < @max_retries do
          case get_companies_request(scroll_param) do
          :ok, %Respstatus_code: 200, body: %"companies" => [] ->
          :ok, :lists.flatten(acc)

          :ok, %Respstatus_code: 200, body: %"companies" => companies, "scroll_param" => n_scroll_param ->
          get_companies([companies | acc], n_scroll_param, errors, 0)

          :ok, %Respstatus_code: sc, body: body ->
          :error, :unexpected_status_code, sc, body

          :error, %HTTPoison.Errorreason: reason ->
          get_companies(acc, scroll_param, [reason | errors], retries + 1)
          end
          end

          def get_companies(acc, _scroll_param, errors, _), do: :error, errors, acc

          defp get_companies_request(nil), do: HTTPoison.get(@intercom_base_url, @intercom_headers)
          defp get_companies_request(sp), do: HTTPoison.get(@intercom_scroll_url <> sp, @intercom_headers)
          end

          MyApp.Intercom.get_companies()





          share|improve this answer

























          • It just gave 100 companies. whereas 229 are available. what could be the problem you think?

            – Junaid Farooq
            Mar 7 at 16:51











          • also how are using scroll_params?

            – Junaid Farooq
            Mar 7 at 17:49











          • Also yes it dont decode JSON as well as your scroll_param is always empty

            – Junaid Farooq
            Mar 7 at 17:50











          • It should be decoding JSON otherwise it would match the 3rd clause. As for the scroll_param always being empty that's because the code as is is not extracting it on the 2nd clause, which you can do and substitute the scroll_param there

            – m3characters
            Mar 7 at 19:15


















          0














          If the scroll_param keeps the same across all the requests (except the first one, of course), then



          all_companies = [scroll_param] # The scroll param acquired by the first request
          |> Stream.cycle()
          |> Stream.map(&companies/1)
          |> Stream.take_while(fn
          :ok, %"companies" => [] -> false
          :ok, _ -> true
          :error, _ -> false
          end)
          |> Enum.map(fn:ok, %"companies" => companies -> companies end)


          Then you prepend the companies fetched by the first request and List.flatten the result.






          share|improve this answer























          • will that really work?

            – Junaid Farooq
            Mar 7 at 11:30










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55038669%2fhow-to-get-enum-reduce-until-list-get-empty-elixir%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          This would probably be better done as a gen_server that could control the requests flow in case of errors (backoff, retries, etc), but that would mean knowing where it's being used and how. I don't think using Enum.reduce has any benefit over just declaring a module that does this. You would probably want to discern on the errors and status code other than 200 and explicitly decide if it's worth retrying, or logging the errors/status codes, etc. As it is it will machine gun 5 times in case of errors, fail on 5th consecutive error or non 200 response code. I'm assuming HTTPoison decodes the body as JSON when provided with the headers, otherwise you'll need to add a decoding step for the response too.



          defmodule MyApp.Intercom do

          @intercom_scroll_url @intercom_url <> "/companies/scroll?scroll_param="
          @intercom_base_url @intercom_url <> "/companies/scroll"
          @intercom_headers ["Authorization": "Bearer #@intercom_token", "Accept": "Accept:application/json"]

          @max_retries 5

          alias HTTPoison.Response, as: Resp

          def get_companies(acc \ [], scroll_param \ nil, errors \ [], retries \ 0)
          def get_companies(acc, scroll_param, errors, retries) when retries < @max_retries do
          case get_companies_request(scroll_param) do
          :ok, %Respstatus_code: 200, body: %"companies" => [] ->
          :ok, :lists.flatten(acc)

          :ok, %Respstatus_code: 200, body: %"companies" => companies, "scroll_param" => n_scroll_param ->
          get_companies([companies | acc], n_scroll_param, errors, 0)

          :ok, %Respstatus_code: sc, body: body ->
          :error, :unexpected_status_code, sc, body

          :error, %HTTPoison.Errorreason: reason ->
          get_companies(acc, scroll_param, [reason | errors], retries + 1)
          end
          end

          def get_companies(acc, _scroll_param, errors, _), do: :error, errors, acc

          defp get_companies_request(nil), do: HTTPoison.get(@intercom_base_url, @intercom_headers)
          defp get_companies_request(sp), do: HTTPoison.get(@intercom_scroll_url <> sp, @intercom_headers)
          end

          MyApp.Intercom.get_companies()





          share|improve this answer

























          • It just gave 100 companies. whereas 229 are available. what could be the problem you think?

            – Junaid Farooq
            Mar 7 at 16:51











          • also how are using scroll_params?

            – Junaid Farooq
            Mar 7 at 17:49











          • Also yes it dont decode JSON as well as your scroll_param is always empty

            – Junaid Farooq
            Mar 7 at 17:50











          • It should be decoding JSON otherwise it would match the 3rd clause. As for the scroll_param always being empty that's because the code as is is not extracting it on the 2nd clause, which you can do and substitute the scroll_param there

            – m3characters
            Mar 7 at 19:15















          0














          This would probably be better done as a gen_server that could control the requests flow in case of errors (backoff, retries, etc), but that would mean knowing where it's being used and how. I don't think using Enum.reduce has any benefit over just declaring a module that does this. You would probably want to discern on the errors and status code other than 200 and explicitly decide if it's worth retrying, or logging the errors/status codes, etc. As it is it will machine gun 5 times in case of errors, fail on 5th consecutive error or non 200 response code. I'm assuming HTTPoison decodes the body as JSON when provided with the headers, otherwise you'll need to add a decoding step for the response too.



          defmodule MyApp.Intercom do

          @intercom_scroll_url @intercom_url <> "/companies/scroll?scroll_param="
          @intercom_base_url @intercom_url <> "/companies/scroll"
          @intercom_headers ["Authorization": "Bearer #@intercom_token", "Accept": "Accept:application/json"]

          @max_retries 5

          alias HTTPoison.Response, as: Resp

          def get_companies(acc \ [], scroll_param \ nil, errors \ [], retries \ 0)
          def get_companies(acc, scroll_param, errors, retries) when retries < @max_retries do
          case get_companies_request(scroll_param) do
          :ok, %Respstatus_code: 200, body: %"companies" => [] ->
          :ok, :lists.flatten(acc)

          :ok, %Respstatus_code: 200, body: %"companies" => companies, "scroll_param" => n_scroll_param ->
          get_companies([companies | acc], n_scroll_param, errors, 0)

          :ok, %Respstatus_code: sc, body: body ->
          :error, :unexpected_status_code, sc, body

          :error, %HTTPoison.Errorreason: reason ->
          get_companies(acc, scroll_param, [reason | errors], retries + 1)
          end
          end

          def get_companies(acc, _scroll_param, errors, _), do: :error, errors, acc

          defp get_companies_request(nil), do: HTTPoison.get(@intercom_base_url, @intercom_headers)
          defp get_companies_request(sp), do: HTTPoison.get(@intercom_scroll_url <> sp, @intercom_headers)
          end

          MyApp.Intercom.get_companies()





          share|improve this answer

























          • It just gave 100 companies. whereas 229 are available. what could be the problem you think?

            – Junaid Farooq
            Mar 7 at 16:51











          • also how are using scroll_params?

            – Junaid Farooq
            Mar 7 at 17:49











          • Also yes it dont decode JSON as well as your scroll_param is always empty

            – Junaid Farooq
            Mar 7 at 17:50











          • It should be decoding JSON otherwise it would match the 3rd clause. As for the scroll_param always being empty that's because the code as is is not extracting it on the 2nd clause, which you can do and substitute the scroll_param there

            – m3characters
            Mar 7 at 19:15













          0












          0








          0







          This would probably be better done as a gen_server that could control the requests flow in case of errors (backoff, retries, etc), but that would mean knowing where it's being used and how. I don't think using Enum.reduce has any benefit over just declaring a module that does this. You would probably want to discern on the errors and status code other than 200 and explicitly decide if it's worth retrying, or logging the errors/status codes, etc. As it is it will machine gun 5 times in case of errors, fail on 5th consecutive error or non 200 response code. I'm assuming HTTPoison decodes the body as JSON when provided with the headers, otherwise you'll need to add a decoding step for the response too.



          defmodule MyApp.Intercom do

          @intercom_scroll_url @intercom_url <> "/companies/scroll?scroll_param="
          @intercom_base_url @intercom_url <> "/companies/scroll"
          @intercom_headers ["Authorization": "Bearer #@intercom_token", "Accept": "Accept:application/json"]

          @max_retries 5

          alias HTTPoison.Response, as: Resp

          def get_companies(acc \ [], scroll_param \ nil, errors \ [], retries \ 0)
          def get_companies(acc, scroll_param, errors, retries) when retries < @max_retries do
          case get_companies_request(scroll_param) do
          :ok, %Respstatus_code: 200, body: %"companies" => [] ->
          :ok, :lists.flatten(acc)

          :ok, %Respstatus_code: 200, body: %"companies" => companies, "scroll_param" => n_scroll_param ->
          get_companies([companies | acc], n_scroll_param, errors, 0)

          :ok, %Respstatus_code: sc, body: body ->
          :error, :unexpected_status_code, sc, body

          :error, %HTTPoison.Errorreason: reason ->
          get_companies(acc, scroll_param, [reason | errors], retries + 1)
          end
          end

          def get_companies(acc, _scroll_param, errors, _), do: :error, errors, acc

          defp get_companies_request(nil), do: HTTPoison.get(@intercom_base_url, @intercom_headers)
          defp get_companies_request(sp), do: HTTPoison.get(@intercom_scroll_url <> sp, @intercom_headers)
          end

          MyApp.Intercom.get_companies()





          share|improve this answer















          This would probably be better done as a gen_server that could control the requests flow in case of errors (backoff, retries, etc), but that would mean knowing where it's being used and how. I don't think using Enum.reduce has any benefit over just declaring a module that does this. You would probably want to discern on the errors and status code other than 200 and explicitly decide if it's worth retrying, or logging the errors/status codes, etc. As it is it will machine gun 5 times in case of errors, fail on 5th consecutive error or non 200 response code. I'm assuming HTTPoison decodes the body as JSON when provided with the headers, otherwise you'll need to add a decoding step for the response too.



          defmodule MyApp.Intercom do

          @intercom_scroll_url @intercom_url <> "/companies/scroll?scroll_param="
          @intercom_base_url @intercom_url <> "/companies/scroll"
          @intercom_headers ["Authorization": "Bearer #@intercom_token", "Accept": "Accept:application/json"]

          @max_retries 5

          alias HTTPoison.Response, as: Resp

          def get_companies(acc \ [], scroll_param \ nil, errors \ [], retries \ 0)
          def get_companies(acc, scroll_param, errors, retries) when retries < @max_retries do
          case get_companies_request(scroll_param) do
          :ok, %Respstatus_code: 200, body: %"companies" => [] ->
          :ok, :lists.flatten(acc)

          :ok, %Respstatus_code: 200, body: %"companies" => companies, "scroll_param" => n_scroll_param ->
          get_companies([companies | acc], n_scroll_param, errors, 0)

          :ok, %Respstatus_code: sc, body: body ->
          :error, :unexpected_status_code, sc, body

          :error, %HTTPoison.Errorreason: reason ->
          get_companies(acc, scroll_param, [reason | errors], retries + 1)
          end
          end

          def get_companies(acc, _scroll_param, errors, _), do: :error, errors, acc

          defp get_companies_request(nil), do: HTTPoison.get(@intercom_base_url, @intercom_headers)
          defp get_companies_request(sp), do: HTTPoison.get(@intercom_scroll_url <> sp, @intercom_headers)
          end

          MyApp.Intercom.get_companies()






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 7 at 19:16

























          answered Mar 7 at 13:29









          m3charactersm3characters

          1,6712815




          1,6712815












          • It just gave 100 companies. whereas 229 are available. what could be the problem you think?

            – Junaid Farooq
            Mar 7 at 16:51











          • also how are using scroll_params?

            – Junaid Farooq
            Mar 7 at 17:49











          • Also yes it dont decode JSON as well as your scroll_param is always empty

            – Junaid Farooq
            Mar 7 at 17:50











          • It should be decoding JSON otherwise it would match the 3rd clause. As for the scroll_param always being empty that's because the code as is is not extracting it on the 2nd clause, which you can do and substitute the scroll_param there

            – m3characters
            Mar 7 at 19:15

















          • It just gave 100 companies. whereas 229 are available. what could be the problem you think?

            – Junaid Farooq
            Mar 7 at 16:51











          • also how are using scroll_params?

            – Junaid Farooq
            Mar 7 at 17:49











          • Also yes it dont decode JSON as well as your scroll_param is always empty

            – Junaid Farooq
            Mar 7 at 17:50











          • It should be decoding JSON otherwise it would match the 3rd clause. As for the scroll_param always being empty that's because the code as is is not extracting it on the 2nd clause, which you can do and substitute the scroll_param there

            – m3characters
            Mar 7 at 19:15
















          It just gave 100 companies. whereas 229 are available. what could be the problem you think?

          – Junaid Farooq
          Mar 7 at 16:51





          It just gave 100 companies. whereas 229 are available. what could be the problem you think?

          – Junaid Farooq
          Mar 7 at 16:51













          also how are using scroll_params?

          – Junaid Farooq
          Mar 7 at 17:49





          also how are using scroll_params?

          – Junaid Farooq
          Mar 7 at 17:49













          Also yes it dont decode JSON as well as your scroll_param is always empty

          – Junaid Farooq
          Mar 7 at 17:50





          Also yes it dont decode JSON as well as your scroll_param is always empty

          – Junaid Farooq
          Mar 7 at 17:50













          It should be decoding JSON otherwise it would match the 3rd clause. As for the scroll_param always being empty that's because the code as is is not extracting it on the 2nd clause, which you can do and substitute the scroll_param there

          – m3characters
          Mar 7 at 19:15





          It should be decoding JSON otherwise it would match the 3rd clause. As for the scroll_param always being empty that's because the code as is is not extracting it on the 2nd clause, which you can do and substitute the scroll_param there

          – m3characters
          Mar 7 at 19:15













          0














          If the scroll_param keeps the same across all the requests (except the first one, of course), then



          all_companies = [scroll_param] # The scroll param acquired by the first request
          |> Stream.cycle()
          |> Stream.map(&companies/1)
          |> Stream.take_while(fn
          :ok, %"companies" => [] -> false
          :ok, _ -> true
          :error, _ -> false
          end)
          |> Enum.map(fn:ok, %"companies" => companies -> companies end)


          Then you prepend the companies fetched by the first request and List.flatten the result.






          share|improve this answer























          • will that really work?

            – Junaid Farooq
            Mar 7 at 11:30















          0














          If the scroll_param keeps the same across all the requests (except the first one, of course), then



          all_companies = [scroll_param] # The scroll param acquired by the first request
          |> Stream.cycle()
          |> Stream.map(&companies/1)
          |> Stream.take_while(fn
          :ok, %"companies" => [] -> false
          :ok, _ -> true
          :error, _ -> false
          end)
          |> Enum.map(fn:ok, %"companies" => companies -> companies end)


          Then you prepend the companies fetched by the first request and List.flatten the result.






          share|improve this answer























          • will that really work?

            – Junaid Farooq
            Mar 7 at 11:30













          0












          0








          0







          If the scroll_param keeps the same across all the requests (except the first one, of course), then



          all_companies = [scroll_param] # The scroll param acquired by the first request
          |> Stream.cycle()
          |> Stream.map(&companies/1)
          |> Stream.take_while(fn
          :ok, %"companies" => [] -> false
          :ok, _ -> true
          :error, _ -> false
          end)
          |> Enum.map(fn:ok, %"companies" => companies -> companies end)


          Then you prepend the companies fetched by the first request and List.flatten the result.






          share|improve this answer













          If the scroll_param keeps the same across all the requests (except the first one, of course), then



          all_companies = [scroll_param] # The scroll param acquired by the first request
          |> Stream.cycle()
          |> Stream.map(&companies/1)
          |> Stream.take_while(fn
          :ok, %"companies" => [] -> false
          :ok, _ -> true
          :error, _ -> false
          end)
          |> Enum.map(fn:ok, %"companies" => companies -> companies end)


          Then you prepend the companies fetched by the first request and List.flatten the result.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 10:15









          AetherusAetherus

          6,6031628




          6,6031628












          • will that really work?

            – Junaid Farooq
            Mar 7 at 11:30

















          • will that really work?

            – Junaid Farooq
            Mar 7 at 11:30
















          will that really work?

          – Junaid Farooq
          Mar 7 at 11:30





          will that really work?

          – Junaid Farooq
          Mar 7 at 11:30

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55038669%2fhow-to-get-enum-reduce-until-list-get-empty-elixir%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved