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

refs #125 Add base Misskey API client #193

Merged
merged 1 commit into from
Feb 11, 2020
Merged
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
7 changes: 7 additions & 0 deletions src/mastodon/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {},
Expand Down
105 changes: 105 additions & 0 deletions src/misskey/api_client.ts
Original file line number Diff line number Diff line change
@@ -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