kean / Get

Web API client built using async/await
MIT License
937 stars 74 forks source link

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

Open larryonoff opened 1 year ago

larryonoff commented 1 year ago

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

kean commented 1 year ago

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

func client(_ client: APIClient, didReceiveResponse response: inout URLResponse, data: Data, task: URLSessionTask) throws
larryonoff commented 1 year ago

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)
      }
    }
  }
kean commented 1 year ago

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.

larryonoff commented 1 year ago

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