refs #125 Add base Misskey API client

This commit is contained in:
AkiraFukushima 2020-02-11 23:47:12 +09:00
parent 461703c608
commit a38b76ecad
2 changed files with 112 additions and 0 deletions

View file

@ -84,6 +84,13 @@ namespace MastodonAPI {
})
}
/**
* Unauthorized POST request to mastodon REST API.
* @param path relative path from baseUrl
* @param params Body parameters
* @param baseUrl base URL of the target
* @param proxyConfig Proxy setting, or set false if don't use proxy.
*/
public static async post<T>(
path: string,
params = {},

105
src/misskey/api_client.ts Normal file
View file

@ -0,0 +1,105 @@
import axios, { AxiosResponse, CancelTokenSource, AxiosRequestConfig } from 'axios'
import { DEFAULT_UA } from '@/default'
import proxyAgent, { ProxyConfig } from '@/proxy_config'
import Response from '@/response'
namespace MisskeyAPI {
/**
* Interface
*/
export interface Interface {}
/**
* Misskey API client.
*
* Usign axios for request, you will handle promises.
*/
export class Client implements Interface {
static DEFAULT_URL = 'https://misskey.io'
private accessToken: string
private baseUrl: string
private userAgent: string
private cancelTokenSource: CancelTokenSource
private proxyConfig: ProxyConfig | false = false
/**
* @param baseUrl hostname or base URL
* @param accessToken access token from OAuth2 authorization
* @param userAgent UserAgent is specified in header on request.
* @param proxyConfig Proxy setting, or set false if don't use proxy.
*/
constructor(baseUrl: string, accessToken: string, userAgent: string = DEFAULT_UA, proxyConfig: ProxyConfig | false = false) {
this.accessToken = accessToken
this.baseUrl = baseUrl
this.userAgent = userAgent
this.cancelTokenSource = axios.CancelToken.source()
this.proxyConfig = proxyConfig
}
/**
* Unauthorized POST request to mastodon REST API.
* @param path relative path from baseUrl
* @param params Body parameters
* @param baseUrl base URL of the target
* @param proxyConfig Proxy setting, or set false if don't use proxy.
*/
public static async post<T>(
path: string,
params = {},
baseUrl = this.DEFAULT_URL,
proxyConfig: ProxyConfig | false = false
): Promise<Response<T>> {
let options: AxiosRequestConfig = {}
if (proxyConfig) {
options = Object.assign(options, {
httpsAgent: proxyAgent(proxyConfig)
})
}
const apiUrl = baseUrl
return axios.post<T>(apiUrl + path, params, options).then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
/**
* POST request to mastodon REST API.
* @param path relative path from baseUrl
* @param params Form data
*/
public async post<T>(path: string, params = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
cancelToken: this.cancelTokenSource.token,
headers: {
'User-Agent': this.userAgent
}
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpsAgent: proxyAgent(this.proxyConfig)
})
}
const bodyParams = Object.assign(params, {
i: this.accessToken
})
return axios.post<T>(this.baseUrl + path, bodyParams, options).then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
}
}
export default MisskeyAPI