Skip to content

Commit d54141b

Browse files
authored
Merge pull request #119 from coder/impl-custom-user-agent
impl: custom user agent for the HTTP client
2 parents 51bf92e + 16a34f8 commit d54141b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- a `Create workspace` button which links to Coder's templates page
1111
- workspace icons
1212
- quick toolbar action to open Coder Dashboard in the browser
13+
- custom user agent for the HTTP client
1314

1415
### Changed
1516

src/main/kotlin/com/coder/gateway/sdk/CoderRestClientService.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import com.coder.gateway.sdk.v2.models.WorkspaceBuild
1616
import com.coder.gateway.sdk.v2.models.WorkspaceTransition
1717
import com.google.gson.Gson
1818
import com.google.gson.GsonBuilder
19+
import com.intellij.ide.plugins.PluginManagerCore
1920
import com.intellij.openapi.components.Service
21+
import com.intellij.openapi.extensions.PluginId
22+
import com.intellij.openapi.util.SystemInfo
2023
import okhttp3.OkHttpClient
2124
import okhttp3.logging.HttpLoggingInterceptor
2225
import retrofit2.Retrofit
@@ -43,7 +46,14 @@ class CoderRestClientService {
4346
*/
4447
fun initClientSession(url: URL, token: String): User {
4548
val gson: Gson = GsonBuilder().registerTypeAdapter(Instant::class.java, InstantConverter()).setPrettyPrinting().create()
46-
httpClient = OkHttpClient.Builder().addInterceptor { it.proceed(it.request().newBuilder().addHeader("Coder-Session-Token", token).build()) }.addInterceptor(HttpLoggingInterceptor().apply { setLevel(HttpLoggingInterceptor.Level.BASIC) }).build()
49+
val pluginVersion = PluginManagerCore.getPlugin(PluginId.getId("com.coder.gateway"))!! // this is the id from the plugin.xml
50+
51+
httpClient = OkHttpClient.Builder()
52+
.addInterceptor { it.proceed(it.request().newBuilder().addHeader("Coder-Session-Token", token).build()) }
53+
.addInterceptor { it.proceed(it.request().newBuilder().addHeader("User-Agent", "Coder Gateway/${pluginVersion.version} (${SystemInfo.getOsNameAndVersion()}; ${SystemInfo.OS_ARCH})").build()) }
54+
// this should always be last if we want to see previous interceptors logged
55+
.addInterceptor(HttpLoggingInterceptor().apply { setLevel(HttpLoggingInterceptor.Level.BASIC) })
56+
.build()
4757

4858
retroRestClient = Retrofit.Builder().baseUrl(url.toString()).client(httpClient).addConverterFactory(GsonConverterFactory.create(gson)).build().create(CoderV2RestFacade::class.java)
4959

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy