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

metry-angular-sdk

v0.13.2

Published

Angular SDK for Metry API v2

Downloads

8

Readme

Metry Angular SDK

This repo contains the new SDK for connecting with Metry's API. This is an update of the previous energimolnet-ng. Compared to energimolnet-ng sdk, this sdk generates no endpoint classes, and contains no limitations on what requests you can perform for each endpoint. The endpoints and available HTTP methods are listed in the Metry API documentation.

Installation is now only handled with npm, and you need a bundler, such as webpack or browserify, to use it.

npm install metry-angular-sdk

Including in your angular app

angular.app('myApp', [
  ... your dependencies...,
  require('metry-angular-sdk')
]) ...

Setting the Base URL

Before performing any api requests, you need to configure what server to use. Unless you have recieved other instructions, use https://app.metry.io as base url.

angular.module('myModule').constant('METRY_BASE_URL', 'https://app.metry.io');

Authenticating user

There are currently three ways of authenticating a user.

  • OAuth
  • Private token
  • Manual authentication

The mryAuth service has a isAuthenticated() method that returns whether the user has either a private or refresh token set.

Oauth

In order to use OAuth, you need to register a client id and client secret with Metry. Contact [email protected] if you are interested in developing services on Metry.

When you have recieved your client id and client secret for your application, you need to configure the auth service to use these values.

angular.module('myModule').constant('METRY_AUTH_CONFIG', {
  disabled: false,
  clientId: <your client id>,
  clientSecret: <your client secret>,
  redirectUri: <the uri your app resides at, i.e. a web server or app url scheme>
});

Note that the redirectUri is specific for the client, so you'll need to update the client settings on Metry in order to change the URI of your app.

When the SDK detects unauthenticated api access, it will emit a mry:loginNeeded event on $rootScope. You should listen to this event and redirect the user to the login URL.

angular.module('myApp').run([
  '$rootScope',
  '$window',
  'mryAuth',
  function($rootScope, $window, Auth) {
    $rootScope.$on('mry:loginNeeded', function() {
      $window.location.href = Auth.authorizeUrl();
    });
  }
]);

Once the authentication is completed, the user will be redirected to the provided redirectUri. The oAuth auth code will be appended as a ?code=<auth code> to the uri. You need to define a state in your app that catches the refresh token and hands it over to the mryAuth service. The mryAuth service will then use this code to fetch a refresh and access token.

// Somewhere in your initial code where mryAuth is injected.
// var authCode = // extract code from url, e.g. by using $location.search

mryAuth.handleAuthCode(authCode).then(function() {
  // App is ready to request data from API
})

Private token

While developing, it might be convenient to use your private developer key. This key can be injected using the setPrivateToken method on mryAuth. Remember to keep it from being commited to public repos.

In .gitignored config file

angular.module('myConfig').constant('privateToken', 'myVerySecretTokenThatIShallNotCommitToPublicRepos');

In main app

angular.module('myApp').run([
  'mryAuth',
  'privateToken'
  function(auth, privateToken) {
    auth.setPrivateToken(privateToken);
  }
]);

The private key will always be used when available, overriding any OAuth authorization. To remove the private token, simply call setPrivateToken(null) on the mryAuth service.

Manual authentication

If you manually want to handle OAuth tokens, you can configure the mryAuth service to disable OAuth.

angular.module('myModule').constant('METRY_AUTH_CONFIG', {disabled: true});

Getting data

This SDK exposes a single mry function that is called with a collection name as the argument. That function then returns an object with get, save, delete, of, batch, and action.

angular.controller('myController', function(mry) {
  var _this = this;
  _this.meters = [];
  _this.user = null;

  mry('accounts').get('me').then(function(me) {
    _this.user = me;
    
    me.name = '1337 Hacker';
    return mry('accounts').save(me);
  }).then(function() {
    console.log('User is now a 1337 hacker');
  });

  mry('meters').query().then(function(res) {
    _this.meters = res.data;
  });
});

The of, batch and action functions are not yet used for any public endpoints and are therefore not documented.