Skip to content

Commit

Permalink
Merge pull request #285 from systemli/Remove-deprecated-AuthService
Browse files Browse the repository at this point in the history
♻️ Rewrite API Calls & Remove AuthService
  • Loading branch information
0x46616c6b authored Jul 9, 2022
2 parents 76f0186 + c61fc15 commit 315ba75
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 250 deletions.
73 changes: 42 additions & 31 deletions src/api/Message.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { ApiUrl, Response } from './Api'
import AuthSingleton from '../components/AuthService'

const Auth = AuthSingleton.getInstance()

interface MessagesResponseData {
messages: Array<Message>
Expand All @@ -23,34 +20,48 @@ export interface Message {
attachments: any[]
}

export function getMessages(
ticker: number
): Promise<Response<MessagesResponseData>> {
return Auth.fetch(`${ApiUrl}/admin/tickers/${ticker}/messages`)
}
export function useMessageApi(token: string) {
const headers = {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
}

// TODO: any
export function postMessage(
ticker: string,
text: string,
geoInformation: any,
attachments: any[]
): Promise<Response<MessageResponseData>> {
return Auth.fetch(`${ApiUrl}/admin/tickers/${ticker}/messages`, {
body: JSON.stringify({
text: text,
geo_information: geoInformation,
attachments: attachments,
}),
method: 'POST',
})
}
const getMessages = (
ticker: number
): Promise<Response<MessagesResponseData>> => {
return fetch(`${ApiUrl}/admin/tickers/${ticker}/messages`, {
headers: headers,
}).then(response => response.json())
}

// TODO: any
const postMessage = (
ticker: string,
text: string,
geoInformation: any,
attachments: any[]
): Promise<Response<MessageResponseData>> => {
return fetch(`${ApiUrl}/admin/tickers/${ticker}/messages`, {
headers: headers,
body: JSON.stringify({
text: text,
geo_information: geoInformation,
attachments: attachments,
}),
method: 'post',
}).then(response => response.json())
}

const deleteMessage = (
ticker: string,
message: string
): Promise<Response<any>> => {
return fetch(`${ApiUrl}/admin/tickers/${ticker}/messages/${message}`, {
headers: headers,
method: 'delete',
}).then(response => response.json())
}

export function deleteMessage(
ticker: string,
message: string
): Promise<Response<any>> {
return Auth.fetch(`${ApiUrl}/admin/tickers/${ticker}/messages/${message}`, {
method: 'DELETE',
})
return { deleteMessage, getMessages, postMessage }
}
72 changes: 46 additions & 26 deletions src/api/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { ApiUrl, Response } from './Api'
import AuthSingleton from '../components/AuthService'

const Auth = AuthSingleton.getInstance()

interface InactiveSettingsResponseData {
setting: Setting<InactiveSetting>
Expand Down Expand Up @@ -33,30 +30,53 @@ export interface RefreshInterval {
value: string
}

export function getInactiveSettings(): Promise<
Response<InactiveSettingsResponseData>
> {
return Auth.fetch(`${ApiUrl}/admin/settings/inactive_settings`)
}
export function useSettingsApi(token: string) {
const headers = {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
}

export function getRefreshInterval(): Promise<
Response<RefreshIntervalResponseData>
> {
return Auth.fetch(`${ApiUrl}/admin/settings/refresh_interval`)
}
const getInactiveSettings = (): Promise<
Response<InactiveSettingsResponseData>
> => {
return fetch(`${ApiUrl}/admin/settings/inactive_settings`, {
headers: headers,
}).then(response => response.json())
}

export function putInactiveSettings(data: InactiveSetting) {
return Auth.fetch(`${ApiUrl}/admin/settings/inactive_settings`, {
body: JSON.stringify(data),
method: 'PUT',
})
}
const getRefreshInterval = (): Promise<
Response<RefreshIntervalResponseData>
> => {
return fetch(`${ApiUrl}/admin/settings/refresh_interval`, {
headers: headers,
}).then(response => response.json())
}

const putRefreshInterval = (
refreshInterval: number
): Promise<Response<RefreshIntervalResponseData>> => {
return fetch(`${ApiUrl}/admin/settings/refresh_interval`, {
headers: headers,
body: JSON.stringify({ refresh_interval: refreshInterval }),
method: 'put',
}).then(response => response.json())
}

const putInactiveSettings = (
data: InactiveSetting
): Promise<Response<InactiveSettingsResponseData>> => {
return fetch(`${ApiUrl}/admin/settings/inactive_settings`, {
headers: headers,
body: JSON.stringify(data),
method: 'put',
}).then(response => response.json())
}

export function putRefreshInterval(
refreshInterval: number
): Promise<Response<RefreshIntervalResponseData>> {
return Auth.fetch(`${ApiUrl}/admin/settings/refresh_interval`, {
body: JSON.stringify({ refresh_interval: refreshInterval }),
method: 'PUT',
})
return {
getInactiveSettings,
getRefreshInterval,
putInactiveSettings,
putRefreshInterval,
}
}
135 changes: 56 additions & 79 deletions src/api/Ticker.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { ApiUrl, Response } from './Api'
import AuthSingleton from '../components/AuthService'
import { User } from './User'

const Auth = AuthSingleton.getInstance()

interface TickersResponse {
data: {
tickers: Array<Ticker>
}
interface TickersResponseData {
tickers: Array<Ticker>
}

interface TickerResponse {
data: {
ticker: Ticker
}
interface TickerResponseData {
ticker: Ticker
}

interface TickerUsersResponseData {
Expand Down Expand Up @@ -50,78 +43,14 @@ export interface TickerLocation {
lon: number
}

export function getTickers(): Promise<TickersResponse> {
return Auth.fetch(`${ApiUrl}/admin/tickers`)
}

export function getTicker(id: number): Promise<TickerResponse> {
return Auth.fetch(`${ApiUrl}/admin/tickers/${id}`)
}

export function postTicker(data: any): Promise<TickerResponse> {
return Auth.fetch(`${ApiUrl}/admin/tickers`, {
body: JSON.stringify(data),
method: 'POST',
})
}

export function putTicker(data: any, id: number): Promise<TickerResponse> {
return Auth.fetch(`${ApiUrl}/admin/tickers/${id}`, {
body: JSON.stringify(data),
method: 'PUT',
})
}

/**
*
* @param data
* @param id
* @returns {Promise<any>}
*/
/*
export function putTickerTwitter(data: any, id: number) {
return Auth.fetch(`${ApiUrl}/admin/tickers/${id}/twitter`, {
body: JSON.stringify(data),
method: 'PUT',
})
}

export function deleteTicker(id: number) {
return Auth.fetch(`${ApiUrl}/admin/tickers/${id}`, {
method: 'DELETE',
})
}

/**
*
* @param {string|number} id
* @returns {Promise<Response>}
*/
export function getTickerUsers(id: number) {
return Auth.fetch(`${ApiUrl}/admin/tickers/${id}/users`)
}

/**
* @param id
* @param users
* @returns {Promise<Response>}
*/
export function putTickerUser(id: number, ...users: any) {
return Auth.fetch(`${ApiUrl}/admin/tickers/${id}/users`, {
body: JSON.stringify({ users: users }),
method: 'PUT',
})
}

/**
* @param id
* @param userId
* @returns {Promise<Response>}
*/
export function deleteTickerUser(id: number, userId: number) {
return Auth.fetch(`${ApiUrl}/admin/tickers/${id}/users/${userId}`, {
method: 'DELETE',
})
}
*/

export function useTickerApi(token: string) {
const headers = {
Expand All @@ -130,6 +59,13 @@ export function useTickerApi(token: string) {
'Content-Type': 'application/json',
}

const deleteTicker = (ticker: Ticker): Promise<Response<any>> => {
return fetch(`${ApiUrl}/admin/tickers/${ticker.id}`, {
headers: headers,
method: 'delete',
}).then(response => response.json())
}

const getTickerUsers = (
ticker: Ticker
): Promise<Response<TickerUsersResponseData>> => {
Expand All @@ -148,6 +84,37 @@ export function useTickerApi(token: string) {
}).then(response => response.json())
}

const getTickers = (): Promise<Response<TickersResponseData>> => {
return fetch(`${ApiUrl}/admin/tickers`, { headers: headers }).then(
response => response.json()
)
}

const getTicker = (id: number): Promise<Response<TickerResponseData>> => {
return fetch(`${ApiUrl}/admin/tickers/${id}`, { headers: headers }).then(
response => response.json()
)
}

const postTicker = (data: any): Promise<Response<TickerResponseData>> => {
return fetch(`${ApiUrl}/admin/tickers`, {
headers: headers,
body: JSON.stringify(data),
method: 'post',
}).then(response => response.json())
}

const putTicker = (
data: any,
id: number
): Promise<Response<TickerResponseData>> => {
return fetch(`${ApiUrl}/admin/tickers/${id}`, {
headers: headers,
body: JSON.stringify(data),
method: 'put',
}).then(response => response.json())
}

const putTickerUsers = (
ticker: Ticker,
users: number[]
Expand All @@ -161,12 +128,22 @@ export function useTickerApi(token: string) {

const putTickerReset = (
ticker: Ticker
): Promise<Response<TickerResponse>> => {
): Promise<Response<TickerResponseData>> => {
return fetch(`${ApiUrl}/admin/tickers/${ticker.id}/reset`, {
headers: headers,
method: 'put',
}).then(response => response.json())
}

return { deleteTickerUser, getTickerUsers, putTickerUsers, putTickerReset }
return {
deleteTicker,
deleteTickerUser,
getTickers,
getTicker,
getTickerUsers,
postTicker,
putTicker,
putTickerUsers,
putTickerReset,
}
}
16 changes: 0 additions & 16 deletions src/api/Upload.js

This file was deleted.

Loading

0 comments on commit 315ba75

Please sign in to comment.