This is a walkthrough on how to setup a project so we can run some http
requests against the Twitter API
with OCaml
. If you already now how to do this, you can skip to the next articles in this series. Have at it!
Setting up the OCaml Postman Twitter project
Download
opam
if you don't have it. Check the install instructions on the link on the opam home page.After you have it installed make a director for this project and
cd
into it:mkdir ocaml-twitter-postman && cd ocaml-twitter-postman
.We need to create a compiler specific to this project which we will do in our root directory which is the
/ocaml-twitter-postman
dir, by runningopam switch create . ocaml-base-compiler.4.08.1
. This part takes a while so go for a walk around the block. Thisswitch
is the one we will use to compile ourOCaml
code in all the examples. After the process finishes lets do what the output is telling us and runeval $(opam env)
to update the current shell environment so it knows to use this compiler.We are going to need
dune
to run some commands so let's install it with:opam install dune
.Now let's make a directory for first example,
mkdir hello-postman && cd !$
then create adune
file and initiatedune
. Thedune install
command we run below creates adune-project
file we wont ever look at again. Run:
touch dune && dune install
# running `tree` in your terminal gets you:
.
├── _build
│ └── log
├── dune
└── dune-project
1 directory, 3 files
Check out dune.build to get started there if you don't know that its not just a movie.
- Create a
hello_postman.ml
file in the root with the following contents:
Lwt_main.run (Lwt_io.printf "Hello, postman!\n")
- Configure the
dune
file by adding the following to it:
(executable
(name hello_postman)
(libraries lwt.unix))
- Lets add a
Makefile
to simplify some running commands. Runtouch Makefile
and the following to it:
INSTALL_ARGS := $(if $(PREFIX),--prefix $(PREFIX),)
default:
dune build
install:
dune install $(INSTALL_ARGS)
uninstall:
dune uninstall $(INSTALL_ARGS)
reinstall: uninstall install
clean:
dune clean
.PHONY: default install uninstall reinstall clean
I don't remember where I stole this file from but thank you, nonetheless.
- To build and run the project run the following commands from the
./hello-postman
directory.make
will run the script at the top of the make file then thedefault
argument in which is thebuild
command. This will install any dependencies we listed in ourdune
file then build the project. This is doing the same thing as when we randune install
above. So if you runmake clean
it will get rid of all the generated files. You can then just runmake
and your deps will be installed anddune-project
will be created. #NoteToSelf: Its worth remembering that we are using the compiler we created in the root repository.
make clean
make
dune exec ./hello_postman.exe
# output
➜ hello-postman make
dune build
Info: Creating file dune-project with this contents:
| (lang dune 2.1)
➜ hello-postman dune exec ./hello_postman.exe
Hello, postman!
- Add a
OCaml.gitignore
file. You can find thegithub
template here
The source code for this post is here.
That was quick. Check out Part 2 to get a bit more serious.
Latest comments (0)