npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@telefonica/api-client

v1.0.4

Published

The base client to interact with Telefónica's 4th Platform and Cognitive Services.

Downloads

10

Readme

API Client

The api-client package includes a set of helper classes to facilitate the creation of API clients, typically to interact with Telefónica's 4th Platform and Cognitive Services.

The most important class is the ApiClient class which includes a common set of functionality (mainly for instantiation and authentication purposes) which any API client can extend and reuse.

On the other hand, a set of error classes are provided to be thrown in case the remote service responds with an error. These error classes are:

  • ApiError: The base API error class including a static method fromStatusCode() from which errors can be instantiated from the statusCode returned by the remote service.
  • BadRequestError: The error instantiated in case the remote service responds with a Bad Request - 400 response.
  • ForbiddenError: The error instantiated in case the remote service responds with a Forbidden - 403 response.
  • NotFoundError: The error instantiated in case the remote service responds with a Not Found - 404 response.
  • InternalServerError: The error instantiated in case the or service responds with a Internal Server - 500 response.
  • GatewayTimeoutError: The error instantiated in case the or service responds with a Gateway Timeout - 504 response.
  • UnknownStatusCodeError: The error instantiated in case an unknown status code is passed to the ApiError.fromStatusCode() method.

Typically, an API client leans on the api.ts file automatically generated using the Swagger Codegen tool from the Swagger API specification file (typically located in the ./swagger directory) of the concrete remote service.

Taking into all this, the code to build a new API client to interact with a fictional external service which exposes a "fictionalServiceOperation" operation as stated in its Swagger API specification file is as simple as:

import * as _ from 'lodash';
import * as http from 'http';
const uuidv4 = require('uuid/v4');

const swagger: any = require('../swagger/fictional.json');
import { ApiClient, ApiError, ErrorCode, HttpMethod, Params } from '@telefonica/api-client';
import { FictionalServiceOperationResult, FictionalServiceApi } from './api';

/**
 * Considering the fictionalServiceOperation() is a POST operation with id equal
 * to "fictionalServiceOperationId" in the ./swagger/fictional.json file.
 */
const fictionalServiceOperationPath: string = _.findKey(swagger.paths, ['post.operationId', 'fictionalServiceOperationId']);

interface FictionalServiceOperationResponse {
    response: http.IncomingMessage;
    body: FictionalServiceOperationResult;
}

export class FictionalServiceClient extends ApiClient {
    public readonly apiName: string = swagger.info.title;

    constructor(basePath?: string, accessToken?: string) {
        super(new FictionalServiceApi(basePath), accessToken);
    }

    private throwFictionalServiceError(method: HttpMethod, url: string, params: Params, reason: any) {
        const statusCode: number = reason && reason.response && reason.response.statusCode;
        throw ApiError.fromStatusCode(this.apiName, ErrorCode['$' + statusCode], method, url, params, reason);
    }

    public async fictionalServiceOperation(param1: boolean, param2: number, param3: string, correlator?: string): Promise<FictionalServiceOperationResult> {
        const method: HttpMethod = HttpMethod.POST;
        const url: string = super.basePath + fictionalServiceOperationPath;
        const params: Params = { param1, param2, param3 };
        try {
            let response: FictionalServiceOperationResponse = await (this.api as any).fictionalServiceOperation(params, correlator || uuidv4());
            /* Everything OK, response 2XX status code received. */
            return response.body;
        } catch (reason) {
            /* Status code other than 2XX received or any other error thrown. */
            this.throwFictionalServiceError(method, url, params, reason);
        }
    }
}