- Elixir Cookbook
- Paulo A Pereira
- 587字
- 2021-07-23 20:49:37
Managing dependencies
One of the advantages of OTP (more information on OTP may be found in Chapter 6, OTP – Open Telecom Platform) is modularity, and it is very common to have several applications running as dependencies of one application. An application is a way to achieve modularity; in this context, we call an application something that is known in other programming languages as a library. In this recipe, we will integrate an HTTP client with a new application. We will be using the Hex package manager (http://hex.pm).
Getting ready
- Generate a new application with
mix new manage_deps
:> mix new manage_deps
The output is shown in the following screenshot:
- Visit https://hex.pm/packages?search=http.
- We will choose HTTPoison (https://hex.pm/packages/httpoison).
How to do it…
To add a dependency to our application, we will follow these steps:
- Inside the
manage_deps
application, openmix.exs
and edit the file to include HTTPoison as a dependency:defp deps do [{:httpoison, "~> 0.4"}] end
- HTTPoison must be started with our system. Add this to the started applications list by including it inside the
application
function:def application do [applications: [:logger, :httpoison]] end
- Save
mix.exs
and runmix deps.get
to fetch the declared dependencies, as shown in this screenshot: - Compile the dependencies by executing
mix deps.compile
, as shown in the following screenshot: - Start your application with
iex –S mix
. - Inside the IEx session, check whether HTTPoison is running:
iex(1)> :application.which_applications [{:manage_deps, 'manage_deps', '0.0.1'}, {:httpoison, ' Yet Another HTTP client for Elixir powered by hackney\n', '0.4.2'}, {:hackney, 'simple HTTP client', '0.13.1'}(…)]
- Get Google's main page using HTTPoison:
iex(5)> HTTPoison.get("http://www.google.com") %HTTPoison.Response{body: "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>302 Moved</TITLE></HEAD><BODY>\n<H1>302 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.pt/?gfe_rd=cr&ei=WFAOVLvQFJSs8wfehYJg\">here</A>.\r\n</BODY></HTML>\r\n", headers: %{"Alternate-Protocol" => "80:quic", "Cache-Control" => "private", "Content-Length" => "256", "Content-Type" => "text/html; charset=UTF-8", "Date" => "Tue, 09 Sep 2014 00:56:56 GMT", "Location" => "http://www.google.pt/?gfe_rd=cr&ei=WFAOVLvQFJSs8wfehYJg", "Server" => "GFE/2.0"}, status_code: 302}
How it works…
Dependencies are preferably added using hex.pm (https://hex.pm/).
Tip
If an application doesn't yet exist in Hex, it is also possible to use a GitHub repository as a source.
To fetch a dependency from GitHub, instead of declaring the dependency with the {:httpoison, "~> 0.4"}
format, the following format is used:
{:httpoison, github: " edgurgel/httpoison "}
The local filesystem may also be configured as a source for dependencies, as follows:
{:httpotion, path: "path/to/httpotion"}
Once the dependencies are declared inside the mix.exs
file, there are Mix tasks to get, compile, and clean them. The dependencies are then fetched, and if these dependencies have more dependencies on themselves, Mix is smart enough to fetch them.
When compiling dependencies, Mix is also capable of figuring out whether any dependent application has its own dependencies and whether they need to be compiled.
Starting IEx with the –S
Mix loads the Mix environment inside IEx, and the application becomes accessible.
As shown in the Inspecting your system recipe, it is possible to get a list of running applications and check whether our dependency (and its own dependencies) are running. In the particular case of HTTPoison, automatic start is ensured by adding the atom representing the application name to the list under applications
([applications: [:logger, :httpoison]]
).
See also
- The documentation on Hex usage available at https://hex.pm/docs/usage.
- The Elixir documentation on Mix tasks is available at http://elixir-lang.org/docs/stable/mix/.
- C和C++安全編碼(原書第2版)
- Spring Boot+Spring Cloud+Vue+Element項(xiàng)目實(shí)戰(zhàn):手把手教你開發(fā)權(quán)限管理系統(tǒng)
- INSTANT Weka How-to
- 深入淺出Serverless:技術(shù)原理與應(yīng)用實(shí)踐
- C++20高級編程
- 深度學(xué)習(xí)入門:基于Python的理論與實(shí)現(xiàn)
- 軟件測試分析與實(shí)踐
- Python計(jì)算機(jī)視覺與深度學(xué)習(xí)實(shí)戰(zhàn)
- HTML5程序設(shè)計(jì)基礎(chǔ)教程
- Python繪圖指南:分形與數(shù)據(jù)可視化(全彩)
- TypeScript High Performance
- 現(xiàn)代JavaScript編程:經(jīng)典范例與實(shí)踐技巧
- HTML5+CSS+JavaScript深入學(xué)習(xí)實(shí)錄
- CorelDRAW X6中文版應(yīng)用教程(第二版)
- Yii框架深度剖析