Skip to content

Commit

Permalink
#36 added getKurjunQuota method
Browse files Browse the repository at this point in the history
added getCurrentUser method
refactored unit tests
  • Loading branch information
dilshat committed Oct 24, 2017
1 parent 4293546 commit e2f9e2d
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 46 deletions.
15 changes: 13 additions & 2 deletions src/main/java/io/subutai/client/api/HubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,23 @@ public String getCdnPrefix()
*/
List<String> getSharedUsers( String fileId );

/**
* Returns user quota on Kurjun
*
* @return {@link KurjunQuota}
*/
KurjunQuota getKurjunQuota();

User getCurrentUser();

/**
* Returns a currently active Kurjun token. The same token will be returned during {@link
* io.subutai.client.api.HubClient#KURJUN_TOKEN_TTL_MIN } for the same instance of the client. If no token is
* obtained yet or the current token is expired , a new token is attempted to be obtained. The client must be
* instantiated using factory method {@link io.subutai.client.impl.HubClients#getClient(String pgpKeyFilePath, *
* String pgpKeyPassword) } in order to be able to obtain a token from Kurjun.
* instantiated using factory method
*
* {@link io.subutai.client.impl.HubClients#getClient(String pgpKeyFilePath, String pgpKeyPassword) } in order to be
* able to obtain a token from Kurjun.
*/
String getKurjunToken();
}
41 changes: 41 additions & 0 deletions src/main/java/io/subutai/client/api/KurjunQuota.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.subutai.client.api;


import com.google.gson.GsonBuilder;


/**
* Represents user quota on Kurjun<br/> <b>left</b> - number of bytes unused<br/> <b>used</b> - number of bytes
* used<br/> <b>quota</b> - total number of bytes in quota
*/
public class KurjunQuota
{
private Long left;
private Long quota;
private Long used;


public Long getLeft()
{
return left;
}


public Long getQuota()
{
return quota;
}


public Long getUsed()
{
return used;
}


@Override
public String toString()
{
return new GsonBuilder().setPrettyPrinting().create().toJson( this );
}
}
18 changes: 18 additions & 0 deletions src/main/java/io/subutai/client/impl/HubClientImplementation.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import io.subutai.client.api.EnvironmentRef;
import io.subutai.client.api.FriendsInfo;
import io.subutai.client.api.HubClient;
import io.subutai.client.api.KurjunQuota;
import io.subutai.client.api.OperationFailedException;
import io.subutai.client.api.Organization;
import io.subutai.client.api.Peer;
Expand All @@ -71,6 +72,7 @@
import io.subutai.client.pgp.Signer;


//TODO login method should return current user to avoid second request
public class HubClientImplementation implements HubClient
{
private static final Logger LOG = LoggerFactory.getLogger( HubClientImplementation.class );
Expand All @@ -91,6 +93,7 @@ public class HubClientImplementation implements HubClient
private PGPSecretKey secretKey;
private long kurjunTokenSetTime;
private String kurjunToken = "";
User currentUser;


HubClientImplementation( HubEnv hubEnv )
Expand Down Expand Up @@ -135,6 +138,8 @@ public void login( final String username, final String password )
response = execute( request );

checkHttpStatus( response, HttpStatus.SC_OK, "login" );

currentUser = findUserByEmail( username );
}
finally
{
Expand Down Expand Up @@ -1374,6 +1379,13 @@ public List<String> getSharedUsers( final String fileId )
return kurjunClient.getSharedUsers( fileId, getKurjunToken() );
}


@Override
public KurjunQuota getKurjunQuota()
{
return kurjunClient.getQuota( currentUser.getFingerprint(), getKurjunToken() );
}

// <<<<< KURJUN

//**************
Expand Down Expand Up @@ -1467,6 +1479,12 @@ public synchronized String getKurjunToken()
}


public User getCurrentUser()
{
return currentUser;
}


String toJson( Object object )
{
return gson.toJson( object );
Expand Down
35 changes: 32 additions & 3 deletions src/main/java/io/subutai/client/impl/KurjunClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
import com.google.gson.reflect.TypeToken;

import io.subutai.client.api.HubClient;
import io.subutai.client.api.KurjunQuota;
import io.subutai.client.api.OperationFailedException;
import io.subutai.client.api.RawFile;
import io.subutai.client.api.Template;


//TODO add precondition checks for token where applicable
class KurjunClient
{
private static final String UTF8 = "UTF-8";
Expand Down Expand Up @@ -175,7 +177,7 @@ String uploadFile( final String filename, final String version, final String tok

void downloadFile( final String fileId, final String outputDirectory, final String token )
{
try ( CloseableHttpClient client = HttpClients.createDefault(); )
try ( CloseableHttpClient client = HttpClients.createDefault() )
{
HttpGet httpGet = new HttpGet( String.format( "%s/raw/download?id=%s&token=%s", getKurjunBaseUrl(),
URLEncoder.encode( fileId, UTF8 ), token ) );
Expand Down Expand Up @@ -210,13 +212,13 @@ void downloadFile( final String fileId, final String outputDirectory, final Stri
}


void removeFile( final String fileId, final String kurjunToken )
void removeFile( final String fileId, final String token )
{
CloseableHttpClient client = HttpClients.createDefault();
try
{
String url = String.format( "%s/raw/delete?id=%s&token=%s", getKurjunBaseUrl(),
URLEncoder.encode( fileId, UTF8 ), kurjunToken );
URLEncoder.encode( fileId, UTF8 ), token );

HttpDelete httpDelete = new HttpDelete( url );

Expand Down Expand Up @@ -317,6 +319,33 @@ List<String> getSharedUsers( final String fileId, final String token )
}


KurjunQuota getQuota( final String userFingerprint, final String token )
{
KurjunQuota quota;

HttpGet httpGet = new HttpGet( String.format( "%s/quota?user=%s&token=%s", getKurjunBaseUrl(), userFingerprint,
StringUtil.isBlank( token ) ? "" : token ) );

CloseableHttpClient client = HttpClients.createDefault();
try
{
CloseableHttpResponse response = execute( client, httpGet );

checkHttpStatus( response, HttpStatus.SC_OK, "get quota" );

quota = parse( response, new TypeToken<KurjunQuota>()
{
} );
}
finally
{
IOUtils.closeQuietly( client );
}

return quota;
}


private String getKurjunToken( String username, String signedAuthId )
{
HttpPost post = new HttpPost( String.format( "%s/auth/token", getKurjunBaseUrl() ) );
Expand Down
Loading

0 comments on commit e2f9e2d

Please sign in to comment.