Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
larryonoff opened this issue Jan 25, 2023 · 4 comments
Labels

Comments

@larryonoff
Copy link

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

@kean
Copy link
Owner

kean commented Feb 15, 2023

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
Copy link
Author

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
Copy link
Owner

kean commented Feb 15, 2023

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 kean added the feature label Feb 15, 2023
@larryonoff
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants