Skip to content

Instantly share code, notes, and snippets.

@dashedstripes
Created March 23, 2020 19:57
Show Gist options
  • Save dashedstripes/7feacef88ba2c12005830369bf32ee23 to your computer and use it in GitHub Desktop.
Save dashedstripes/7feacef88ba2c12005830369bf32ee23 to your computer and use it in GitHub Desktop.
interface Auth {
clientId: string;
clientSecret: string;
token: string;
authorize: () => void;
}
interface OutputInterface {
play: (type: string, uri: string) => void;
pause: () => void;
}
interface APIClient {
search: (type: string, value: string) => any;
}
interface RequestClient {
get: (url: string) => any;
}
class SpotifyAppleScriptOutput implements OutputInterface {
public play(type: string, uri: string) {
console.log(`Applescript: spotify play ${type}:${uri}`);
}
public pause() {
console.log('Applescript: spotify playpause');
}
}
class AppleMusicAppleScriptOutput implements OutputInterface {
public play(type: string, uri: string) {
console.log(`Applescript: applemusic play ${type}:${uri}`);
}
public pause() {
console.log('Applescript: applemusic playpause');
}
}
class MockRequestClient implements RequestClient {
public get(url: string) {
return { data: 'fakeuri138419id' };
}
}
class SpotifyAPIClient implements APIClient {
public request: RequestClient;
public accessToken: string;
constructor(request: RequestClient, accessToken: string) {
this.request = request;
this.accessToken = accessToken;
}
public search(type: string, value: string) {
const response = this.request.get(`https://api.spotify.com/search?q=${value}&type=${type}`);
return response.data;
}
}
class AppleMusicAPIClient implements APIClient {
public request: RequestClient;
public accessToken: string;
constructor(request: RequestClient, accessToken: string) {
this.request = request;
this.accessToken = accessToken;
}
public search(type: string, value: string) {
const response = this.request.get(`https://api.applemusic.com/search?q=${value}&type=${type}`);
return response.data;
}
}
class SpotifyAuth implements Auth {
clientId: string;
clientSecret: string;
token: string;
constructor(clientId: string, clientSecret: string) {
this.clientId = clientId;
this.clientSecret = clientSecret;
}
public authorize() {
// Make some calls using the clientId and clientSecret to get token
this.token = 'mytoken';
return this.token;
}
}
class AppleMusicAuth implements Auth {
clientId: string;
clientSecret: string;
token: string;
constructor(clientId: string, clientSecret: string) {
this.clientId = clientId;
this.clientSecret = clientSecret;
}
public authorize() {
// Make some calls using the clientId and clientSecret to get token
this.token = 'mytoken';
return this.token;
}
}
class CommandManager {
public outputInterface: OutputInterface;
public client: APIClient;
constructor(outputInterface: OutputInterface, client: APIClient) {
this.outputInterface = outputInterface;
this.client = client;
}
public play(type: string, value: string) {
const result = this.client.search(type, value);
this.outputInterface.play(type, result);
}
public pause() {
this.outputInterface.pause();
}
}
// All variations
const mockRequestClient = new MockRequestClient();
// Spotify
const spotifyAuth = new SpotifyAuth('myclientid', 'myclientsecret');
spotifyAuth.authorize();
const spotifyAPIClient = new SpotifyAPIClient(mockRequestClient, spotifyAuth.token);
const spotifyAppleScriptOutput = new SpotifyAppleScriptOutput();
const spotify = new CommandManager(spotifyAppleScriptOutput, spotifyAPIClient);
// Apple Music
const appleMusicAuth = new AppleMusicAuth('myclientid', 'myclientsecret');
appleMusicAuth.authorize();
const appleMusicAPIClient = new AppleMusicAPIClient(mockRequestClient, spotifyAuth.token);
const appleMusicAppleScriptOutput = new AppleMusicAppleScriptOutput();
const appleMusic = new CommandManager(appleMusicAppleScriptOutput, appleMusicAPIClient);
spotify.play('artist', 'weeknd');
spotify.pause();
appleMusic.play('artist', 'weeknd');
appleMusic.pause();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment