Code Monkey home page Code Monkey logo

urlshortener-rs's Introduction

urlshortener-rs

Crates badge CI MIT licensed

A very simple urlshortener for Rust.

This library aims to implement as much URL shortener services as possible and to provide an interface as minimal and simple as possible. For easing pain with dependency hell, the library provides request objects since 0.9.0 version which can be used for performing requests via user http-client library.

MSRV

The minimum supported rust version is bumped to 1.63 just because one of the dependencies. The code itself should work fine with Rust version 1.46, and, perhaps, even lower versions.

Implementations

Currently the following URL shorteners are implemented:

With authentication:

  • goo.gl
  • bit.ly
  • kutt.it (supports self hosting)

Without authentication:

  • bn.gy
  • is.gd
  • v.gd
  • bam.bz
  • fifo.cc
  • tiny.ph
  • tny.im
  • s.coop
  • bmeo.org
  • hmm.rs
  • url-shortener.io
  • biturl.top

The following services are supported, but are discouraged from use, due to restrictions such as rate limits:

  • tinyurl.com
  • psbe.co
  • rlu.ru
  • sirbz.com
  • hec.su
  • abv8.me
  • nowlinks.net

Usage without "client" feature

You can make a Request object without "client" feature only via provider functions:

extern crate urlshortener;

use urlshortener::providers::{Provider, self};

fn main() {
    let long_url = "https://google.com";
    let key = "MY_API_KEY";
    let req = providers::request(long_url, &Provider::GooGl { api_key: key.to_owned() });
    println!("A request object for shortening URL via GooGl: {:?}", req);
}

Usage with "client" feature

Without authentication

extern crate urlshortener;

use urlshortener::client::UrlShortener;

fn main() {
    let us = UrlShortener::new().unwrap();
    let long_url = "https://google.com";
    println!("Short url for google: {:?}", us.try_generate(long_url, None));
}

With authentication (Goo.Gl)

extern crate urlshortener;

use urlshortener::{ client::UrlShortener, providers::Provider };

fn main() {
    let us = UrlShortener::new().unwrap();
    let long_url = "https://google.com";
    let key = "MY_API_KEY";
    println!("Short url for google: {:?}", us.generate(long_url, Provider::GooGl { api_key: key.to_owned() }));
}

Combined (Goo.Gl + Is.Gd)

extern crate urlshortener;

use urlshortener::{ client::UrlShortener, providers::Provider };

fn main() {    
    let us = UrlShortener::new().unwrap();
    let providers = vec![
        Provider::GooGl { api_key: "MY_API_KEY".to_owned() },
        Provider::IsGd,
    ];
    let long_url = "https://rust-lang.org";
    println!("Short url for google: {:?}", us.try_generate(long_url, Some(providers)));
}

License

This project is licensed under the MIT license.

urlshortener-rs's People

Contributors

arshubham avatar dependabot-preview[bot] avatar iddm avatar ignatenkobrain avatar panicbit avatar paradoxspiral avatar timothyye avatar timvisee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

urlshortener-rs's Issues

Provider: kutt.it

How about support for kutt.it? This would be cool because it's a shortener that is open-source and easily self hosted in contrast to the current list of supported shorteners. A kutt.it instance provides an API and uses an API key.

What do you think? I might have some time to implement a provider for this.

Spurious build failures

Builds are spuriously failing.

Commit: 2c7dce4

Error:

running 1 test

test tests::providers ... FAILED

failures:

---- tests::providers stdout ----

    thread 'tests::providers' panicked at 'assertion failed: us.generate(url, provider).is_ok()', src/lib.rs:158

note: Run with `RUST_BACKTRACE=1` for a backtrace.

The error obviously wasn't the doc changes, so that leads me to believe it's either:

a. the 3 second timeout on the hyper request is not enough time to generate a URL for some services; or
b. the service simply did not respond

So this leads me to suggest one of three courses of action:

  1. Create a check in the test, where if the result of a generate call is a "Service is unavailable" error, then to either:

    a. skip the provider for the build and assume it works;
    b. re-try the provider up to 3 times;

  2. Increase the maximum time per request to a 5 second timeout;

  3. ignore provider errors (this essentially invalidates the tests). Instead, we test against sample stored outputs of each of these services, and simply check that decoding them works.

Personally, I prefer option 1A. I think that allowing over 3 seconds for a provider is too long (using try_generate would fix this, as it'd simply attempt the next provider), and re-trying probably wouldn't matter, because the service likely would still be slower than usual within a 1-minute window.

Parsing issue with bit.ly when long url contains "#"

ex: Long Url: google.com/asd#123
Output :
{"status_code":200,"status_txt":"OK","data":{"url":"http://bit.ly/2nYsQDH","hash":"2nYsQDH","global_hash":"ipdYoe","long_url":"http://google.com/asd","new_hash":0}}

Let the crate to build requests without sending

It would be useful for users of this crate just to have everything for requesting via their own method (not using hyper crate for example). To do this, we must add a method to the UrlShortener implementation which just returns an object which can be used for making requests by providing all the data: url, parameters, type of parameters, headers, and so on. By having this the user will be able to perform the requests as how it wants. For example, the user code may already have some hyper::Client instance, we may just use it instead of having our own client. Or, the user may have some another http-client library which he wants to use.

This issue could be done in another way: we use some trait "NetworkSender" with method "send" which the user can implement for it network sender (hyper client or anything else). Then we write a method of UrlShortener which accepts a NetworkSender trait object and uses it for sending the request and getting response.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.