-
Notifications
You must be signed in to change notification settings - Fork 347
/
general.cheatmd
181 lines (136 loc) · 3.96 KB
/
general.cheatmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Basic Cheat Sheet
## Making Requests 101
### Creating a client
```elixir
client = Tesla.client([{Tesla.Middleware.BaseUrl, "https://httpbin.org"}])
Tesla.get(client, "/path")
```
### All Methods
```elixir
Tesla.get("https://httpbin.org/get")
Tesla.head("https://httpbin.org/anything")
Tesla.options("https://httpbin.org/anything")
Tesla.trace("https://httpbin.org/anything")
Tesla.post("https://httpbin.org/post", "body")
Tesla.put("https://httpbin.org/put", "body")
Tesla.patch("https://httpbin.org/patch", "body")
Tesla.delete("https://httpbin.org/anything")
```
### Query Params
```elixir
# GET /path?a=hi&b[]=1&b[]=2&b[]=3
Tesla.get("https://httpbin.org/anything", query: [a: "hi", b: [1, 2, 3]])
```
### Request Headers
```elixir
Tesla.get("https://httpbin.org/headers", headers: [{"x-api-key", "1"}])
```
### Client Default Headers
```elixir
client = Tesla.client([{Tesla.Middleware.Headers, [{"user-agent", "Tesla"}]}])
```
### Multipart
You can pass a `Tesla.Multipart` struct as the body:
```elixir
alias Tesla.Multipart
mp =
Multipart.new()
|> Multipart.add_content_type_param("charset=utf-8")
|> Multipart.add_field("field1", "foo")
|> Multipart.add_field("field2", "bar",
headers: [{"content-id", "1"}, {"content-type", "text/plain"}]
)
|> Multipart.add_file("test/tesla/multipart_test_file.sh")
|> Multipart.add_file("test/tesla/multipart_test_file.sh", name: "foobar")
|> Multipart.add_file_content("sample file content", "sample.txt")
{:ok, response} = Tesla.post("https://httpbin.org/post", mp)
```
## Streaming
### Streaming Request Body
If adapter supports it, you can pass a [Stream](https://hexdocs.pm/elixir/main/Stream.html)
as request body, e.g.:
```elixir
defmodule ElasticSearch do
def index(records_stream) do
stream = Stream.map(records_stream, fn record -> %{index: [some, data]} end)
Tesla.post(client(), "/_bulk", stream)
end
defp client do
Tesla.client([
{Tesla.Middleware.BaseUrl, "http://localhost:9200"},
Tesla.Middleware.JSON
], {Tesla.Adapter.Finch, name: MyFinch})
end
end
```
### Streaming Response Body
If adapter supports it, you can pass a `response: :stream` option to return
response body as a [Stream](https://hexdocs.pm/elixir/main/Stream.html)
```elixir
defmodule OpenAI do
def client(token) do
middleware = [
{Tesla.Middleware.BaseUrl, "https://api.openai.com/v1"},
{Tesla.Middleware.BearerAuth, token: token},
{Tesla.Middleware.JSON, decode_content_types: ["text/event-stream"]},
{Tesla.Middleware.SSE, only: :data}
]
Tesla.client(middleware, {Tesla.Adapter.Finch, name: MyFinch})
end
def completion(client, prompt) do
data = %{
model: "gpt-3.5-turbo",
messages: [%{role: "user", content: prompt}],
stream: true
}
Tesla.post(client, "/chat/completions", data, opts: [adapter: [response: :stream]])
end
end
client = OpenAI.new("<token>")
{:ok, env} = OpenAI.completion(client, "What is the meaning of life?")
env.body |> Stream.each(fn chunk -> IO.inspect(chunk) end)
```
## Middleware
### Custom middleware
```elixir
defmodule Tesla.Middleware.MyCustomMiddleware do
@moduledoc """
Short description what it does
Longer description, including e.g. additional dependencies.
### Options
- `:list` - all possible options
- `:with` - their default values
### Examples
client = Tesla.client([{Tesla.Middleware.MyCustomMiddleware, with: value}])
"""
@behaviour Tesla.Middleware
@impl Tesla.Middleware
def call(env, next, options) do
with %Tesla.Env{} = env <- preprocess(env) do
env
|> Tesla.run(next)
|> postprocess()
end
end
defp preprocess(env) do
env
end
defp postprocess({:ok, env}) do
{:ok, env}
end
def postprocess({:error, reason}) do
{:error, reason}
end
end
```
## Adapter
### Custom adapter
```elixir
defmodule Tesla.Adapter.MyCustomAdapter do
@behaviour Tesla.Adapter
@impl Tesla.Adapter
def run(env, opts) do
# do something
end
end
```