-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
135: Practice Exercise: Building a Type-Safe HTTP Client Wrapper
Why should I bother wrapping a client instead of just calling the library directly?
I've seen this a lot in production codebases: sttp or zio-http calls scattered across every single service class. It seems fine at first, but the moment you need to add a global correlation ID to your headers or change how you handle 401 Unauthorized errors, you're stuck doing a find-and-replace across fifty files. It's a nightmare.
By building a type-safe wrapper, you create a "boundary." The rest of your app doesn't need to know that you're using a specific library or how the JSON is being parsed. You're basically saying, "I don't care how the bytes move over the wire; I just want a User object or a clear explanation of why I didn't get one."
How do I make the response types generic so it works for any endpoint?
The trick here is combining generics with a decoder. You don't want a getUser method and a getPosts method that do the exact same thing. Instead, you want a single get method that takes a type parameter T.
Here is how I usually structure this. I'll assume we're using a JSON library like Circe for the decoding part:
case class ApiClient(baseUrl: String, httpClient: HttpClient) { def get[T](path: String)(implicit decoder: Decoder[T]): Either[ApiError, T] = { val response = httpClient.get(s"$baseUrl$path") response.body match { case Some(json) => decoder.decode(json).fold( err => Left(ApiError.DecodeError(err.getMessage)), data => Right(data) ) case None => Left(ApiError.EmptyResponse) } } }Notice the
(implicit decoder: Decoder[T]). This is the secret sauce. It tells Scala, "I can fetch this data for you, provided you can show me how to turn the resulting JSON into the typeTyou're asking for."How should I handle errors so I don't just crash on a 404 or bad JSON?
Please, for the love of your on-call rotation, don't use
throworTryfor API errors. HTTP failures are "expected" failures—they aren't exceptional crashes; they're just part of the business logic. I always use a sealed trait to represent the specific ways an API call can fail.It looks something like this:
sealed trait ApiError case class NetworkError(message: String) extends ApiError case class DecodeError(message: String) extends ApiError case class ServerError(code: Int, body: String) extends ApiError case object EmptyResponse extends ApiErrorBy returning
Either[ApiError, T], you force the developer using your client to actually think about the failure cases. They can't just ignore the possibility of a 404; the compiler won't let them get to theTwithout handling theApiErrorfirst. It makes your entire system significantly more resilient.
📋 Practical Task
Exercise: Implement a Type-Safe GitHub User Client
Your task is to build a wrapper for the GitHub API that fetches user profile information. You need to ensure the client is type-safe and handles errors gracefully.
- The Data Model: Create a case class
GitHubUserwith fields forlogin(String),id(Long), andpublic_repos(Int). - The Error Logic: Implement a sealed trait
GitHubErrorthat distinguishes between aUserNotFound(404), aRateLimitExceeded(403), and a genericUnexpectedError. - The Wrapper: Write a class
GitHubClientwith a methodfetchUser(username: String): Either[GitHubError, GitHubUser]. - The Requirement: The method must use a mock HTTP response (you can simulate this with a Map or a simple if/else block) to return a
GitHubUserif the username is "scala_dev", aUserNotFounderror if the username is "unknown", and aRateLimitExceedederror for any other input.
Focus on the type signatures and the way you map the HTTP status codes to your GitHubError types.
There are no comments for now.