SDWebImage / SDWebImageSwiftUI

SwiftUI Image loading and Animation framework powered by SDWebImage
https://sdwebimage.github.io/SDWebImageSwiftUI
MIT License
2.21k stars 230 forks source link

can't add a custom header #99

Closed mesheilah closed 4 years ago

mesheilah commented 4 years ago

I've read your wiki on how to set a default header, but the class SDWebImageDownloader itself isn't available with SDWebImageSwiftUI namespace, or there is maybe some kind of necessary import that I'm not aware of ? plus, I need to pass a user token with some images but not all images in the app, so it's not going to be a default header set in AppDelegate, but rather a custom header for some cases, I couldn't find a way to a pass a custom header with SDWebImageSwiftUI

dreampiggy commented 4 years ago

You need :

import SDWebImageSwiftUI
import SDWebImage

The SDWebImageSwiftUI does not have a @_exported import SDWebImage to automatically do this.

Maybe I can added one in 1.3.2 version ? Any idea or PR ?

@mesheilah

dreampiggy commented 4 years ago

Maybe this can help: #100


Note: Previous version you don't need import SDWebImage as well, if you use CocoaPods or Carthage. This is SwiftPM's only issue because SwiftPM can not contains Swift && Objc code at the same target.

We already import SDWebImage in SDWebImageSwiftUI.h umbrella headers (available in CocoaPods && Carthage user)

mesheilah commented 4 years ago

@dreampiggy yeah I thought it was an issue with SwiftPM too. However, regarding the custom header thing, is there a way to include it for some images rather than in AppDelegate for all images ?

dreampiggy commented 4 years ago

Maybe I don't correctly understand your use case ?

You mean: "Can I choose only use A decoder to this URL, instead of register the coder plugin globally" ?

Use context option [.imageCoder : SDImageSVGCoder.shared] solve this.

or means the custom header for single image URL request ?

Use context option [.requestModifier : MyHTTPHeaderModifier] solve this.

dreampiggy commented 4 years ago

Request Modifier is a powerful tool beyond just HTTP header. You can modify the HTTP Method (for example, HEAD, POST, instead of default GET method), HTTP Body, allowsCellularAccess, allowsExpensiveNetworkAccess for low data mode, etc.

mesheilah commented 4 years ago

Type 'SDWebImageContextOption' has no member 'requestModifier' error fires

mesheilah commented 4 years ago

it must be downloadRequestModifier not requestModifier this change makes it compile.

dreampiggy commented 4 years ago

Sorry the strange naming :)...In the future this context option will become a Swift enum with associated value.

See more https://github.com/SDWebImage/SDWebImage/issues/2980

mesheilah commented 4 years ago

so to wrap up, a simple usage for this option is:

[.downloadRequestModifier: SDWebImageDownloaderRequestModifier(block: { (request) -> URLRequest? in
                    var request = request
                    request.setValue("MyValue", forHTTPHeaderField: "MyHeader")
                    return request
                })]
dreampiggy commented 4 years ago

Yes. Use Swift closure syntax can simply to

[.downloadRequestModifier: SDWebImageDownloaderRequestModifier {
    var request = $0
    request.setValue("MyValue", forHTTPHeaderField: "MyHeader")
    return request
}]

The request modifier is powerful, and can be reused. So this is why it design like that.

Maybe can have a single convience one like .HTTPHeader : ["Foo" : "bar"] which translate to this in future Swift API.


But things become complicated when you pass a Dictionary, because HTTP / 1.1 allows the Header Key and Value to CAPITAL.

However, HTTP 2.0 disable CAPITAL and must in lower cases. (See HTTP 2 Spec, or some blogs: https://blog.yaakov.online/http-2-header-casing/) So, if you write:

let options = [.HTTPHeader : ["Foo": "Bar", "foo" : "bar"]]

This will cause a misunderstanding...Actually there will be only one foo:bar in HTTP 2 request.

dreampiggy commented 4 years ago

@mesheilah 1.3.2 version released. Please upgrade, so that you don't need to manually import SDWebImage.

dreampiggy commented 4 years ago

@mesheilah SDWebImage 5.8.0 released. Which added the convenient API for HTTP header request/response modifier. See full changelog: https://github.com/SDWebImage/SDWebImage/releases/tag/5.8.0