Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you implement a RequestAdapter in Alamofire 5?

Tags:

alamofire

In the Alamofire 5 Beta, SessionManager has been replaced by Session. I am wondering what the procedure is now for assigning a RequestAdapter, as this was a var in Alamofire 4, but in Alamofire 5 it's now a let constant. Hence the following code:

var session = Session()
session.adapter = AccessTokenAdapter

throws a compile-time error, "Cannot assign to property: 'adapter' is a 'let' constant"

The end goal is to set custom header fields such as JWT token.

like image 420
Fuad Kamal Avatar asked Jan 16 '19 22:01

Fuad Kamal


People also ask

What is session in Alamofire?

Session creates and manages Alamofire's Request types during their lifetimes. It also provides common functionality for all Request s, including queuing, interception, trust management, redirect handling, and response cache handling. default. Shared singleton instance used by all AF. request APIs.

What is Alamofire interceptor?

Alamofire's RequestInterceptor protocol ( RequestAdapter & RequestRetrier ) provides important and powerful request adaptation and retry features. It can be applied at both the Session and Request level.

Why was there Alamofire?

Advantages of Using AlamofireUsing Alamofire will give a cleaner project. API call interactions (POST/GET/PUT/etc.) will be easier and more understable. Alamofire simplifies a number of common networking tasks that makes development faster and easier.


1 Answers

You should conform to 'RequestInterceptor' protocol in your class, which have identical methods:

adapt and retry as in RequestAdapter and RequestRetrier.

Then you just pass it to request:

AF.request(urlRequest, interceptor: RequestInterceptor? = nil)
like image 139
karjaubayev Avatar answered Oct 16 '22 03:10

karjaubayev