android - How to add Authorization header to all Glide 4.x request -
in glide 3.x add register string model loader following:
public class glideservice /* implements glidemodule*/ { @override public void registercomponents(context context, glide glide) { glide.register(string.class, inputstream.class, new headeredloader.factory()); } private static class headeredloader extends baseglideurlloader<string> { public headeredloader(context context) { super(context); } @override protected string geturl(string model, int width, int height) { return model; } @override protected headers getheaders(string model, int width, int height) { lazyheaders.builder headersbuilder = new lazyheaders.builder(); if (buildconfig.flavor.equals("staging")) { string auth = "username:password"; string base64 = base64.encodetostring(auth.getbytes(), base64.no_wrap); headersbuilder.addheader("authorization", "basic " + base64); } return headersbuilder.build(); } public static class factory implements modelloaderfactory<string, inputstream> { @override public streammodelloader<string> build(context context, genericloaderfactory factories) { return new headeredloader(context); } @override public void teardown() { } } } }
but it's not clear docs how can accomplish lastest version.
any tips? thanks!
try
import com.bumptech.glide.load.options; import com.bumptech.glide.load.model.glideurl; import com.bumptech.glide.load.model.modelloader; import com.bumptech.glide.load.model.modelloaderfactory; import com.bumptech.glide.load.model.multimodelloaderfactory; import java.io.inputstream; import okhttp3.call; import okhttp3.okhttpclient; /** * simple model loader fetching media on http/https using okhttp. */ public class okhttpurlloader implements modelloader<glideurl, inputstream> { private final call.factory client; public okhttpurlloader(call.factory client) { this.client = client; } @override public boolean handles(glideurl url) { return true; } @override public loaddata<inputstream> buildloaddata(glideurl model, int width, int height, options options) { return new loaddata<>(model, new okhttpstreamfetcher(client, model)); } /** * default factory {@link okhttpurlloader}s. */ public static class factory implements modelloaderfactory<glideurl, inputstream> { private static volatile call.factory internalclient; private call.factory client; private static call.factory getinternalclient() { if (internalclient == null) { synchronized (factory.class) { if (internalclient == null) { internalclient = new okhttpclient(); } } } return internalclient; } /** * constructor new factory runs requests using static singleton client. */ public factory() { this(getinternalclient()); } /** * constructor new factory runs requests using given client. * * @param client typically instance of {@code okhttpclient}. */ public factory(call.factory client) { this.client = client; } @override public modelloader<glideurl, inputstream> build(multimodelloaderfactory multifactory) { return new okhttpurlloader(client); } @override public void teardown() { // nothing, instance doesn't own client. } } }`
Comments
Post a Comment