Skip to content

Commit

Permalink
feat: initSdk helper fn in @looker/extension-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
josephaxisa committed Feb 21, 2022
1 parent da01692 commit b50e8c2
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 60 deletions.
19 changes: 16 additions & 3 deletions packages/api-explorer/src/StandaloneApiExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@

import type { FC } from 'react'
import React, { useEffect, useState } from 'react'
import { initRunItSdk, RunItProvider } from '@looker/run-it'
import { RunItConfigKey, RunItProvider } from '@looker/run-it'
import type { OAuthConfigProvider } from '@looker/extension-utils'
import { OAuthScene } from '@looker/extension-utils'
import { initSdk, OAuthScene } from '@looker/extension-utils'
import { Provider } from 'react-redux'
import { useLocation } from 'react-router'
import { functionalSdk40 } from '@looker/sdk'
import type { BrowserSession } from '@looker/sdk-rtl'

import { ApiExplorer } from './ApiExplorer'
import { store } from './state'
Expand All @@ -46,8 +48,19 @@ export const StandaloneApiExplorer: FC<StandaloneApiExplorerProps> = ({
headless = false,
}) => {
const [browserAdaptor] = useState(
new ApixAdaptor(initRunItSdk(), window.origin)
() =>
new ApixAdaptor(
initSdk({
agentTag: 'RunIt 0.8',
configKey: RunItConfigKey,
clientId: 'looker.api-explorer',
createSdkCallback: (session: BrowserSession) =>
functionalSdk40(session),
}),
window.origin
)
)

const location = useLocation()
const oauthReturn = location.pathname === `/${oAuthPath}`
const sdk = browserAdaptor.sdk
Expand Down
4 changes: 2 additions & 2 deletions packages/api-explorer/src/state/specs/sagas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
*/
import ReduxSagaTester from 'redux-saga-tester'
import { registerTestEnvAdaptor } from '@looker/extension-utils'
import { initRunItSdk } from '@looker/run-it'
import cloneDeep from 'lodash/cloneDeep'
import { ApixAdaptor } from '../../utils'

import { getLoadedSpecs } from '../../test-data'
import { testSdk } from '../../test-utils'
import { specActions, specsSlice } from './slice'
import * as sagas from './sagas'

describe('Specs Sagas', () => {
let sagaTester: ReduxSagaTester<any>
const adaptor = new ApixAdaptor(initRunItSdk(), '')
const adaptor = new ApixAdaptor(testSdk, '')
registerTestEnvAdaptor(adaptor)
const specState = getLoadedSpecs()
const mockError = new Error('boom')
Expand Down
1 change: 1 addition & 0 deletions packages/api-explorer/src/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
export * from './lodes'
export * from './router'
export * from './redux'
export * from './sdk'
4 changes: 2 additions & 2 deletions packages/api-explorer/src/test-utils/redux.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import { renderWithTheme } from '@looker/components-test-utils'
import type { RenderOptions } from '@testing-library/react'
import { createStore } from '@looker/redux'
import { BrowserAdaptor, registerEnvAdaptor } from '@looker/extension-utils'
import { initRunItSdk } from '@looker/run-it'

import type { LodesState, RootState, SettingState, SpecState } from '../state'
import {
Expand All @@ -44,13 +43,14 @@ import {
specsSlice,
} from '../state'
import { specState } from '../test-data'
import { testSdk } from './sdk'
import { renderWithRouter } from './router'

export const withReduxProvider = (
consumers: ReactElement<any>,
store: Store<RootState> = createTestStore()
) => {
registerEnvAdaptor(new BrowserAdaptor(initRunItSdk()))
registerEnvAdaptor(new BrowserAdaptor(testSdk))
return <Provider store={store}>{consumers}</Provider>
}

Expand Down
35 changes: 35 additions & 0 deletions packages/api-explorer/src/test-utils/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
MIT License
Copyright (c) 2021 Looker Data Sciences, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import { initSdk } from '@looker/extension-utils'
import type { BrowserSession } from '@looker/sdk-rtl'
import { functionalSdk40 } from '@looker/sdk'

export const testSdk = initSdk({
agentTag: 'RunIt',
configKey: 'ConfigKey',
clientId: 'looker.api-explorer',
createSdkCallback: (session: BrowserSession) => functionalSdk40(session),
})
1 change: 1 addition & 0 deletions packages/extension-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@looker/components": "^2.8.1",
"@looker/extension-sdk": "^22.0.0",
"@looker/extension-sdk-react": "^22.0.0",
"@looker/sdk": "^22.0.0",
"@looker/sdk-rtl": "^21.3.2",
"react": "^16.13.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,25 @@
SOFTWARE.
*/

import type { IApiSettings } from '@looker/sdk-rtl'
import {
BrowserSession,
BrowserTransport,
DefaultSettings,
} from '@looker/sdk-rtl'
import { functionalSdk40 } from '@looker/sdk'
import { OAuthConfigProvider } from '@looker/extension-utils'

import { RunItConfigKey } from '../components'

export const initRunItSdk = () => {
const settings = {
...DefaultSettings(),
base_url: 'https://self-signed.looker.com:19999',
agentTag: 'RunIt 0.8',
} as IApiSettings

const options = new OAuthConfigProvider(settings, RunItConfigKey)
const transport = new BrowserTransport(options)
const session = new BrowserSession(options, transport)
const sdk = functionalSdk40(session)
BrowserTransport.trackPerformance = true
return sdk
}

export enum StoreConstants {
LOCALSTORAGE_SETTINGS_KEY = 'settings',
}
import type { BrowserSession } from '@looker/sdk-rtl'

import type { InitSdkSettings } from './authUtils'
import { initSdk } from './authUtils'

describe('Auth utilities', () => {
describe('initSdk', () => {
it('should initialize with provided settings', () => {
const settings: InitSdkSettings = {
agentTag: 'RunIt',
configKey: 'RunItConfigKey',
clientId: 'looker.api-explorer',
createSdkCallback: (session: BrowserSession) =>
functionalSdk40(session),
trackPerformance: false,
}
const actual = initSdk(settings)
expect(actual.authSession.settings.agentTag).toEqual(settings.agentTag)
})
})
})
52 changes: 48 additions & 4 deletions packages/extension-utils/src/authUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
SOFTWARE.
*/
import type { IApiSection, IApiSettings } from '@looker/sdk-rtl'
import { ApiSettings } from '@looker/sdk-rtl'
import type { APIMethods, IApiSection, IApiSettings } from '@looker/sdk-rtl'
import {
ApiSettings,
DefaultSettings,
BrowserTransport,
BrowserSession,
} from '@looker/sdk-rtl'

export type StorageLocation = 'session' | 'local'

Expand All @@ -42,7 +47,9 @@ export interface IStorageValue {
export class OAuthConfigProvider extends ApiSettings {
constructor(
settings: Partial<IApiSettings>,
private readonly configKey: string
/** local storage key for retrieving auth config */
private readonly configKey: string,
private readonly clientId: string
) {
super(settings)
}
Expand Down Expand Up @@ -108,9 +115,46 @@ export class OAuthConfigProvider extends ApiSettings {
...{
base_url,
looker_url,
client_id: 'looker.api-explorer',
client_id: this.clientId,
redirect_uri: `${window.location.origin}/oauth`,
},
}
}
}

export interface InitSdkSettings {
/** agent tag to use for the SDK requests */
agentTag: string
/** Local storage key for retrieving auth configuration settings */
configKey: string
/** Id used to register this application on the Looker server */
clientId: string
/** A callback function that returns an sdk instance */
createSdkCallback: (session: BrowserSession) => APIMethods
/** Track request performance if the browser has the necessary APIs. Defaults to false. */
trackPerformance?: boolean
}

/**
* Initializes an sdk with provided settings
* @returns An sdk instance
*/
export const initSdk = (initSettings: InitSdkSettings) => {
const settings = {
...DefaultSettings(),
/** Albeit not used, this has to be set otherwise ApiSettings throws */
base_url: 'https://localhost:8080',
agentTag: initSettings.agentTag,
} as IApiSettings

const options = new OAuthConfigProvider(
settings,
initSettings.configKey,
initSettings.clientId
)
const transport = new BrowserTransport(options)
const session = new BrowserSession(options, transport)
const sdk = initSettings.createSdkCallback(session)
BrowserTransport.trackPerformance = initSettings.trackPerformance ?? false
return sdk
}
3 changes: 1 addition & 2 deletions packages/run-it/src/RunIt.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ import type { ApiModel, IMethod } from '@looker/sdk-codegen'
import { BrowserAdaptor, OAuthConfigProvider } from '@looker/extension-utils'
import { RunIt } from './RunIt'
import { api, testTextResponse } from './test-data'
import { initRunItSdk } from './utils'
import { testSdk as sdk } from './test-utils'
import { RunItProvider } from './RunItProvider'

describe('RunIt', () => {
const run = 'Run'
const sdk = initRunItSdk()
const adaptor = new BrowserAdaptor(sdk)

const renderRunIt = (
Expand Down
4 changes: 2 additions & 2 deletions packages/run-it/src/components/ConfigForm/ConfigForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import { renderWithTheme } from '@looker/components-test-utils'
import userEvent from '@testing-library/user-event'
import { BrowserAdaptor, registerTestEnvAdaptor } from '@looker/extension-utils'

import { initRunItSdk } from '../..'
import { testSdk } from '../../test-utils'
import { ConfigForm, RunItConfigKey } from '.'

describe('ConfigForm', () => {
const adaptor = new BrowserAdaptor(initRunItSdk())
const adaptor = new BrowserAdaptor(testSdk)
registerTestEnvAdaptor(adaptor)

const apiLabel = /API server URL/i
Expand Down
6 changes: 3 additions & 3 deletions packages/run-it/src/components/LoginForm/LoginForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ import {
OAuthConfigProvider,
} from '@looker/extension-utils'

import { initRunItSdk, readyToLogin } from '../..'
import { readyToLogin } from '../..'
import { testSdk } from '../../test-utils'
import { LoginForm, notReadyToLogin } from './LoginForm'

describe('LoginForm', () => {
const sdk = initRunItSdk()
registerTestEnvAdaptor(new BrowserAdaptor(sdk))
registerTestEnvAdaptor(new BrowserAdaptor(testSdk))

test('it renders a login form with instructions if auth is not configured', async () => {
renderWithTheme(<LoginForm requestContent={{}} />)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,4 @@
SOFTWARE.
*/

import { initRunItSdk } from '@looker/run-it'

describe('RunItSDK', () => {
it('should keep agentTag', () => {
const actual = initRunItSdk()
expect(actual.authSession.settings.agentTag).toContain('RunIt')
})
})
export { testSdk } from './sdk'
36 changes: 36 additions & 0 deletions packages/run-it/src/test-utils/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
MIT License
Copyright (c) 2021 Looker Data Sciences, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import { initSdk } from '@looker/extension-utils'
import type { BrowserSession } from '@looker/sdk-rtl'
import { functionalSdk40 } from '@looker/sdk'

export const testSdk = initSdk({
agentTag: 'RunIt',
configKey: 'ConfigKey',
clientId: 'looker.api-explorer',
createSdkCallback: (session: BrowserSession) => functionalSdk40(session),
})
2 changes: 1 addition & 1 deletion packages/run-it/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
SOFTWARE.
*/
export * from './RunItSDK'
export * from './requestUtils'
export * from './settings'
4 changes: 1 addition & 3 deletions packages/run-it/src/utils/requestUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@
*/
import type { RunItInput } from '../RunIt'
import { testJsonResponse, api } from '../test-data'
import { testSdk as sdk } from '../test-utils'
import {
createRequestParams,
pathify,
runRequest,
createInputs,
initRequestContent,
} from './requestUtils'
import { initRunItSdk } from './RunItSDK'

const sdk = initRunItSdk()

describe('requestUtils', () => {
afterEach(() => {
Expand Down
Loading

0 comments on commit b50e8c2

Please sign in to comment.