Skip to content

Commit

Permalink
Add handleMethod function to improve legibility and reusability of th…
Browse files Browse the repository at this point in the history
…e code
  • Loading branch information
matextrem committed Jul 1, 2022
1 parent 6363911 commit add77eb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 64 deletions.
12 changes: 6 additions & 6 deletions src/api/cow/cow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ test('Valid: Get Profile Data', async () => {
await cowSdk1.cowApi.getProfileData('0x6810e776880c02933d47db1b9fc05908e5386b96')
expect(fetchMock).toHaveBeenCalledTimes(1)
expect(fetchMock).toHaveBeenCalledWith(
'https://barn.api.cow.fi/affiliate/api/v1/profile/0x6810e776880c02933d47db1b9fc05908e5386b96',
'https://api.cow.fi/affiliate/api/v1/profile/0x6810e776880c02933d47db1b9fc05908e5386b96',
FETCH_RESPONSE_PARAMETERS
)
})
Expand All @@ -459,7 +459,7 @@ test('Invalid: Get Profile Data from unexisting address', async () => {
expect(error.message).toEqual("You've passed an invalid URL")
expect(fetchMock).toHaveBeenCalledTimes(1)
expect(fetchMock).toHaveBeenCalledWith(
'https://barn.api.cow.fi/affiliate/api/v1/profile/unexistingAddress',
'https://api.cow.fi/affiliate/api/v1/profile/unexistingAddress',
FETCH_RESPONSE_PARAMETERS
)
}
Expand Down Expand Up @@ -494,7 +494,7 @@ test('Valid: Send sign order cancellation', async () => {
await cowSdk.cowApi.sendSignedOrderCancellation(ORDER_CANCELLATION)
expect(fetchMock).toHaveBeenCalledTimes(1)
expect(fetchMock).toHaveBeenCalledWith(
`https://barn.api.cow.fi/rinkeby/api/v1/orders/${ORDER_CANCELLATION.cancellation.orderUid}`,
`https://api.cow.fi/rinkeby/api/v1/orders/${ORDER_CANCELLATION.cancellation.orderUid}`,
{
...FETCH_RESPONSE_PARAMETERS,
body: JSON.stringify({ ...SIGNED_ORDER_RESPONSE, signingScheme: 'eip712', from: ORDER_CANCELLATION.owner }),
Expand All @@ -520,7 +520,7 @@ test('Invalid: Send sign not found order cancellation', async () => {
const error = e as CowError
expect(error.message).toEqual('Token pair selected has insufficient liquidity')
expect(fetchMock).toHaveBeenCalledTimes(1)
expect(fetchMock).toHaveBeenCalledWith('https://barn.api.cow.fi/rinkeby/api/v1/orders/unexistingOrder', {
expect(fetchMock).toHaveBeenCalledWith('https://api.cow.fi/rinkeby/api/v1/orders/unexistingOrder', {
...FETCH_RESPONSE_PARAMETERS,
body: JSON.stringify({ ...SIGNED_ORDER_RESPONSE, signingScheme: 'eip712', from: ORDER_CANCELLATION.owner }),
method: 'DELETE',
Expand Down Expand Up @@ -593,7 +593,7 @@ test('Valid: AppDataHash properly set on X-AppId header', async () => {
cowSdk1.updateChainId(1)
await cowSdk1.cowApi.getProfileData('0x6810e776880c02933d47db1b9fc05908e5386b96')
expect(fetchMock).toHaveBeenCalledWith(
'https://barn.api.cow.fi/affiliate/api/v1/profile/0x6810e776880c02933d47db1b9fc05908e5386b96',
'https://api.cow.fi/affiliate/api/v1/profile/0x6810e776880c02933d47db1b9fc05908e5386b96',
{
...FETCH_RESPONSE_PARAMETERS,
headers: { ...FETCH_RESPONSE_PARAMETERS.headers, 'X-AppId': cowSdk1.context.appDataHash },
Expand All @@ -607,7 +607,7 @@ test('Valid: AppDataHash properly set on X-AppId header when undefined', async (
cowSdk1.updateChainId(1)
await cowSdk1.cowApi.getProfileData('0x6810e776880c02933d47db1b9fc05908e5386b96')
expect(fetchMock).toHaveBeenCalledWith(
'https://barn.api.cow.fi/affiliate/api/v1/profile/0x6810e776880c02933d47db1b9fc05908e5386b96',
'https://api.cow.fi/affiliate/api/v1/profile/0x6810e776880c02933d47db1b9fc05908e5386b96',
{
...FETCH_RESPONSE_PARAMETERS,
headers: {
Expand Down
80 changes: 22 additions & 58 deletions src/api/cow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,81 +373,45 @@ export class CowApi {
})
}

private async post(url: string, data: unknown, options: Options = {}): Promise<Response> {
const { chainId: networkId, isDevEnvironment } = options
const prodUri = getGnosisProtocolUrl(false)
const barnUri = getGnosisProtocolUrl(true)
const chainId = networkId || (await this.context.chainId)
let response
if (isDevEnvironment === undefined) {
try {
response = await this.fetch(url, 'POST', `${prodUri[chainId]}/v1`, data)
} catch (error) {
response = await this.fetch(url, 'POST', `${barnUri[chainId]}/v1`, data)
}
return response
} else {
const uri = isDevEnvironment ? barnUri : prodUri
return await this.fetch(url, 'POST', `${uri[chainId]}/v1`, data)
}
private post(url: string, data: unknown, options: Options = {}): Promise<Response> {
return this.handleMethod(url, 'POST', this.fetch.bind(this), getGnosisProtocolUrl, options, data)
}

private async get(url: string, options: Options = {}): Promise<Response> {
const { chainId: networkId, isDevEnvironment } = options
const prodUri = getGnosisProtocolUrl(false)
const barnUri = getGnosisProtocolUrl(true)
const chainId = networkId || (await this.context.chainId)

let response
if (isDevEnvironment === undefined) {
try {
response = await this.fetch(url, 'GET', `${prodUri[chainId]}/v1`)
} catch (error) {
response = await this.fetch(url, 'GET', `${barnUri[chainId]}/v1`)
}
} else {
const uri = isDevEnvironment ? barnUri : prodUri
response = await this.fetch(url, 'GET', `${uri[chainId]}/v1`)
}
return response
private get(url: string, options: Options = {}): Promise<Response> {
return this.handleMethod(url, 'GET', this.fetch.bind(this), getGnosisProtocolUrl, options)
}

private async getProfile(url: string, options: Options = {}): Promise<Response> {
const { chainId: networkId, isDevEnvironment } = options
const prodUri = getProfileUrl(false)
const barnUri = getProfileUrl(true)
const chainId = networkId || (await this.context.chainId)
private getProfile(url: string, options: Options = {}): Promise<Response> {
return this.handleMethod(url, 'GET', this.fetchProfile.bind(this), getProfileUrl, options)
}

let response
if (isDevEnvironment === undefined) {
try {
response = await this.fetchProfile(url, 'GET', `${barnUri[chainId]}/v1`)
} catch (error) {
response = await this.fetchProfile(url, 'GET', `${prodUri[chainId]}/v1`)
}
} else {
const uri = isDevEnvironment ? barnUri : prodUri
response = await this.fetchProfile(url, 'GET', `${uri[chainId]}/v1`)
}
return response
private delete(url: string, data: unknown, options: Options = {}): Promise<Response> {
return this.handleMethod(url, 'DELETE', this.fetch.bind(this), getGnosisProtocolUrl, options, data)
}

private async delete(url: string, data: unknown, options: Options = {}): Promise<Response> {
private async handleMethod(
url: string,
method: 'GET' | 'POST' | 'DELETE',
fetchFn: typeof this.fetch | typeof this.fetchProfile,
getUrl: typeof getGnosisProtocolUrl | typeof getProfileUrl,
options: Options = {},
data?: unknown
): Promise<Response> {
const { chainId: networkId, isDevEnvironment } = options
const prodUri = getGnosisProtocolUrl(false)
const barnUri = getGnosisProtocolUrl(true)
const prodUri = getUrl(false)
const barnUri = getUrl(true)
const chainId = networkId || (await this.context.chainId)

let response
if (isDevEnvironment === undefined) {
try {
response = await this.fetch(url, 'DELETE', `${barnUri[chainId]}/v1`, data)
response = await fetchFn(url, method, `${prodUri[chainId]}/v1`, data)
} catch (error) {
response = await this.fetch(url, 'DELETE', `${prodUri[chainId]}/v1`, data)
response = await fetchFn(url, method, `${barnUri[chainId]}/v1`, data)
}
} else {
const uri = isDevEnvironment ? barnUri : prodUri
response = await this.fetch(url, 'DELETE', `${uri[chainId]}/v1`, data)
response = await fetchFn(url, method, `${uri[chainId]}/v1`, data)
}
return response
}
Expand Down

0 comments on commit add77eb

Please sign in to comment.