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: Token retry #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions .github/auto_assign.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
addReviewers: true

addAssignees: true

reviewers:
- dragon-fish

assignees:
- dragon-fish
13 changes: 13 additions & 0 deletions .github/workflows/auto_assign_pull_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: 'Auto Assign'
on:
pull_request:
types: [opened, ready_for_review]

jobs:
add-reviews:
runs-on: ubuntu-latest
steps:
- uses: kentaro-m/auto-assign-action@v1.2.5
with:
configuration-path: '.github/auto_assign.yml' # Only needed if you use something other than .github/auto_assign.yml
repo-token: ${{ secrets.GITHUB_TOKEN }}
6 changes: 4 additions & 2 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ name: Unit test for MediaWikiApi core
on:
push:
branches: [master, dev, actions]
pull_request:
branches: [master, dev, actions]
pull_request_review:
types: [submitted]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
test:
# The type of runner that the job will run on
runs-on: ubuntu-latest

if: github.event_name == 'push' || (github.event_name == 'pull_request_review' && github.event.review.state == 'APPROVED')

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- name: Checkout
Expand Down
46 changes: 32 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export class MediaWikiApi {
lgname: string,
lgpassword: string,
params?: MwApiParams,
postOptions?: { retry?: number; noCache?: boolean }
postOptions?: { retry?: number; noCache?: boolean, tokenRetry?: number }
): Promise<{
result: 'Success' | 'NeedToken' | 'WrongToken' | 'Failed'
token?: string
Expand Down Expand Up @@ -322,30 +322,48 @@ export class MediaWikiApi {
}

/** Token Handler */
async getTokens(type: MwTokenName[] = ['csrf']) {
async getTokens(type: MwTokenName[] = ['csrf'], retry: number = 3): Promise<Record<string, string>> {
if (retry < 1) {
throw new WikiSaikouError(
WikiSaikouErrorCode.TOKEN_RETRY_LIMIT_EXCEEDED,
'The limit of the number of times to automatically re-acquire the token has been exceeded'
)
}
this.defaultOptions.credentials = 'include'
const { data } = await this.get({
action: 'query',
meta: 'tokens',
type,
})
this.tokens = { ...this.tokens, ...data.query.tokens }
return this.tokens
try {
const { data } = await this.get({
action: 'query',
meta: 'tokens',
type,
})
this.tokens = { ...this.tokens, ...data.query.tokens }
return this.tokens
} catch (err) {
if (retry < 1) {
throw new WikiSaikouError(
WikiSaikouErrorCode.HTTP_ERROR,
"The server returns an error, but it doesn't seem to be caused by MediaWiki",
err
)
} else {
return this.getTokens(type, retry - 1);
}
}
}
async token(type: MwTokenName = 'csrf', noCache = false) {
async token(type: MwTokenName = 'csrf', noCache = false, retry: number = 3) {
if (!this.tokens[`${type}token`] || noCache) {
delete this.tokens[`${type}token`]
await this.getTokens([type])
await this.getTokens([type], retry)
}
return this.tokens[`${type}token`]
}

async postWithToken<T = any>(
tokenType: MwTokenName,
body: MwApiParams,
options?: { tokenName?: string; retry?: number; noCache?: boolean }
options?: { tokenName?: string; retry?: number; noCache?: boolean; tokenRetry?: number }
): Promise<FexiosFinalContext<T>> {
const { tokenName = 'token', retry = 3, noCache = false } = options || {}
const { tokenName = 'token', retry = 3, noCache = false, tokenRetry = 3 } = options || {}

if (retry < 1) {
throw new WikiSaikouError(
Expand All @@ -354,7 +372,7 @@ export class MediaWikiApi {
)
}

const token = await this.token(tokenType, noCache)
const token = await this.token(tokenType, noCache, tokenRetry)

const doRetry = () =>
this.postWithToken(tokenType, body, {
Expand Down