import { Injectable } from '@angular/core' ; import { Http , Response } from '@angular/http' ; import { Options } from '../options' ; import { Observable } from 'rxjs/Rx' ; import 'rxjs/add/operator/map' ; import { map } from 'rxjs/operators' ; import { api } from '../../../../config/code-gov-config.json' ; interface Options { base ? : string ; debug ? : boolean ; environment ? : string ; } export interface Requirements { agencyWidePolicy : number ; openSourceRequirement : number ; inventoryRequirement : number ; schemaFormat : number ; overallCompliance : number ; } export interface Agency { acronym : string ; name : string ; website ? : string ; codeUrl ? : string ; numRepos ? : string ; requirements ? : Requirements ; } export interface Contact { email : string ; } export interface License { URL : string ; name : string ; } export interface Permissions { usageType : string ; exemptionText : string ; licenses : License [ ] ; } export interface Date { created : string ; lastModified : string ; metadataLastUpdated : string ; } export interface RelatedCode { codeName : string ; codeURL : string ; isGovernmentRepo : boolean ; } export interface ReusedCode { name : string ; URL : string ; } export interface Repo { name : string ; description : string ; tags : string [ ] ; contact : Contact ; agency : Agency ; laborHours : number ; repositoryURL : string ; homepageURL : string ; permissions : Permissions ; date : Date ; vcs : string ; disclaimerText : string ; disclaimerURL : string ; relatedCode : RelatedCode [ ] ; reusedCode : ReusedCode [ ] ; repoID : string ; } export interface AgencyStatus { timestamp : string ; statuses : object ; } @ Injectable ( ) export class ClientService { private BASE : string = api . base ; private KEY : string = process . env . CODE_GOV_API_KEY || api . key || undefined ; constructor ( private http ) { } getStatuses ( ) { let url = this . BASE + 'status.json' ; return this . http . get ( url ) . map ( ( response ) => response . json ( ) ) . map ( ( data ) => { return data ; } ) ; } getAgencies ( ) { let url = this . BASE + `agencies?size=1000` ; if ( this . KEY ) { url += `&api_key=${ this . KEY }` ; } return this . http . get ( url ) . map ( ( response ) => response . json ( ) ) . map ( ( data ) => { return data . agencies . sort ( function ( a , b ) { return a . name . localeCompare ( b . name ) ; } ) ; } ) ; } getAgencyByAcronym ( acronym ) { let url = this . BASE + `agencies?size=1000` ; if ( this . KEY ) { url += `&api_key=${ this . KEY }` ; } return this . http . get ( url ) . map ( ( response ) => response . json ( ) ) . map ( ( data ) => { return data . agencies . find ( ( agency ) => agency . acronym === acronym ) ; } ) ; } getAgencyRepos ( acronym = '' , size = 10 ) { let url = this . BASE + `repos?agency.acronym=${ acronym }&size=${ size }&sort=name__asc` ; if ( this . KEY ) { url += `&api_key=${ this . KEY }` ; } return this . http . get ( url ) . map ( ( response ) => response . json ( ) ) . map ( ( data ) => data . repos ) ; } getRepoByID ( acronym = '' ) { let url = this . BASE + `repos/${ acronym }` ; if ( this . KEY ) { url += `?api_key=${ this . KEY }` ; } return this . http . get ( url ) . map ( ( response ) => response . json ( ) ) ; } suggest ( term = '' , size = 10 ) { let url = this . BASE + `terms?term=${ term }&size=${ size }` ; if ( this . KEY ) { url += `&api_key=${ this . KEY }` ; } return this . http . get ( url ) . map ( ( response ) => response . json ( ) ) . map ( ( data ) => data . terms ) ; } search ( text = '' , size = 10 ) { let permissionsFilter = '&permissions.usageType=openSource&permissions.usageType=governmentWideReuse' ; let url = this . BASE + `repos?q=${ text }` + permissionsFilter + `&size=${ size }` ; if ( this . KEY ) { url += `&api_key=${ this . KEY }` ; } return this . http . get ( url ) . map ( ( response ) => response . json ( ) ) . map ( ( data ) => { data . repos . filter ( repo => { return repo . permissions && repo . permissions . usageType && [ 'openSource' , 'governmentWideReuse' ] . indexOf ( repo . permissions . usageType ) > - 1 ; } ) ; return data ; } ) ; } }