viernes, 14 de septiembre de 2012

Once upon a time CURL

Have you had the chance to work with REST???

Something that I've come to learn working with REST is that people working with it are fond of the CURL command line.

Wikipedia defines CURL as:

"is a computer software project providing a library and command-line tool for transferring data using various protocols."

Thing is, I really don't like it. Not because it's a bad tool but because I just don't want to learn yet another command line, let's face it I'm lazy. Any way this people I was talking about have so much faith in CURL that may come to demand you to post the output of a CURL execution to validate what ever you're saying about a REST call.

Providing all this I thought it may be worth to write a small post about the really basic things that you can do with CRUL.

Now the prototype of the command it's quite simple:


curl [options] [URL...]


The options portion is the one that really matters.
Here is a list that I found useful, but keep an eye on the post for I may keep updating it:

 -H  this is for header. Any valid HTTP may be used here.

It's useful  for things like Authorization.

-X this can be used for for HTTP verbs

Like so -X DELETE, -X POST


So this would be how a valid call looks like:

curl  -H "Authorization: Basic sdaqwrdsfdsfafdszadsafdsafdsfad3" -X DELETE http://somehost.com/something?queryparam=somethingelese

Any way, for now this suits me but would you need more data please refer to:


Of course over there you'll find all there is to find about CURL, but I just like to post here things that I tend to forget.

Enjoy





viernes, 7 de septiembre de 2012

Retry Strategy Implementation

Hi, this small portion of code is to be used when ever you need to perform a task that may fail and in which event you should retry a predefined amount of times.

I won't be explaining the code in detail but if you have questions please to comment :D.

RetriableTask Class:


public class RetriableTask<V> implements Callable<V> {

    private static final int DEFAULT_RETRY_ATTEMPTS = 3;
    private static final int DEFAULT_TIME_BETWEEN_RETRIES = 1000;

    private Callable<V> task;
    private int retryAttempts;
    private int timeBetweenRetries;

    private int retryCount;

    public RetriableTask(Callable<V> task) {
        this(task, DEFAULT_RETRY_ATTEMPTS, DEFAULT_TIME_BETWEEN_RETRIES);
    }

    public RetriableTask(Callable<V> task, int retryAttempts, int timeBetweenRetries) {
        this.task = task;
        this.retryAttempts = retryAttempts;
        this.timeBetweenRetries = timeBetweenRetries;
        this.retryCount = retryAttempts;
    }

    @Override
    public V call() throws Exception {

        while (true) {
            try {
                return task.call();
            } catch (InterruptedException e) {
                throw e;
            } catch (CancellationException e) {
                throw e;
            } catch (Exception e) {
                retryCount--;
                if (retryCount == 0) {
                    throw new RetryException(retryAttempts + " attempts to retry failed at " + timeBetweenRetries
                            + "ms interval", e);
                }
                Thread.sleep(timeBetweenRetries);
            }
        }
    }

}


How to use the class above:

Put this code in a method of your choosing. Of course this can be perfected and used in such way as to change what ever task you want to perform in runtime.

Callable<T> task = new Callable<T>() {
@Override
public T call() throws Exception {
    return // WHAT EVER YOU NEED TO ACTUALLY DO;
        }
};
       
       
String response = "";
try {
    response = new RetriableTask<String>(task).call();
} catch (RetryException e) {
    // THIS WILL BE CATCH WHEN:
    // The call to the task fails and all the retries were exausted

    // So do what ever you need to do when your operation fails:
    // HERE
} catch (Exception e) {
    // If you reach this point something else
    // besides the actuall retry failed
    throw new RuntimeException(e);
}

Attach existing directory to a new GIT Hub Repo

So........

This one may sound familiar to you right?

You started working on something and at some point some one tells you: "oh!!!! brilliant upload it Git Hub so I can take a look at it"

And ad that point is when the problem starts.

So how do you update code you are working on (this is an existing project) to a repo you are about to create.

Well this is basically solved in StackOverflow BUT allow me to replicate it here ok?



1. Create the remote repository, and get the URL such asgit://github.com/youruser/somename.git

If your local GIT repo is already set up, skips steps 2 and 3

2. Locally, at the root directory of your source, git init

3. Locally, add and commit what you want in your initial repo (for everything, git add . git commit -m 'initial commit comment')to attach your remote repo with the name 'origin' (like cloning would do)

4. to attach your remote repo with the name 'origin' (like cloning would do) git remote add origin [URL From Step 1]

5. git pull origin master

6. to push up your master branch (change master to something else for a different branch): git push origin master

So this worked pretty well for me to have a look at it.

I case you wander this is the StackOverflow link