Skip to content

Commit

Permalink
First valid version of api
Browse files Browse the repository at this point in the history
  • Loading branch information
NoneTirex committed Jul 10, 2017
1 parent b59209b commit 8583f8b
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 125 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
target/
*.iml
*.iml
dependency-reduced-pom.xml
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
# Simpay-Api
# Simpay-Api

Example API usage:
```java
SimpayApi simpayApi = new SimpayApi("key", "secret");
try
{
SimpayStatusResponse statusResponse = simpayApi.getStatus(1, 7055, "code");
if (statusResponse.isSuccess())
{
//code is valid
System.out.println("Code is correct");
}
else if (statusResponse.isUsed())
{
//code is used [405]
LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(statusResponse.getRespond().getTimeUsed()), ZoneId.systemDefault());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss dd.MM.yyyy");
System.out.println("Code has already been used: " + formatter.format(dateTime));
}
else if (statusResponse.isNotFound())
{
//code is not found [404]
System.out.println("Code not found");
}
else
{
//some other errors [< 200]
SimpayError error = statusResponse.getError();
System.out.println("Undefined error: " + error.getName() + " [" + error.getCode() + "]");
}
}
catch (IOException e)
{
//problem with connection
e.printStackTrace();
}
```
45 changes: 32 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@

<build>
<defaultGoal>clean install</defaultGoal>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -33,19 +50,21 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>compile-shade</id>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
25 changes: 7 additions & 18 deletions src/main/java/pl/edu/tirex/simpay/api/SimpayApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pl.edu.tirex.simpay.api.connection.SimpayStatusResponse;
import pl.edu.tirex.simpay.api.models.SimpayAuth;
import pl.edu.tirex.simpay.api.models.SimpayStatusParameters;
import pl.edu.tirex.simpay.api.models.SimpayStatusRespond;
import pl.edu.tirex.simpay.api.utils.JsonEntity;
import pl.edu.tirex.simpay.api.utils.PayServiceUtils;

Expand All @@ -17,8 +16,7 @@
public class SimpayApi
{
private static final int SIMPAY_API_VERSION = 1;
private final String key;
private final String secret;
private final SimpayAuth auth;
private final int version;

public SimpayApi(String key, String secret)
Expand All @@ -28,30 +26,23 @@ public SimpayApi(String key, String secret)

public SimpayApi(String key, String secret, int version)
{
this.key = key;
this.secret = secret;
this.auth = new SimpayAuth(key, secret);
this.version = version;
}

public String getKey()
public SimpayAuth getAuth()
{
return key;
}

public String getSecret()
{
return secret;
return auth;
}

public int getVersion()
{
return version;
}

public SimpayStatusRespond getStatus(int serviceId, int number, String code) throws IOException
public SimpayStatusResponse getStatus(int serviceId, int number, String code) throws IOException
{
SimpayAuth auth = new SimpayAuth(this.key, this.secret);
SimpayStatusParameters parameters = new SimpayStatusParameters(auth, serviceId, number, code);
SimpayStatusParameters parameters = new SimpayStatusParameters(this.auth, serviceId, number, code);
SimpayStatusRequest statusRequest = new SimpayStatusRequest(parameters);

HttpClient httpClient = PayServiceUtils.getHttpClient();
Expand All @@ -60,10 +51,8 @@ public SimpayStatusRespond getStatus(int serviceId, int number, String code) thr
post.setEntity(new JsonEntity(statusRequest));

HttpResponse httpResponse = httpClient.execute(post);

SimpayStatusResponse statusResponse = PayServiceUtils.getGson().fromJson(new InputStreamReader(httpResponse.getEntity().getContent()), SimpayStatusResponse.class);

System.out.println(statusResponse);
return null;
return statusResponse;
}
}
21 changes: 0 additions & 21 deletions src/main/java/pl/edu/tirex/simpay/api/SimpayException.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ public void setParameters(P parameters)
this.parameters = parameters;
}

