Skip to content

Instantly share code, notes, and snippets.

@brianm
Created December 8, 2016 17:04
Show Gist options
  • Select an option

  • Save brianm/5358a1aa3e39b0ffe3467e19e4ec740c to your computer and use it in GitHub Desktop.

Select an option

Save brianm/5358a1aa3e39b0ffe3467e19e4ec740c to your computer and use it in GitHub Desktop.

Revisions

  1. brianm created this gist Dec 8, 2016.
    23 changes: 23 additions & 0 deletions CallThingie.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    public class HttpClient implements Call.Factory {

    private final OkHttpClient ok;

    HttpClient(final OkHttpClient ok) {
    this.ok = ok;
    }

    /**
    * Returns a CompletableCall, which supports CompletableFuture based enqueueing.
    */
    public CompletableCall newCall(final Request request) {
    return newCall(Ctx.fromThread().orElseGet(Ctx::empty), request);
    }

    /**
    * Returns a CompletableCall, which supports CompletableFuture based enqueueing
    */
    public CompletableCall newCall(Ctx ctx, final Request request) {
    OkHttpClient.Builder b = this.ok.newBuilder();
    b.interceptors().add(0, new CtxInterceptor(ctx));
    return new CompletableCall(ctx, b.build().newCall(request));
    }