Convert v3 api into v1
This commit is contained in:
		
							parent
							
								
									9a77135d30
								
							
						
					
					
						commit
						1dd7a6ebac
					
				@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					import config from '../../config';
 | 
				
			||||||
import { query } from '../../utils/axios-query';
 | 
					import { query } from '../../utils/axios-query';
 | 
				
			||||||
import { ConversionFeed, ConversionRates } from '../price-updater';
 | 
					import { ConversionFeed, ConversionRates } from '../price-updater';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -37,19 +38,26 @@ const emptyRates = {
 | 
				
			|||||||
  ZAR: -1,
 | 
					  ZAR: -1,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class FreeCurrencyApi implements ConversionFeed {
 | 
					type PaidCurrencyData = {
 | 
				
			||||||
  private API_KEY: string;
 | 
					  [key: string]: {
 | 
				
			||||||
  private apiUrlPrefix: string = `https://api.freecurrencyapi.com/v1/`;
 | 
					      code: string;
 | 
				
			||||||
 | 
					      value: number;
 | 
				
			||||||
  constructor(apiKey: string, paid = false) {
 | 
					 | 
				
			||||||
    this.API_KEY = apiKey;
 | 
					 | 
				
			||||||
    if (paid) {
 | 
					 | 
				
			||||||
      this.apiUrlPrefix = `https://api.currencyapi.com/v3/`;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type FreeCurrencyData = {
 | 
				
			||||||
 | 
					  [key: string]: number;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class FreeCurrencyApi implements ConversionFeed {
 | 
				
			||||||
 | 
					  private API_KEY = config.FIAT_PRICE.API_KEY;
 | 
				
			||||||
 | 
					  private PAID = config.FIAT_PRICE.PAID;
 | 
				
			||||||
 | 
					  private API_URL_PREFIX: string = this.PAID ? `https://api.currencyapi.com/v3/` : `https://api.freecurrencyapi.com/v1/`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  constructor() { }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async $getQuota(): Promise<any> {
 | 
					  public async $getQuota(): Promise<any> {
 | 
				
			||||||
    const response = await query(`${this.apiUrlPrefix}status?apikey=${this.API_KEY}`);
 | 
					    const response = await query(`${this.API_URL_PREFIX}status?apikey=${this.API_KEY}`);
 | 
				
			||||||
    if (response && response['quotas']) {
 | 
					    if (response && response['quotas']) {
 | 
				
			||||||
      return response['quotas'];
 | 
					      return response['quotas'];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -57,21 +65,36 @@ class FreeCurrencyApi implements ConversionFeed {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async $fetchLatestConversionRates(): Promise<ConversionRates> {
 | 
					  public async $fetchLatestConversionRates(): Promise<ConversionRates> {
 | 
				
			||||||
    const response = await query(`${this.apiUrlPrefix}latest?apikey=${this.API_KEY}`);
 | 
					    const response = await query(`${this.API_URL_PREFIX}latest?apikey=${this.API_KEY}`);
 | 
				
			||||||
    if (response && response['data']) {
 | 
					    if (response && response['data']) {
 | 
				
			||||||
 | 
					      if (this.PAID) {
 | 
				
			||||||
 | 
					        response['data'] = this.convertData(response['data']);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      return response['data'];
 | 
					      return response['data'];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return emptyRates;
 | 
					    return emptyRates;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async $fetchConversionRates(date: string): Promise<ConversionRates> {
 | 
					  public async $fetchConversionRates(date: string): Promise<ConversionRates> {
 | 
				
			||||||
    const response = await query(`${this.apiUrlPrefix}historical?date=${date}&apikey=${this.API_KEY}`);
 | 
					    const response = await query(`${this.API_URL_PREFIX}historical?date=${date}&apikey=${this.API_KEY}`);
 | 
				
			||||||
    if (response && response['data'] && response['data'][date]) {
 | 
					    if (response && response['data'] && (response['data'][date] || this.PAID)) {
 | 
				
			||||||
 | 
					      if (this.PAID) {
 | 
				
			||||||
 | 
					        response['data'] = this.convertData(response['data']);
 | 
				
			||||||
 | 
					        response['data'][response['meta'].last_updated_at.substr(0, 10)] = response['data'];
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      return response['data'][date];
 | 
					      return response['data'][date];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return emptyRates;
 | 
					    return emptyRates;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private convertData(data: PaidCurrencyData): FreeCurrencyData {
 | 
				
			||||||
 | 
					    const simplifiedData: FreeCurrencyData = {};
 | 
				
			||||||
 | 
					    for (const key in data) {
 | 
				
			||||||
 | 
					      simplifiedData[key] = data[key].value;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return simplifiedData;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default FreeCurrencyApi;
 | 
					export default FreeCurrencyApi;
 | 
				
			||||||
 | 
				
			|||||||
@ -71,7 +71,7 @@ class PriceUpdater {
 | 
				
			|||||||
    this.feeds.push(new BitfinexApi());
 | 
					    this.feeds.push(new BitfinexApi());
 | 
				
			||||||
    this.feeds.push(new GeminiApi());
 | 
					    this.feeds.push(new GeminiApi());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.currencyConversionFeed = new FreeCurrencyApi(config.FIAT_PRICE.API_KEY, config.FIAT_PRICE.PAID);
 | 
					    this.currencyConversionFeed = new FreeCurrencyApi();
 | 
				
			||||||
    this.setCyclePosition();
 | 
					    this.setCyclePosition();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user