public List<SimpayError> getErrors()
public SimpayError getError()
{
return errors;
return this.errors.size() < 1 ? null : this.errors.get(0);
}

@Override
public String toString()
{
return "SimpayRequest{" + "errors=" + errors + ", parameters=" + parameters + '}';
final StringBuilder sb = new StringBuilder("SimpayRequest{");
sb.append("parameters=").append(parameters);
sb.append(", error=").append(getError());
sb.append('}');
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
package pl.edu.tirex.simpay.api.connection;

import com.google.gson.annotations.SerializedName;
import pl.edu.tirex.simpay.api.models.SimpayError;

import java.util.ArrayList;
import java.util.List;

public class SimpayResponse<R, P>
public class SimpayResponse<R, P> extends SimpayRequest<P>
{
@SerializedName("error")
private final List<SimpayError> errors = new ArrayList<>();

@SerializedName("params")
private P parameters;

private R respond;

public SimpayResponse(R respond, P parameters)
{
super(parameters);
this.respond = respond;
this.parameters = parameters;
}

public P getParameters()
{
return parameters;
}

public void setParameters(P parameters)
{
this.parameters = parameters;
}

public R getRespond()
Expand All @@ -42,14 +20,14 @@ public void setRespond(R respond)
this.respond = respond;
}

public List<SimpayError> getErrors()
{
return errors;
}

@Override
public String toString()
{
return "SimpayResponse{" + "errors=" + errors + ", parameters=" + parameters + ", respond=" + respond + '}';
final StringBuilder sb = new StringBuilder("SimpayResponse{");
sb.append("respond=").append(respond);
sb.append(", parameters=").append(getParameters());
sb.append(", error=").append(getError());
sb.append('}');
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pl.edu.tirex.simpay.api.connection;

import pl.edu.tirex.simpay.api.models.SimpayStatusParameters;

public class SimpayStatusRequest extends SimpayRequest<SimpayStatusParameters>
{
public SimpayStatusRequest(SimpayStatusParameters parameters)
{
super(parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,32 @@

import pl.edu.tirex.simpay.api.models.SimpayStatusParameters;
import pl.edu.tirex.simpay.api.models.SimpayStatusRespond;
import pl.edu.tirex.simpay.api.models.SimpayStatusType;

public class SimpayStatusResponse extends SimpayResponse<SimpayStatusRespond, SimpayStatusParameters>
{
public SimpayStatusResponse(SimpayStatusRespond respond, SimpayStatusParameters parameters)
{
super(respond, parameters);
}

public boolean isSuccess()
{
return this.getRespond() != null && this.getRespond().getStatus() == SimpayStatusType.OK;
}

public boolean isUsed()
{
return this.isError() && this.getRespond() != null && this.getRespond().getStatus() == SimpayStatusType.USED;
}

public boolean isNotFound()
{
return this.isError() && this.getError().getCode() == 404;
}

public boolean isError()
{
return !this.isSuccess() && this.getError() != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
public class SimpayStatusRespond
{
private final String code;
private final SimpayStatus status;
private final SimpayStatusType status;
private final int test;

@SerializedName("time_used")
private final long timeUsed;

public SimpayStatusRespond(String code, SimpayStatus status, int test, long timeUsed)
public SimpayStatusRespond(String code, SimpayStatusType status, int test, long timeUsed)
{
this.code = code;
this.status = status;
Expand All @@ -24,7 +24,7 @@ public String getCode()
return code;
}

public SimpayStatus getStatus()
public SimpayStatusType getStatus()
{
return status;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pl.edu.tirex.simpay.api.models;

public enum SimpayStatus
public enum SimpayStatusType
{
OK,
USED;
Expand Down
34 changes: 0 additions & 34 deletions src/test/java/pl/edu/tirex/simpay/api/Test.java

This file was deleted.

0 comments on commit 8583f8b

Please sign in to comment.