Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
210 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createAction } from 'typesafe-actions'; | ||
import { GlobalSettings, getGlobalSettings } from 'app/dim-api/global-settings'; | ||
import { ThunkResult } from 'app/store/reducers'; | ||
|
||
/** Bulk update global settings after they've been loaded. */ | ||
export const settingsLoaded = createAction('dim-api/GLOBAL_SETTINGS_LOADED')< | ||
Partial<GlobalSettings> | ||
>(); | ||
|
||
export function loadGlobalSettings(): ThunkResult<Promise<void>> { | ||
return async (dispatch, getState) => { | ||
// TODO: better to use a state machine (UNLOADED => LOADING => LOADED) | ||
if (!getState().dimApi.settingsLoaded) { | ||
const globalSettings = await getGlobalSettings(); | ||
console.log('globalSettings', globalSettings); | ||
dispatch(settingsLoaded(globalSettings)); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { HttpClientConfig } from 'bungie-api-ts/http'; | ||
import { stringify } from 'simple-query-string'; | ||
|
||
/** | ||
* Call one of the unauthenticated DIM APIs. | ||
*/ | ||
export async function unauthenticatedApi<T>(config: HttpClientConfig): Promise<T> { | ||
let url = `https://api.destinyitemmanager.com${config.url}`; | ||
if (config.params) { | ||
url = `${url}?${stringify(config.params)}`; | ||
} | ||
const response = await fetch( | ||
new Request(url, { | ||
method: config.method, | ||
body: JSON.stringify(config.body), | ||
headers: { | ||
// TODO: send an API Key | ||
// 'X-API-Key': DIM_API_KEY, | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
); | ||
|
||
return response.json() as Promise<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { unauthenticatedApi } from './dim-api-helper'; | ||
|
||
/** | ||
* Global DIM platform settings from the DIM API. | ||
*/ | ||
export interface GlobalSettings { | ||
/** Whether to use the DIM API for */ | ||
dimApiEnabled: boolean; | ||
/** Don't allow refresh more often than this many seconds. */ | ||
destinyProfileMinimumRefreshInterval: number; | ||
/** Time in seconds to refresh the profile when autoRefresh is true. */ | ||
destinyProfileRefreshInterval: number; | ||
/** Whether to refresh profile automatically. */ | ||
autoRefresh: boolean; | ||
/** Whether to refresh profile when the page becomes visible after being in the background. */ | ||
refreshProfileOnVisible: boolean; | ||
/** Whether to use dirty tricks to bust the Bungie.net cache when users manually refresh. */ | ||
bustProfileCacheOnHardRefresh: boolean; | ||
} | ||
|
||
export async function getGlobalSettings() { | ||
try { | ||
const response = await unauthenticatedApi<{ settings: GlobalSettings }>({ | ||
url: '/platform_info', | ||
method: 'GET' | ||
}); | ||
return response.settings; | ||
} catch (e) { | ||
console.log(e); | ||
throw e; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Reducer } from 'redux'; | ||
import * as actions from './actions'; | ||
import { ActionType, getType } from 'typesafe-actions'; | ||
import _ from 'lodash'; | ||
import { GlobalSettings } from 'app/dim-api/global-settings'; | ||
|
||
export interface DimApiState { | ||
settings: GlobalSettings; | ||
settingsLoaded: boolean; | ||
} | ||
|
||
/** | ||
* Global DIM platform settings from the DIM API. | ||
*/ | ||
const initialState: DimApiState = { | ||
settingsLoaded: false, | ||
settings: { | ||
dimApiEnabled: true, | ||
destinyProfileMinimumRefreshInterval: 30, | ||
destinyProfileRefreshInterval: 30, | ||
// 2019-12-17 we've been asked to disable auto-refresh | ||
autoRefresh: false, | ||
refreshProfileOnVisible: true, | ||
bustProfileCacheOnHardRefresh: false | ||
} | ||
}; | ||
|
||
type DimApiAction = ActionType<typeof actions>; | ||
|
||
export const dimApi: Reducer<DimApiState, DimApiAction> = ( | ||
state: DimApiState = initialState, | ||
action: DimApiAction | ||
) => { | ||
switch (action.type) { | ||
case getType(actions.settingsLoaded): | ||
return { | ||
...state, | ||
settingsLoaded: true, | ||
settings: { | ||
...state.settings, | ||
...action.payload | ||
} | ||
}; | ||
|
||
default: | ||
return state; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters