Get icon indicating copy to clipboard operation
Get copied to clipboard

Post processing of raw response in delegate, like client(,willSendRequest:)

Open larryonoff opened this issue 2 years ago • 4 comments

Could you please add method to APIClientDelegate for post processing raw response?

larryonoff avatar Jan 25 '23 09:01 larryonoff

Something like this? What use case do you have in mind?

func client(_ client: APIClient, didReceiveResponse response: inout URLResponse, data: Data, task: URLSessionTask) throws

kean avatar Feb 15 '23 14:02 kean

Something like this? What use case do you have in mind?

func client(_ client: APIClient, didReceiveResponse response: inout URLResponse, data: Data, task: URLSessionTask) throws

Yes. Looks what I need.

My use case is setting cookies manually. See the code below.

  func postProcess(_ response: HTTPURLResponse) {
    if
      let url = response.url,
      let fields = response.allHeaderFields as? [String: String]
    {
      let cookies = HTTPCookie.cookies(
        withResponseHeaderFields: fields,
        for: url
      )

      let cookieStorage =
        client.session.configuration.httpCookieStorage ?? .shared

      for cookie in cookies {
        cookieStorage.setCookie(cookie)
      }
    }
  }

larryonoff avatar Feb 15 '23 15:02 larryonoff

Got it, thanks. Yes, I was thinking about adding a more comprehensive way of monitoring the session. The existing delegate is designed more for customizing the client behavior. By the way, you can use the existing validateResponse method as a stop-gap solution.

kean avatar Feb 15 '23 15:02 kean

Got it, thanks. Yes, I was thinking about adding a more comprehensive way of monitoring the session. The existing delegate is designed more for customizing the client behavior. By the way, you can use the existing validateResponse method as a stop-gap solution.

Thanks! For the moment I already use validateResponse

larryonoff avatar Feb 15 '23 16:02 larryonoff