11defmodule ElixirPlayground.Airtable do
2- @ base_id "appCmysegOYgrUMsE"
3- @ table_name "tblILZeZPnCFsgxd4"
4- @ url "https://api.airtable.com/v0/#{ @ base_id } /#{ @ table_name } "
5-
62 @ default_page_size 100
73
84 def list_records ( opts \\ [ ] ) do
9- api_key = Application . get_env ( :elixir_playground , :airtable ) [ :api_key ]
5+ config = Application . get_env ( :elixir_playground , :airtable )
6+ base_id = config [ :base_id ]
7+ table_name = config [ :table_name ]
8+ api_key = config [ :api_key ]
109
10+ base_url = "https://api.airtable.com/v0/#{ base_id } /#{ table_name } "
1111 IO . inspect ( api_key )
1212
1313 headers = [
@@ -18,7 +18,7 @@ defmodule ElixirPlayground.Airtable do
1818 offset = Keyword . get ( opts , :offset , nil )
1919 page_size = Keyword . get ( opts , :page_size , @ default_page_size )
2020
21- url = build_url ( @ url , offset: offset , page_size: page_size )
21+ url = build_url ( base_url , offset: offset , page_size: page_size )
2222 IO . puts ( "Requesting Airtable API: #{ url } " )
2323 request = Finch . build ( :get , url , headers )
2424
@@ -34,6 +34,130 @@ defmodule ElixirPlayground.Airtable do
3434 end
3535 end
3636
37+ def get_record ( record_id ) do
38+ config = Application . get_env ( :elixir_playground , :airtable )
39+ base_id = config [ :base_id ]
40+ table_name = config [ :table_name ]
41+ api_key = config [ :api_key ]
42+
43+ url = "https://api.airtable.com/v0/#{ base_id } /#{ table_name } /#{ record_id } "
44+ headers = build_headers ( api_key )
45+
46+ IO . puts ( "Getting record from Airtable: #{ url } " )
47+ request = Finch . build ( :get , url , headers )
48+
49+ case Finch . request ( request , ElixirPlayground.Finch ) do
50+ { :ok , % Finch.Response { status: 200 , body: body } } ->
51+ { :ok , Jason . decode! ( body ) }
52+
53+ { :ok , % Finch.Response { status: 404 , body: _body } } ->
54+ { :error , :not_found }
55+
56+ { :ok , % Finch.Response { status: status , body: body } } ->
57+ { :error , % { status: status , body: Jason . decode! ( body ) } }
58+
59+ { :error , reason } ->
60+ { :error , reason }
61+ end
62+ end
63+
64+ def update_record ( record_id , fields ) do
65+ config = Application . get_env ( :elixir_playground , :airtable )
66+ base_id = config [ :base_id ]
67+ table_name = config [ :table_name ]
68+ api_key = config [ :api_key ]
69+
70+ url = "https://api.airtable.com/v0/#{ base_id } /#{ table_name } /#{ record_id } "
71+ headers = build_headers ( api_key )
72+
73+ body =
74+ % {
75+ "fields" => fields
76+ }
77+ |> Jason . encode! ( )
78+
79+ IO . puts ( "Updating record in Airtable: #{ url } " )
80+ request = Finch . build ( :patch , url , headers , body )
81+
82+ case Finch . request ( request , ElixirPlayground.Finch ) do
83+ { :ok , % Finch.Response { status: 200 , body: response_body } } ->
84+ { :ok , Jason . decode! ( response_body ) }
85+
86+ { :ok , % Finch.Response { status: 404 , body: _body } } ->
87+ { :error , :not_found }
88+
89+ { :ok , % Finch.Response { status: 422 , body: response_body } } ->
90+ { :error , % { status: 422 , type: :validation_error , body: Jason . decode! ( response_body ) } }
91+
92+ { :ok , % Finch.Response { status: status , body: response_body } } ->
93+ { :error , % { status: status , body: Jason . decode! ( response_body ) } }
94+
95+ { :error , reason } ->
96+ { :error , reason }
97+ end
98+ end
99+
100+ def delete_record ( record_id ) do
101+ config = Application . get_env ( :elixir_playground , :airtable )
102+ base_id = config [ :base_id ]
103+ table_name = config [ :table_name ]
104+ api_key = config [ :api_key ]
105+
106+ url = "https://api.airtable.com/v0/#{ base_id } /#{ table_name } /#{ record_id } "
107+ headers = build_headers ( api_key )
108+
109+ IO . puts ( "Deleting record from Airtable: #{ url } " )
110+ request = Finch . build ( :delete , url , headers )
111+
112+ case Finch . request ( request , ElixirPlayground.Finch ) do
113+ { :ok , % Finch.Response { status: 200 , body: response_body } } ->
114+ { :ok , Jason . decode! ( response_body ) }
115+
116+ { :ok , % Finch.Response { status: 404 , body: _body } } ->
117+ { :error , :not_found }
118+
119+ { :ok , % Finch.Response { status: status , body: response_body } } ->
120+ { :error , % { status: status , body: Jason . decode! ( response_body ) } }
121+
122+ { :error , reason } ->
123+ { :error , reason }
124+ end
125+ end
126+
127+ # Bonus: Create a new record
128+ def create_record ( fields ) do
129+ config = Application . get_env ( :elixir_playground , :airtable )
130+ base_id = config [ :base_id ]
131+ table_name = config [ :table_name ]
132+ api_key = config [ :api_key ]
133+
134+ url = "https://api.airtable.com/v0/#{ base_id } /#{ table_name } "
135+ headers = build_headers ( api_key )
136+
137+ body =
138+ % {
139+ "fields" => fields
140+ }
141+ |> Jason . encode! ( )
142+
143+ IO . puts ( "Creating record in Airtable: #{ url } " )
144+ request = Finch . build ( :post , url , headers , body )
145+
146+ case Finch . request ( request , ElixirPlayground.Finch ) do
147+ { :ok , % Finch.Response { status: 200 , body: response_body } } ->
148+ { :ok , Jason . decode! ( response_body ) }
149+
150+ { :ok , % Finch.Response { status: 422 , body: response_body } } ->
151+ { :error , % { status: 422 , type: :validation_error , body: Jason . decode! ( response_body ) } }
152+
153+ { :ok , % Finch.Response { status: status , body: response_body } } ->
154+ { :error , % { status: status , body: Jason . decode! ( response_body ) } }
155+
156+ { :error , reason } ->
157+ { :error , reason }
158+ end
159+ end
160+
37161 defp build_url ( base , opts ) do
38162 query_p =
39163 [
@@ -48,4 +172,11 @@ defmodule ElixirPlayground.Airtable do
48172 params -> base <> "?" <> URI . encode_query ( params )
49173 end
50174 end
175+
176+ defp build_headers ( api_key ) do
177+ [
178+ { "Authorization" , "Bearer #{ api_key } " } ,
179+ { "Content-Type" , "application/json" }
180+ ]
181+ end
51182end
0 commit comments