Use new api gateway

This commit is contained in:
Zhiyuan Zheng 2021-06-21 11:59:29 +02:00
parent f3abcf640c
commit 927e7df4e9
11 changed files with 137 additions and 51 deletions

View File

@ -5,7 +5,7 @@ export SENTRY_PROJECT=""
export SENTRY_AUTH_TOKEN=""
export SENTRY_DSN=""
export TRANSLATE_KEY=""
export TOOOT_API_KEY=""
# Fastlane start
export LC_ALL=""

View File

@ -40,7 +40,7 @@ jobs:
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
TRANSLATE_KEY: ${{ secrets.TRANSLATE_KEY }}
TOOOT_API_KEY: ${{ secrets.TOOOT_API_KEY }}
FASTLANE_USER: ${{ secrets.FASTLANE_USER }}
MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
MATCH_GIT_URL: ${{ secrets.MATCH_GIT_URL }}

View File

@ -14,7 +14,7 @@ export default (): ExpoConfig => ({
assetBundlePatterns: ['assets/*'],
extra: {
sentryDSN: process.env.SENTRY_DSN,
translateKey: process.env.TRANSLATE_KEY
toootApiKey: process.env.TOOOT_API_KEY
},
hooks: {
postPublish: [

View File

@ -3,8 +3,8 @@
"versions": {
"native": "210511",
"major": 2,
"minor": 0,
"patch": 5,
"minor": 1,
"patch": 0,
"expo": "41.0.0"
},
"description": "tooot app for Mastodon",

104
src/api/tooot.ts Normal file
View File

@ -0,0 +1,104 @@
import axios from 'axios'
import chalk from 'chalk'
import { Constants } from 'react-native-unimodules'
import * as Sentry from 'sentry-expo'
const ctx = new chalk.Instance({ level: 3 })
export type Params = {
service: 'push' | 'translate'
method: 'get' | 'post'
url: string
params?: {
[key: string]: string | number | boolean | string[] | number[] | boolean[]
}
headers?: { [key: string]: string }
body?: FormData | Object
sentry?: boolean
}
const DOMAIN = __DEV__ ? 'testapi.tooot.app' : 'api.tooot.app'
const apiTooot = async <T = unknown>({
service,
method,
url,
params,
headers,
body,
sentry = false
}: Params): Promise<{ body: T }> => {
const key = Constants.manifest.extra?.toootApiKey
console.log(
ctx.bgGreen.bold(' API tooot ') +
' ' +
method +
ctx.green(' -> ') +
`/${url}` +
(params ? ctx.green(' -> ') : ''),
params ? params : ''
)
return axios({
timeout: method === 'post' ? 1000 * 60 : 1000 * 15,
method,
baseURL: `https://${DOMAIN}/`,
url: `${service}/${url}`,
params,
headers: {
...(key && { 'x-tooot-key': key }),
'Content-Type': 'application/json',
'User-Agent': `tooot/${Constants.manifest.version}`,
Accept: '*/*',
...headers
},
...(body && { data: body })
})
.then(response => {
return Promise.resolve({
body: response.data
})
})
.catch(error => {
if (sentry) {
Sentry.Native.setExtras(error.response)
Sentry.Native.captureException(error)
}
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(
ctx.bold(' API tooot '),
ctx.bold('response'),
error.response.status,
error.response.data.error
)
return Promise.reject({
status: error.response.status,
message: error.response.data.error
})
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(
ctx.bold(' API tooot '),
ctx.bold('request'),
error.request
)
return Promise.reject()
} else {
console.error(
ctx.bold(' API tooot '),
ctx.bold('internal'),
error.message,
url
)
return Promise.reject()
}
})
}
export default apiTooot

View File

@ -1,12 +1,9 @@
import apiGeneral from '@api/general'
import apiTooot from '@api/tooot'
import { displayMessage } from '@components/Message'
import { NavigationContainerRef } from '@react-navigation/native'
import { Dispatch } from '@reduxjs/toolkit'
import {
disableAllPushes,
Instance,
PUSH_SERVER
} from '@utils/slices/instancesSlice'
import { disableAllPushes, Instance } from '@utils/slices/instancesSlice'
import * as Notifications from 'expo-notifications'
import { useEffect } from 'react'
import { TFunction } from 'react-i18next'
@ -34,10 +31,10 @@ const pushUseConnect = ({
})
).data
apiGeneral({
apiTooot({
method: 'post',
domain: PUSH_SERVER,
url: 'v1/connect',
service: 'push',
url: 'connect',
body: {
expoToken
},

View File

@ -1,4 +1,4 @@
import apiGeneral from '@api/general'
import apiTooot from '@api/tooot'
import haptics from '@components/haptics'
import { AxiosError } from 'axios'
import { Buffer } from 'buffer'
@ -21,16 +21,7 @@ export type QueryKeyTranslate = [
}
]
export const TRANSLATE_SERVER = __DEV__
? 'testtranslate.tooot.app'
: 'translate.tooot.app'
const queryFunction = async ({ queryKey }: { queryKey: QueryKeyTranslate }) => {
const key = Constants.manifest.extra?.translateKey
if (!key) {
return Promise.reject()
}
const { uri, source, target, text } = queryKey[1]
const uriEncoded = Buffer.from(uri.replace(/https?:\/\//, ''))
@ -42,11 +33,11 @@ const queryFunction = async ({ queryKey }: { queryKey: QueryKeyTranslate }) => {
'base64'
)
const res = await apiGeneral<Translations>({
domain: TRANSLATE_SERVER,
const res = await apiTooot<Translations>({
method: 'get',
url: `v1/translate/${uriEncoded}/${target}`,
headers: { key, original }
service: 'translate',
url: `source/${uriEncoded}/target/${target}`,
headers: { original }
})
haptics('Light')
return res.body

View File

@ -1,12 +1,8 @@
import apiGeneral from '@api/general'
import apiInstance from '@api/instance'
import apiTooot from '@api/tooot'
import i18n from '@root/i18n/i18n'
import { RootState } from '@root/store'
import {
getInstance,
Instance,
PUSH_SERVER
} from '@utils/slices/instancesSlice'
import { getInstance, Instance } from '@utils/slices/instancesSlice'
import * as Notifications from 'expo-notifications'
import { Platform } from 'react-native'
import androidDefaults from './androidDefaults'
@ -22,13 +18,13 @@ const register1 = async ({
accountId: Mastodon.Account['id']
accountFull: string
}) => {
return apiGeneral<{
return apiTooot<{
endpoint: string
keys: { public: string; private: string; auth: string }
}>({
method: 'post',
domain: PUSH_SERVER,
url: 'v1/register1',
service: 'push',
url: 'register1',
body: { expoToken, instanceUrl, accountId, accountFull },
sentry: true
})
@ -47,10 +43,10 @@ const register2 = async ({
accountId: Mastodon.Account['id']
removeKeys: boolean
}) => {
return apiGeneral({
return apiTooot({
method: 'post',
domain: PUSH_SERVER,
url: 'v1/register2',
service: 'push',
url: 'register2',
body: { expoToken, instanceUrl, accountId, serverKey, removeKeys },
sentry: true
})

View File

@ -1,7 +1,7 @@
import apiGeneral from '@api/general'
import apiInstance from '@api/instance'
import apiTooot from '@api/tooot'
import { RootState } from '@root/store'
import { getInstance, PUSH_SERVER } from '@utils/slices/instancesSlice'
import { getInstance } from '@utils/slices/instancesSlice'
import * as Notifications from 'expo-notifications'
import { Platform } from 'react-native'
@ -19,10 +19,10 @@ const pushUnregister = async (state: RootState, expoToken: string) => {
url: 'push/subscription'
})
await apiGeneral<{ endpoint: string; publicKey: string; auth: string }>({
await apiTooot<{ endpoint: string; publicKey: string; auth: string }>({
method: 'post',
domain: PUSH_SERVER,
url: 'v1/unregister',
service: 'push',
url: 'unregister',
body: {
expoToken,
instanceUrl: instance.url,

View File

@ -1,10 +1,10 @@
import apiGeneral from '@api/general'
import apiTooot from '@api/tooot'
import { createAsyncThunk } from '@reduxjs/toolkit'
import i18n from '@root/i18n/i18n'
import { RootState } from '@root/store'
import * as Notifications from 'expo-notifications'
import { Platform } from 'react-native'
import { getInstance, Instance, PUSH_SERVER } from '../instancesSlice'
import { getInstance, Instance } from '../instancesSlice'
import androidDefaults from './push/androidDefaults'
export const updateInstancePushDecode = createAsyncThunk(
@ -25,10 +25,10 @@ export const updateInstancePushDecode = createAsyncThunk(
})
).data
await apiGeneral({
await apiTooot({
method: 'post',
domain: PUSH_SERVER,
url: 'v1/update-decode',
service: 'push',
url: 'update-decode',
body: {
expoToken,
instanceUrl: instance.url,

View File

@ -11,8 +11,6 @@ import { updateInstancePush } from './instances/updatePush'
import { updateInstancePushAlert } from './instances/updatePushAlert'
import { updateInstancePushDecode } from './instances/updatePushDecode'
export const PUSH_SERVER = __DEV__ ? 'testpush.tooot.app' : 'push.tooot.app'
export type Instance = {
active: boolean
appData: {