Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Thoughts about HTTP layer
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Rostislav Zhuravsky
December 06, 2021
Programming
0
56
Thoughts about HTTP layer
Rostislav Zhuravsky
December 06, 2021
Tweet
Share
More Decks by Rostislav Zhuravsky
See All by Rostislav Zhuravsky
Why do we need code review?
woarewe
1
94
How to become a ReCaptcha v3 expert
woarewe
0
38
The evolution of service objects
woarewe
0
42
What's "wrong" with Ruby threads?
woarewe
0
41
What's wrong with ActiveRecord?
woarewe
0
36
Other Decks in Programming
See All in Programming
Rで始めるML・LLM活用入門
wakamatsu_takumu
0
140
Agent Skills Workshop - AIへの頼み方を仕組み化する
gotalab555
13
7.6k
DSPy入門 Pythonで実現する自動プロンプト最適化 〜人手によるプロンプト調整からの卒業〜
seaturt1e
1
440
Event Storming
hschwentner
3
1.3k
今、アーキテクトとして 品質保証にどう関わるか
nealle
0
200
コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions
handlename
0
130
Python’s True Superpower
hynek
0
190
米国のサイバーセキュリティタイムラインと見る Goの暗号パッケージの進化
tomtwinkle
1
370
JPUG勉強会 OSSデータベースの内部構造を理解しよう
oga5
2
220
並行開発のためのコードレビュー
miyukiw
2
2.1k
受け入れテスト駆動開発(ATDD)×AI駆動開発 AI時代のATDDの取り組み方を考える
kztakasaki
2
510
Ruby x Terminal
a_matsuda
5
550
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
83
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.6k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.9k
Bash Introduction
62gerente
615
210k
For a Future-Friendly Web
brad_frost
183
10k
My Coaching Mixtape
mlcsv
0
63
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.3k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
140
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
620
WENDY [Excerpt]
tessaabrams
9
36k
Transcript
How we have been taming Faraday
None
My first task
We use Faraday
What else can be used for HTTP calls?
More detailed overview
My experience
HTTParty
Faraday using class methods
Faraday using connection
Get back to the task
How bad requests are handled
The problematic code
If you wouldn’t approve it, please, raise a hand
What problems have we got?
Adding a new request requires remembering about a lot of
things: • Which statuses can be returned? • What errors can be thrown? • How to handle the errors? • What to do with a failed request? Retries?
Our codebase is heavily glued to Faraday • What if
Faraday API would be changed? • What if we would want to change the library? • What if we would need to fix/extend current implementation? • How to test it? Should we stub HTTP requests or mock objects?
Everything we’re trying to achieve is to get some data
HTTP is only an implementation detail
Transportation layer Business logic Gem API
Isolate all transportation details
Business logic Transportation layer Gem API
Isolate your dependencies
Business logic Transportation layer Library wrapper Gem API
Situation on the project Business logic Transportation layer Gem API
Our plan was • Look through all the places where
Faraday is used • Collect all the features we need to implement in our wrapper • Replace step by step Faraday with our library using feature toggles
Features we need to support • Retries • Follow redirects
• Raise an error on failure • Notify to Bugsnag • Parallel execution • Configure timeouts • Different request/response data format
An example of serial requests
An example of concurrent requests
How have we implemented that?
Faraday has a great feature: middleware
Faraday::Response::RaiseError
Retries
The hardest part was concurrent mode
We use Typhoeus for concurrent requests
Usage
Solution #1: Faraday supports Typhoeus adapter
None
Why is that problem for us?
Solution #2. Gem parallel
Final version
How to test an HTTP client?
None
Mock requests
Webmock
More about the topic
Summary • Write a high-level abstractions which correspond to actual
business rules • Isolate dependencies on gems/libs • Explicit is better than implicit a.k.a keep everything you can under your control • Write tests
Useful links • https://github.com/lostisland/faraday/issues/1310 • https://thoughtbot.com/upcase/videos/testing-interaction-with-3rd-party-apis • https://gist.github.com/woarewe/543a1275382941ffbb87324d2b302100 • https://gist.github.com/woarewe/81e2f8d181f99511985f91926d7f2f54
• https://gist.github.com/woarewe/16a33e3eecbd7e2af9622afb2898db28 •
Thanks