Skip to content

Commit

Permalink
Fetch the ORCID access token
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid committed Nov 30, 2023
1 parent b341a66 commit 781de2b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type * as OrcidId from './OrcidId.js'
import * as Users from './Users.js'
import * as Zenodo from './Zenodo.js'

const processUser = (orcidId: OrcidId.OrcidId) =>
const processUser = ({ orcidId }: { orcidId: OrcidId.OrcidId; accessToken: string }) =>
Effect.gen(function* (_) {
yield* _(Effect.logInfo('Processing user'))

Expand Down
7 changes: 7 additions & 0 deletions src/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ export const scanStream = (
return Stream.fromAsyncIterable(redis.scanStream(options), error => new RedisError({ error }))
}),
).pipe(Stream.map(Schema.decodeSync(Schema.array(Schema.string))))

export const get = (key: IoRedis.RedisKey): Effect.Effect<Redis, RedisError, string | null> =>
Effect.gen(function* (_) {
const redis = yield* _(Redis)

return yield* _(Effect.tryPromise({ try: () => redis.get(key), catch: error => new RedisError({ error }) }))
})
14 changes: 12 additions & 2 deletions src/Users.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { type ParseResult, Schema } from '@effect/schema'
import { ReadonlyArray, type Scope, Stream, String, identity } from 'effect'
import { Effect, ReadonlyArray, type Scope, Stream, String, flow, identity } from 'effect'
import * as OrcidId from './OrcidId.js'
import * as Redis from './Redis.js'

export const getUsers: Stream.Stream<
Redis.Redis | Scope.Scope,
Redis.RedisError | ParseResult.ParseError,
OrcidId.OrcidId
{ orcidId: OrcidId.OrcidId; accessToken: string }
> = Redis.scanStream({
match: 'orcid-token:*',
}).pipe(
Stream.mapConcat(identity),
Stream.map(String.split(':')),
Stream.map(ReadonlyArray.lastNonEmpty),
Stream.flatMap(Schema.decodeEither(OrcidId.OrcidIdSchema)),
Stream.bindTo('orcidId'),
Stream.bind('accessToken', ({ orcidId }) => getAccessToken(orcidId)),
)

const OrcidTokenSchema = Schema.struct({ value: Schema.struct({ accessToken: Schema.string }) })

const getAccessToken = flow(
(orcidId: OrcidId.OrcidId) => Redis.get(`orcid-token:${orcidId}`),
Effect.flatMap(Schema.parse(Schema.ParseJson.pipe(Schema.compose(OrcidTokenSchema)))),
Effect.map(({ value }) => value.accessToken),
)

0 comments on commit 781de2b

Please sign in to comment.