github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/manage/app/purchase/purchase.service.ts (about) 1 import { Injectable } from '@angular/core'; 2 import { Http, Headers } from '@angular/http'; 3 4 import 'rxjs/add/operator/toPromise'; 5 6 import { Purchase } from './purchase'; 7 import { User } from '../user/user'; 8 import { Publication } from '../publication/publication'; 9 10 import { CrudService } from '../crud/crud.service'; 11 12 declare var Config: any; // this comes from the autogenerated config.js file 13 14 @Injectable() 15 export class PurchaseService extends CrudService<Purchase> { 16 baseLSDUrl: string = Config.lsd.url; 17 constructor ( 18 http: Http) { 19 super(); 20 this.http = http; 21 this.baseUrl = Config.frontend.url + '/api/v1/purchases'; 22 this.baseLSDUrl = Config.lsd.url; 23 } 24 25 decode(jsonObj: any): Purchase { 26 return { 27 id: jsonObj.id, 28 uuid: jsonObj.uuid, 29 publication: { 30 id: jsonObj.publication.id, 31 uuid: jsonObj.publication.uuid, 32 title: jsonObj.publication.title 33 }, 34 user: { 35 id: jsonObj.user.id, 36 uuid: jsonObj.uuid, 37 name: jsonObj.user.name, 38 email: jsonObj.user.email 39 }, 40 type: jsonObj.type, 41 licenseUuid: jsonObj.licenseUuid, 42 transactionDate: jsonObj.transactionDate, 43 startDate: jsonObj.startDate, 44 endDate: jsonObj.endDate, 45 status: jsonObj.status 46 }; 47 } 48 49 encode(obj: Purchase): any { 50 return { 51 id: Number(obj.id), 52 uuid: obj.uuid, 53 publication: { 54 id: Number(obj.publication.id) 55 }, 56 user: { 57 id: Number(obj.user.id) 58 }, 59 type: obj.type, 60 licenseUuid: obj.licenseUuid, 61 startDate: obj.startDate, 62 endDate: obj.endDate, 63 status: obj.status 64 } 65 } 66 67 getLicense(id: string): Promise<string> { 68 let licenseUrl = this.baseUrl + "/" + id + "/license"; 69 return this.http 70 .get( 71 licenseUrl, 72 { headers: this.defaultHttpHeaders }) 73 .toPromise() 74 .then(function (response) { 75 if (response.ok) { 76 return response.text() 77 } else { 78 throw 'Error retrieving license ' + response.text(); 79 } 80 }) 81 .catch(this.handleError); 82 } 83 84 revoke(message:string, licenseID:string): Promise<number> { 85 var headers: Headers = new Headers; 86 headers.append('Authorization', 'Basic ' + btoa(Config.lsd.user + ":" + Config.lsd.password)); 87 88 return this.http 89 .patch( 90 this.baseLSDUrl + "/licenses/" + licenseID + "/status", 91 {status: "revoked", 92 message: "Your license has been revoked because overused."}, 93 { headers: headers }) 94 .toPromise() 95 .then(function (response) { 96 if (response.ok) { 97 return 200; 98 } 99 return; 100 }) 101 .catch(this.handleRevokeError); 102 103 104 } 105 handleRevokeError(error: any): Promise<any> { 106 return error.status; 107 } 108 }