Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show full request url in RunIt response #906

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,410 changes: 1,779 additions & 1,631 deletions examplesIndex.json

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions packages/run-it/src/RunIt.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('RunIt', () => {
jest.clearAllMocks()
})

test('it renders endpoint, request and response tabs, and form inputs', () => {
it('it renders endpoint, request and response tabs, and form inputs', () => {
renderRunIt()
// TODO fix this
// expect(screen.getByRole('heading')).toHaveTextContent(
Expand All @@ -93,7 +93,7 @@ describe('RunIt', () => {
).not.toBeInTheDocument()
})

test('the form submit handler invokes the request callback on submit', async () => {
it('the form submit handler invokes the request callback on submit', async () => {
renderRunIt(api, api.methods.me)
const defaultRequestCallback = jest
.spyOn(sdk.authSession.transport, 'rawRequest')
Expand All @@ -106,10 +106,15 @@ describe('RunIt', () => {
expect(
screen.queryByText(testTextResponse.body.toString())
).toBeInTheDocument()
expect(
screen.getByRole('heading', {
name: 'GET https://self-signed.looker.com:19999/api/4.0/user',
})
).toBeInTheDocument()
})
})

test('run_inline_query has required body parameters', async () => {
it('run_inline_query has required body parameters', async () => {
renderRunIt()
const defaultRequestCallback = jest
.spyOn(sdk.authSession.transport, 'rawRequest')
Expand All @@ -135,7 +140,7 @@ describe('RunIt', () => {
})
})

test('it has Configure button', () => {
it('it has Configure button', () => {
renderRunIt()
expect(
screen.getByRole('button', { name: 'Configure' })
Expand All @@ -158,7 +163,7 @@ describe('RunIt', () => {
})
})

test('it has Login button', () => {
it('it has Login button', () => {
renderRunIt()
expect(screen.getByRole('button', { name: 'Login' })).toBeInTheDocument()
expect(
Expand Down
31 changes: 24 additions & 7 deletions packages/run-it/src/RunIt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ import {
initRequestContent,
createRequestParams,
runRequest,
pathify,
sdkNeedsConfig,
prepareInputs,
sdkNeedsAuth,
createInputs,
fullRequestUrl,
} from './utils'
import type { RunItSetter } from '.'
import { runItNoSet, RunItContext } from '.'
Expand Down Expand Up @@ -108,6 +108,10 @@ interface RunItProps {
sdkLanguage?: string
}

// interface SpecParams {
// specKey: string
// }

/**
* Given an array of inputs, a method, and an api model it renders a REST request form
* which on submit performs a REST request and renders the response with the appropriate MIME type handler
Expand All @@ -118,14 +122,15 @@ export const RunIt: FC<RunItProps> = ({
setVersionsUrl = runItNoSet,
sdkLanguage = 'All',
}) => {
// const { specKey } = useParams<SpecParams>()
const httpMethod = method.httpMethod as RunItHttpMethod
const endpoint = method.endpoint
const { sdk, configurator, basePath } = useContext(RunItContext)
const { sdk, configurator } = useContext(RunItContext)
const [inputs] = useState(() => createInputs(api, method))
const [requestContent, setRequestContent] = useState(() =>
initRequestContent(configurator, inputs)
)
const [activePathParams, setActivePathParams] = useState({})
const [activeParams, setActiveParams] = useState({ path: {}, query: {} })
const [loading, setLoading] = useState(false)
const [responseContent, setResponseContent] =
useState<ResponseContent>(undefined)
Expand Down Expand Up @@ -172,15 +177,14 @@ export const RunIt: FC<RunItProps> = ({
return
}
}
setActivePathParams(pathParams)
setActiveParams({ path: pathParams, query: queryParams })
tabs.onSelectTab(1)
if (sdk) {
setLoading(true)
let response: ResponseContent
try {
response = await runRequest(
sdk,
basePath,
httpMethod,
endpoint,
pathParams,
Expand All @@ -207,6 +211,9 @@ export const RunIt: FC<RunItProps> = ({

// No SDK, no RunIt for you!
if (!sdk) return <></>
const baseUrl = isExtension
? 'need extension callback' // `${getExtensionSDK().lookerHostData?.hostUrl}/api/${specKey}`
: sdk.authSession.transport.options.base_url

return (
<Box bg="background" py="large" height="100%">
Expand Down Expand Up @@ -240,12 +247,22 @@ export const RunIt: FC<RunItProps> = ({
<TabPanel key="response">
<Loading
loading={loading}
message={`${httpMethod} ${pathify(endpoint, activePathParams)}`}
message={`${httpMethod} ${fullRequestUrl(
baseUrl,
endpoint,
activeParams.path,
activeParams.query
)}`}
/>
<ResponseExplorer
response={responseContent}
verb={httpMethod}
path={pathify(endpoint, activePathParams)}
path={fullRequestUrl(
baseUrl,
endpoint,
activeParams.path,
activeParams.query
)}
/>
</TabPanel>
<TabPanel key="makeTheCall">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ interface ResponseExplorerProps {
* Explore the raw response from an HTTP request
* @param response IRawResponse values
* @param verb HTTP method
* @param path Path of request
* @param path Full path of request, including query string
* @constructor
*/
export const ResponseExplorer: FC<ResponseExplorerProps> = ({
Expand All @@ -145,11 +145,8 @@ export const ResponseExplorer: FC<ResponseExplorerProps> = ({
{!response && <DarkSpan>No response was received</DarkSpan>}
{response && (
<>
<RunItHeading as="h4">
{`${verb || ''} ${path || ''} (${response.statusCode}: ${
response.statusMessage
})`}
</RunItHeading>
<RunItHeading as="h4">{`${verb || ''} ${path || ''}`}</RunItHeading>
<DarkSpan>{`${response.statusCode}: ${response.statusMessage}`}</DarkSpan>
<CollapserCard
divider={false}
heading={`Body (${getBodySize(response)})`}
Expand Down
2 changes: 1 addition & 1 deletion packages/run-it/src/utils/RunItSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { RunItConfigKey } from '../components'
// https://docs.looker.com/reference/api-and-integration/api-cors
const settings = {
...DefaultSettings(),
base_url: 'https://self-signed.looker.com:19999',
base_url: 'https://self-signed.looker.com:19999/api/4.0',
agentTag: 'RunIt 0.8',
} as IApiSettings

Expand Down
70 changes: 55 additions & 15 deletions packages/run-it/src/utils/requestUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
SOFTWARE.

*/

import type { RunItInput } from '../RunIt'
import { testJsonResponse, api } from '../test-data'
import { defaultConfigurator, StandaloneConfigurator } from '../components'
Expand All @@ -32,6 +33,7 @@ import {
runRequest,
createInputs,
initRequestContent,
fullRequestUrl,
} from './requestUtils'
import { initRunItSdk } from './RunItSDK'

Expand All @@ -43,12 +45,12 @@ describe('requestUtils', () => {
})

describe('pathify', () => {
test('it returns unchanged path if no path params are specified', () => {
it('returns unchanged path if no path params are specified', () => {
const actual = pathify('/logout')
expect(actual).toEqual('/logout')
})

test('it works path params', () => {
it('works path params', () => {
const pathParams = {
query_id: 1,
result_format: 'json',
Expand All @@ -61,6 +63,45 @@ describe('requestUtils', () => {
})
})

describe('fullRequestUrl', () => {
it('has full path', () => {
const path = '/queries/{query_id}/run/{result_format}'
const pathParams = {
query_id: 1,
result_format: 'json',
}
const actual = fullRequestUrl(
sdk.authSession.transport.options.base_url,
path,
pathParams
)
expect(actual).toEqual(
'https://self-signed.looker.com:19999/api/4.0/queries/1/run/json'
)
})

it('has escaped query params', () => {
const path = '/dashboards/{dashboard_id}/search'
const pathParams = {
dashboard_id: 1,
}
const queryParams = {
title: 'SDK%',
description: '%dashboard&',
limit: 4,
offset: 10,
}
const actual = fullRequestUrl(
sdk.authSession.transport.options.base_url,
path,
pathParams,
queryParams
)
expect(actual).toEqual(
'https://self-signed.looker.com:19999/api/4.0/dashboards/1/search?title=SDK%25&description=%25dashboard%26&limit=4&offset=10'
)
})
})
describe('createRequestParams', () => {
const inputs: RunItInput[] = [
{
Expand Down Expand Up @@ -107,7 +148,7 @@ describe('requestUtils', () => {
body: '{}',
}

test('empty json body is not removed', () => {
it('does not remove empty json body', () => {
const [pathParams, queryParams, body] = createRequestParams(
inputs,
noBody
Expand All @@ -121,7 +162,7 @@ describe('requestUtils', () => {
expect(body).toEqual({})
})

test('it correctly identifies requestContent params location', () => {
it('correctly identifies requestContent params location', () => {
const [pathParams, queryParams, body] = createRequestParams(
inputs,
requestContent
Expand All @@ -135,7 +176,7 @@ describe('requestUtils', () => {
expect(body).toEqual(JSON.parse(requestContent.body))
})

test('non JSON parsable strings are treated as x-www-form-urlencoded strings', () => {
it('treats non JSON parsable strings as x-www-form-urlencoded strings', () => {
const urlParams = 'key1=value1&key2=value2'
const [, , body] = createRequestParams(
[
Expand All @@ -156,15 +197,14 @@ describe('requestUtils', () => {
})

describe('defaultRunItCallback', () => {
test('it makes a request', async () => {
it('makes a request', async () => {
const spy = jest
.spyOn(sdk.authSession.transport, 'rawRequest')
.mockResolvedValueOnce(testJsonResponse)
jest.spyOn(sdk.authSession, 'isAuthenticated').mockReturnValue(true)

const resp = await runRequest(
sdk,
'/api/3.1',
'POST',
'/queries/run/{result_format}',
{ result_format: 'json' },
Expand All @@ -174,7 +214,7 @@ describe('requestUtils', () => {

expect(spy).toHaveBeenCalledWith(
'POST',
'/api/3.1/queries/run/json',
'https://self-signed.looker.com:19999/api/4.0/queries/run/json',
{
fields: 'first_name, last_name',
},
Expand All @@ -190,7 +230,7 @@ describe('requestUtils', () => {
})

describe('createInputs', () => {
test('converts delimarray to string', () => {
it('converts delimarray to string', () => {
const method = api.methods.all_users
const actual = createInputs(api, method)
expect(actual).toHaveLength(method.allParams.length)
Expand All @@ -203,7 +243,7 @@ describe('requestUtils', () => {
})
})

test('converts enums in body to string', () => {
it('converts enums in body to string', () => {
const method = api.methods.create_query_task
const actual = createInputs(api, method)
expect(actual).toHaveLength(method.allParams.length)
Expand All @@ -223,7 +263,7 @@ describe('requestUtils', () => {
})
})

test('works with various param types', () => {
it('works with various param types', () => {
const method = api.methods.run_inline_query
const actual = createInputs(api, method)
expect(actual).toHaveLength(method.allParams.length)
Expand Down Expand Up @@ -288,7 +328,7 @@ describe('requestUtils', () => {
})

describe('request content initialization', () => {
test('it initialzies body params with default values', () => {
it('it initialzies body params with default values', () => {
const inputs = createInputs(api, api.methods.run_inline_query)
const actual = initRequestContent(defaultConfigurator, inputs, {})
expect(actual).toEqual({
Expand Down Expand Up @@ -317,7 +357,7 @@ describe('requestUtils', () => {
})
})

test('it contains default-empty body params', () => {
it('it contains default-empty body params', () => {
const inputs = createInputs(api, api.methods.fetch_integration_form)
const bodyInput = inputs.find((i) => i.location === 'body')!
expect(bodyInput.name).toEqual('body')
Expand All @@ -332,7 +372,7 @@ describe('requestUtils', () => {
describe('createRequestParams', () => {
const inputs = createInputs(api, api.methods.run_inline_query)

test('removes empties for path, query and body params', () => {
it('removes empties for path, query and body params', () => {
const requestContent = initRequestContent(defaultConfigurator, inputs)
const [pathParams, queryParams, body] = createRequestParams(
inputs,
Expand All @@ -346,7 +386,7 @@ describe('requestUtils', () => {
})
})

test('does mot remove empty bodies', () => {
it('does mot remove empty bodies', () => {
const requestContent = { body: {} }
const [pathParams, queryParams, body] = createRequestParams(
inputs,
Expand Down
Loading