Skip to content

Commit

Permalink
Add WebApi package
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownskl committed Sep 23, 2024
1 parent 05aaa0f commit 7b93ff7
Show file tree
Hide file tree
Showing 17 changed files with 562 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"authentication": "yarn workspace @greenlight/authentication",
"storeapi": "yarn workspace @greenlight/storeapi",
"xcloudapi": "yarn workspace @greenlight/xcloudapi",
"webapi": "yarn workspace @greenlight/webapi",
"test": "yarn logger test && yarn authentication test && yarn storeapi test && yarn xcloudapi test"
},
"workspaces": [
Expand Down
4 changes: 2 additions & 2 deletions packages/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"license": "MIT",
"main": "dist/index",
"scripts": {
"start": "yarn build && DEBUG='*' node dist/bin/example.js",
"build": "yarn build:deps && tsc --build",
"start": "yarn build:deps && yarn build && DEBUG='*' node dist/bin/example.js",
"build": "tsc --build",
"build:deps": "yarn workspace @greenlight/logger build",
"clean": "rm -rf dist/ && rm -rf *.tsbuildinfo",
"test": "yarn build && mocha -r ../../node_modules/ts-node/register tests/**.ts tests/**/*.ts"
Expand Down
4 changes: 2 additions & 2 deletions packages/storeapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"license": "MIT",
"main": "dist/index",
"scripts": {
"start": "yarn build && DEBUG='*' node dist/bin/example.js",
"build": "yarn build:deps && tsc --build",
"start": "yarn build:deps && yarn build && DEBUG='*' node dist/bin/example.js",
"build": "tsc --build",
"build:deps": "yarn workspace @greenlight/authentication build && yarn workspace @greenlight/logger build && yarn workspace @greenlight/xcloudapi build",
"clean": "rm -rf dist/ && rm -rf *.tsbuildinfo",
"test": "yarn build && mocha -r ../../node_modules/ts-node/register tests/**.ts tests/**/*.ts"
Expand Down
23 changes: 23 additions & 0 deletions packages/webapi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# @greenlight/storeapi

This package is the storeapi. It will communicate with the gamepass catalog and fetch all products and keep the information in a in-memory sql lite database

## Class: StoreApi

### constructor(config:{ market: '<US/NL>' }):void

### loadProductIds(titles:string[]):boolean

Returns true if the supplied product id's have been loaded.

### findTitle():Promise<ItemResponse[]>

Returns a promise with ItemResponse object. This contains an id, title_name and title_data.

## Class: xCloudApi

### constructor(host:string, token:string):void

### getTitles():Promise<any>

Returns data with the titleID included. This is mostly used to test the functionality with a bigger chunk of data.
27 changes: 27 additions & 0 deletions packages/webapi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@greenlight/webapi",
"version": "1.0.0",
"license": "MIT",
"main": "dist/index",
"scripts": {
"start": "yarn build:deps && yarn build && DEBUG='*' node dist/bin/example.js",
"build": "tsc --build",
"build:deps": "yarn workspace @greenlight/authentication build && yarn workspace @greenlight/logger build",
"clean": "rm -rf dist/ && rm -rf *.tsbuildinfo",
"test": "yarn build && mocha -r ../../node_modules/ts-node/register tests/**.ts tests/**/*.ts"
},
"dependencies": {
"@greenlight/authentication": "workspace:^",
"@greenlight/logger": "workspace:^"
},
"devDependencies": {
"@types/chai": "^4",
"@types/debug": "^4.1.7",
"@types/mocha": "^10",
"@types/node": "^20.9.0",
"chai": "^4.3.6",
"mocha": "^10.1.0",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
}
}
52 changes: 52 additions & 0 deletions packages/webapi/src/bin/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

import Authentication from '@greenlight/authentication'
import Logger from '@greenlight/logger'
import WebApi from '../index'

class Cli {
private _auth = new Authentication()
public logger = new Logger('WebApi:Cli')

constructor(){
this.logger.log('Greenlight WebApi Cli')
this.logger.log('User:', this._auth.user.getGamertag())

this._auth.user.getWebToken().then((token) => {
console.log('Userhash:', this._auth.user.getUserhash())
console.log('Token:', token.data.Token)
console.log('Token NotAfter:', token.data.NotAfter)

// Only used for loading all productId's
const webapi = new WebApi({
uhs: this._auth.user.getUserhash(),
token: {
Token: token.data.Token,
NotAfter: token.data.NotAfter
}
})

console.log('Token valid:', webapi.isTokenValid())
// webapi.provider.smartglass.getConsolesList().then((response) => {
// console.log('Console ID:', response[0].id)


// webapi.provider.smartglass.getInstalledApps(response[0].id).then((response) => {
// console.log('Response:', response)

// response.forEach((app) => {
// console.log(app.contentType, ':', app.name)
// })
// })
// })

webapi.provider.people.getFriends().then((response) => {
console.log('Response:', response)
})

}).catch((error) => {
this.logger.error('Error:', error)
})
}
}

new Cli()
50 changes: 50 additions & 0 deletions packages/webapi/src/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Logger from '@greenlight/logger'

const logger = new Logger('WebApi:HTTP')

export default {

get(url:string, headers:any) {
logger.log('GET:', url, headers)

return fetch(url, {
method: 'GET',
headers: headers
}).then(async (response) => {
logger.log(response.status,'-', url)
try {
return {
response: response,
data: await response.json()
}
} catch (error) {
return {
response: response,
data: response.body
}
}
})
},

post(url:string, headers:any, body:any) {
logger.log('POST:', url, headers, JSON.stringify(body))
return fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(body)
}).then(async (response) => {
logger.log(response.status,'-', url)
try {
return {
response: response,
data: await response.json()
}
} catch (error) {
return {
response: response,
data: response.body
}
}
})
}
}
46 changes: 46 additions & 0 deletions packages/webapi/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Logger from '@greenlight/logger'

import SmartglassProvider from './providers/smartglass'
import SocialProvider from './providers/social'
import PeopleProvider from './providers/people'

export interface xCloudApiConfig {
uhs:string
token:XstsToken
language?:string
}

export interface XstsToken {
Token:string
NotAfter:string
}

export default class WebApi {

public logger:Logger = new Logger('WebApi')
private _uhs:string
private _token:XstsToken
private _language:string

public provider = {
smartglass: new SmartglassProvider(this),
social: new SocialProvider(this),
people: new PeopleProvider(this),
}

constructor(config:xCloudApiConfig) {
this.logger.log('constructor() Creating new WebApi instance')

this._uhs = config.uhs
this._token = config.token
this._language = config.language || 'en-US'
}

isTokenValid() {
return new Date(this._token.NotAfter) > new Date()
}

getAuthorizationHeader() {
return 'XBL3.0 x='+this._uhs+';'+this._token.Token
}
}
30 changes: 30 additions & 0 deletions packages/webapi/src/providers/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import WebApi from '..'
import Logger from '@greenlight/logger'
import Http from '../http'

export interface HttpHeaders {
[name:string]: string
}

export default class BaseProvider {

private _api:WebApi
public logger:Logger

public _endpoint = 'https://undefined.xboxlive.com'
public _headers:HttpHeaders = {}

constructor(api:WebApi){
this._api = api
this.logger = this._api.logger.extend(this.constructor.name)
}

async get(path:string, headers:HttpHeaders = {}) {
const reqHeaders = Object.assign({}, {
'Authorization': this._api.getAuthorizationHeader()
}, headers)

return await Http.get(this._endpoint+path, reqHeaders)
}

}
Loading

0 comments on commit 7b93ff7

Please sign in to comment.