Skip to content

Commit

Permalink
Add secureConnection parameter (#3)
Browse files Browse the repository at this point in the history
Allows using https with LFMRequest
  • Loading branch information
valentinschabschneider authored Dec 18, 2020
1 parent f82a189 commit bd0e478
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ export default class LFMBase {
protected key:string;
protected secret:string;
protected userAgent:string;
protected secureConnection: boolean;

public constructor(apiKey:string, apiSecret:string = "", userAgent:string = "lastfm-typed-npm") {
public constructor(apiKey:string, apiSecret:string = "", userAgent:string = "lastfm-typed-npm", secureConnection:boolean = false) {
this.key = apiKey;
this.secret = apiSecret;
this.userAgent = userAgent;
this.secureConnection = secureConnection;
}

protected checkLimit(limit:number|undefined, maxLimit:number) {
Expand Down Expand Up @@ -48,7 +50,7 @@ export default class LFMBase {
}

protected async sendRequest(apiKey:string, apiSecret:string, params:LFMArgumentObject) {
return await new LFMRequest(apiKey, apiSecret, this.userAgent, params).execute();
return await new LFMRequest(apiKey, apiSecret, this.userAgent, this.secureConnection, params).execute();
}

}
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ export default class LastFM {
public user:UserClass;
public helper:HelperClass;

public constructor(apiKey:string, apiSecret:string = "", userAgent:string = "lastfm-typed-npm") {
this.tag = new TagClass(apiKey, apiSecret, userAgent);
this.geo = new GeoClass(apiKey, apiSecret, userAgent);
this.chart = new ChartClass(apiKey, apiSecret, userAgent);
this.auth = new AuthClass(apiKey, apiSecret, userAgent);
this.album = new AlbumClass(apiKey, apiSecret, userAgent);
this.artist = new ArtistClass(apiKey, apiSecret, userAgent);
this.library = new LibraryClass(apiKey, apiSecret, userAgent);
this.track = new TrackClass(apiKey, apiSecret, userAgent);
this.user = new UserClass(apiKey, apiSecret, userAgent);
public constructor(apiKey:string, apiSecret:string = "", userAgent:string = "lastfm-typed-npm", secureConnection:boolean = false) {
this.tag = new TagClass(apiKey, apiSecret, userAgent, secureConnection);
this.geo = new GeoClass(apiKey, apiSecret, userAgent, secureConnection);
this.chart = new ChartClass(apiKey, apiSecret, userAgent, secureConnection);
this.auth = new AuthClass(apiKey, apiSecret, userAgent, secureConnection);
this.album = new AlbumClass(apiKey, apiSecret, userAgent, secureConnection);
this.artist = new ArtistClass(apiKey, apiSecret, userAgent, secureConnection);
this.library = new LibraryClass(apiKey, apiSecret, userAgent, secureConnection);
this.track = new TrackClass(apiKey, apiSecret, userAgent, secureConnection);
this.user = new UserClass(apiKey, apiSecret, userAgent, secureConnection);
this.helper = new HelperClass(this);
}
}
8 changes: 5 additions & 3 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ export class LFMRequest {
private secret:string;
private response:any;
private userAgent:string;
private connectionType:string;

public constructor(key:string, secret:string, userAgent:string, params:LFMArgumentObject) {
public constructor(key:string, secret:string, userAgent:string, secureConnection:boolean, params:LFMArgumentObject) {

this.key = key;
this.params = params;
this.secret = secret;
this.userAgent = userAgent;
this.connectionType = secureConnection ? "https" : "http";

}

Expand Down Expand Up @@ -130,7 +132,7 @@ export class LFMRequest {

const paramString = stringify(requestParam);

return await fetch("http://ws.audioscrobbler.com/2.0/", {
return await fetch(`${this.connectionType}://ws.audioscrobbler.com/2.0/`, {
method: "POST",
headers: {
"Content-Length": Buffer.byteLength(paramString).toString(),
Expand All @@ -150,7 +152,7 @@ export class LFMRequest {
...this.params
};

return await fetch(`http://ws.audioscrobbler.com/2.0?${stringify(params)}`, {
return await fetch(`${this.connectionType}://ws.audioscrobbler.com/2.0?${stringify(params)}`, {
method: "GET",
headers: {
"User-Agent": this.userAgent
Expand Down

0 comments on commit bd0e478

Please sign in to comment.