Skip to content

Commit a9d77ff

Browse files
committed
Use shared authenticated http client
1 parent 791e98b commit a9d77ff

File tree

1 file changed

+32
-18
lines changed
  • unison-cli/src/Unison/Codebase/Transcript

1 file changed

+32
-18
lines changed

unison-cli/src/Unison/Codebase/Transcript/Runner.hs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ withRunner ::
104104
(Runner -> m r) ->
105105
m r
106106
withRunner isTest verbosity ucmVersion action = do
107+
credMan <- AuthN.newCredentialManager
108+
authenticatedHTTPClient <- initTranscriptAuthenticatedHTTPClient credMan
109+
107110
-- If we're in a transcript test, configure the environment to use a non-existent fzf binary
108111
-- so that errors are consistent.
109112
-- This also prevents automated transcript tests from mistakenly opening fzf and waiting for user input.
@@ -121,19 +124,38 @@ withRunner isTest verbosity ucmVersion action = do
121124
(MCP.mcpServer mcpServerConfig)
122125
\case
123126
Nothing -> pure $ Left PortBindingFailure
124-
Just baseUrl ->
125-
either
126-
(pure . Left . ParseError)
127-
( run isTest verbosity codebase runtime sbRuntime ucmVersion $
128-
tShow @Server.BaseUrl baseUrl
129-
)
130-
$ Transcript.parse transcriptName transcriptSrc
127+
Just baseUrl -> do
128+
let baseUrlText = tShow @Server.BaseUrl baseUrl
129+
case (Transcript.parse transcriptName transcriptSrc) of
130+
Left parseError -> pure $ Left (ParseError parseError)
131+
Right stanzas ->
132+
run
133+
isTest
134+
verbosity
135+
codebase
136+
runtime
137+
sbRuntime
138+
ucmVersion
139+
baseUrlText
140+
authenticatedHTTPClient
141+
credMan
142+
stanzas
131143
where
132144
withRuntimes :: (Runtime.Runtime Symbol -> Runtime.Runtime Symbol -> m a) -> m a
133145
withRuntimes action =
134146
RTI.withRuntime False RTI.Persistent ucmVersion \runtime ->
135147
RTI.withRuntime True RTI.Persistent ucmVersion \sbRuntime ->
136148
action runtime sbRuntime
149+
initTranscriptAuthenticatedHTTPClient :: AuthN.CredentialManager -> m AuthN.AuthenticatedHttpClient
150+
initTranscriptAuthenticatedHTTPClient credMan = liftIO $ do
151+
mayShareAccessToken <- fmap Text.pack <$> lookupEnv accessTokenEnvVarKey
152+
let tokenProvider :: AuthN.TokenProvider
153+
tokenProvider =
154+
maybe
155+
(AuthN.newTokenProvider credMan)
156+
(\accessToken _codeserverID -> pure $ Right accessToken)
157+
mayShareAccessToken
158+
AuthN.newAuthenticatedHTTPClient tokenProvider ucmVersion
137159

138160
isGeneratedBlock :: ProcessedBlock -> Bool
139161
isGeneratedBlock = generated . getCommonInfoTags
@@ -147,9 +169,11 @@ run ::
147169
Runtime.Runtime Symbol ->
148170
UCMVersion ->
149171
Text ->
172+
AuthN.AuthenticatedHttpClient ->
173+
AuthN.CredentialManager ->
150174
Transcript ->
151175
IO (Either Error Transcript)
152-
run isTest verbosity codebase runtime sbRuntime ucmVersion baseURL transcript = UnliftIO.try do
176+
run isTest verbosity codebase runtime sbRuntime ucmVersion baseURL authenticatedHTTPClient credMan transcript = UnliftIO.try do
153177
let behaviors = extractBehaviors $ settings transcript
154178
let stanzas' = stanzas transcript
155179
httpManager <- HTTP.newManager HTTP.defaultManagerSettings
@@ -163,14 +187,6 @@ run isTest verbosity codebase runtime sbRuntime ucmVersion baseURL transcript =
163187
"Running the provided transcript file...",
164188
""
165189
]
166-
mayShareAccessToken <- fmap Text.pack <$> lookupEnv accessTokenEnvVarKey
167-
credMan <- AuthN.newCredentialManager
168-
let tokenProvider :: AuthN.TokenProvider
169-
tokenProvider =
170-
maybe
171-
(AuthN.newTokenProvider credMan)
172-
(\accessToken _codeserverID -> pure $ Right accessToken)
173-
mayShareAccessToken
174190
-- Queue of Stanzas and Just index, or Nothing if the stanza was programmatically generated
175191
-- e.g. a unison-file update by a command like 'edit'
176192
inputQueue <-
@@ -510,8 +526,6 @@ run isTest verbosity codebase runtime sbRuntime ucmVersion baseURL transcript =
510526
\issues."
511527
(_, _, _) -> pure ()
512528

513-
authenticatedHTTPClient <- AuthN.newAuthenticatedHTTPClient tokenProvider ucmVersion
514-
515529
seedRef <- newIORef (0 :: Int)
516530

517531
let env =

0 commit comments

Comments
 (0)