square/okhttp

`OkHttpClient.Builder().cache` couldn't use because `okhttp3.Cache` constructor is internal

Closed this issue · 5 comments

When I try to define my request cache for OkHttpClient, it says "Cannot access 'constructor(directory: Path, maxSize: Long, fileSystem: FileSystem, taskRunner: TaskRunner): Cache': it is internal in 'okhttp3/Cache'."

OkHttpClient
    .Builder()
    .cache(
        Cache(  // Cannot access 'constructor(directory: Path, maxSize: Long, fileSystem: FileSystem, taskRunner: TaskRunner): Cache': it is internal in 'okhttp3/Cache'.
        "request_cache".toPath(),
        20L * 1024L * 1024L,
        FileSystem.SYSTEM,
    ),
).build()

Happens when I upgrade okhttp to 5.1.0.

What did you upgrade from?

It changed in https://github.com/square/okhttp/pull/8338/files#diff-bba5c0e6ada142a637fe1708e16d6f8219069e002232741024c2a75df6e688ea

If you want to use Path and FileSystem, then that's the API order in 5.x

But in 4.x it was only directory: File, maxSize: Long

https://github.com/square/okhttp/blob/okhttp_4x/okhttp/src/main/kotlin/okhttp3/Cache.kt

I upgraded from 5.0.0-alpha11 to 5.1.0, okhttp3.Cache is marked as internal since then, user code cannot use the class, the java doc of the class says I might need to use .cacheControl, but I didn't see how to migrate.

The APIs in alphas aren't guaranteed. Just use the new different order of the constructor

I'm sorry, did you really read what I'm asking? Well, that okhttp3.Cache class is now inaccessible by user code, I cannot find a proper way to refactor the .cache call to anything else.

But that's not true. Look at either the Code or the API files

https://github.com/square/okhttp/blob/master/okhttp/api/jvm/okhttp.api

public final class okhttp3/Cache : java/io/Closeable, java/io/Flushable {
	public static final field Companion Lokhttp3/Cache$Companion;
	public final fun -deprecated_directory ()Ljava/io/File;
	public fun <init> (Ljava/io/File;J)V
	public fun <init> (Lokio/FileSystem;Lokio/Path;J)V
class Cache internal constructor(
...
) : Closeable,
  Flushable {
  /** Create a cache of at most [maxSize] bytes in [directory]. */
  constructor(
    fileSystem: FileSystem,
    directory: Path,
    maxSize: Long,
  ) : this(
    directory,
    maxSize,
    fileSystem,
    TaskRunner.INSTANCE,
  )

  /** Create a cache of at most [maxSize] bytes in [directory]. */
  constructor(directory: File, maxSize: Long) : this(
    FileSystem.SYSTEM,
    directory.toOkioPath(),
    maxSize,
  )

I'm sorry, we can't offer 1:1 support with general build issues. Perhaps post on Stackoverflow.

If you think it's bug/broken API, please provide a reproducible project we can investigate.