Skip to content

Commit

Permalink
acyRsa v 0.0.0.9007
Browse files Browse the repository at this point in the history
  • Loading branch information
augustohassel committed May 7, 2020
1 parent 7d602db commit be7aad2
Show file tree
Hide file tree
Showing 19 changed files with 275 additions and 113 deletions.
6 changes: 6 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
^_pkgdown\.yml$
^docs$
^pkgdown$
^\.travis\.yml$
cran-comments.md
\.github
README\.Rmd
CODE_OF_CONDUCT\.md
FUNDING\.yml
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r

language: R
cache: packages
17 changes: 12 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: acyRsa
Type: Package
Title: Interface to ACyRSA's API BO
Version: 0.0.0.9006
Title: Interface to ACyRSA's API
Version: 0.0.0.9007
Authors@R: c(person("Augusto", "Hassel", role = c("aut", "cre"), email = "augusto.hassel@gmail.com"))
Description: Provides a convenient wrapper for consuming data from ACyRSA's BackOffice API.
License: MIT + file LICENSE
Expand All @@ -14,9 +14,16 @@ Imports:
dplyr,
httr,
jsonlite,
methods
Suggests:
methods,
glue,
magrittr,
purrr,
tidyr
tidyr,
tibble
Collate:
'acyRsa.R'
'attach.R'
's4_object.R'
'functions.R'
'functions_helpers.R'
'global.R'
5 changes: 2 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Generated by roxygen2: do not edit by hand

export("base_url<-")
export("token<-")
export(acyrsa_connection)
export(acyrsa_cotizaciones)
export(acyrsa_garantias_integradas)
export(acyrsa_login)
export(acyrsa_margenes)
export(agent)
export(base_url)
export(token)
export(user_name)
export(valid_until)
import(dplyr)
import(httr)
Expand Down
19 changes: 14 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
# acyRsa 0.0.0.9005
## acyRsa 0.0.0.9007

### Changes

* I've improved log-in method by storing more information into it.
* Now sending 'user-agent' in each API request.
* Preparing repo to apply to CRAN
* Fix issue #2

## acyRsa 0.0.0.9005

* Added a `NEWS.md` file to track changes to the package.
* Using `pkgdown` to build the documentation.

# acyRsa 0.0.0.9004
## acyRsa 0.0.0.9004

## New methods
### New methods

* `acyrsa_cotizaciones()`

# acyRsa 0.0.0.9001
## acyRsa 0.0.0.9001

## New Methods
### New Methods

* `acyrsa_connection()`
* `acyrsa_login()`
Expand Down
2 changes: 1 addition & 1 deletion R/attach.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.onAttach <- function(...) {
packageStartupMessage(sprintf("acyRsa v.%s\nDeveloped by HasselPunk \nwww.hasselpunk.com", packageVersion("acyRsa")))
packageStartupMessage(sprintf("acyRsa v.%s\nDeveloped by HasselPunk \nwww.hasselpunk.com", utils::packageVersion("acyRsa")))
}
63 changes: 20 additions & 43 deletions R/functions.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#' @export acyrsa_connection
#' @export acyrsa_login
#' @export acyrsa_margenes
#' @export acyrsa_garantias_integradas
Expand All @@ -8,33 +7,6 @@ NULL
#' @include s4_object.R
#' NULL

#' @title cyRsaConnection Object
#'
#' @description `acyrsa_connection()` creates a New Connection Object. It should be created manually when saving the token outside the application.
#'
#' @param token String. \strong{Mandatory} Obtained with \code{\link{acyrsa_login}}
#' @param base_url String. \strong{Mandatory} URL given to \code{\link{acyrsa_login}} to initiate the connection.
#'
#' @return Creates an 'acyRsaConnection' S4 Object
#'
#' @section Accesors:
#' You can use accesors to get information about the Object by using:
#' \itemize{
#' \item \code{token(conn)}
#' \item \code{base_url(conn)}
#' \item \code{valid_until(conn)}
#' }
#'
#' @family connection functions
#'
#' @examples
#' \dontrun{
#' conn <- acyrsa_connection(token = "1234", base_url = "https://api.anywhereportfolio.com.ar/")
#' }
acyrsa_connection <- function(token, base_url) {
new("acyRsaConnection", token = token, base_url = base_url, valid_until = as.character(Sys.Date()))
}

#' @title Log-in Method
#'
#' @description `acyrsa_login()` it's used to Log-in and obtained a valid token tthat then should be used in all requests to the API.
Expand All @@ -43,8 +15,8 @@ acyrsa_connection <- function(token, base_url) {
#' @param pass String. \strong{Mandatory} Password
#' @param env String. \strong{Mandatory} Wich environment are you going to connect:
#' \itemize{
#' \item 'demoapi' for testing
#' \item 'api' for production
#' \item \strong{demoapi} - for testing
#' \item \strong{api} - for production
#' }
#'
#' @return Creates an 'acyRsaConnection' S4 Object with a token and a base_url.
Expand All @@ -56,7 +28,9 @@ acyrsa_connection <- function(token, base_url) {
#' \itemize{
#' \item \code{token(conn)}
#' \item \code{base_url(conn)}
#' \item \code{valid_until(conn)}
#' \item \code{login_date_time(conn)}
#' \item \code{agent(conn)}
#' \item \code{user_name(conn)}
#' }
#'
#' @family connection functions
Expand All @@ -73,9 +47,7 @@ acyrsa_login <- function(user, pass, env) {
# Base URL
base_url <- glue('https://{env}.anywhereportfolio.com.ar/')

url <- glue(base_url, "AuthToken/AuthToken")

query <- POST(url = url,
query <- POST(url = glue(base_url, "AuthToken/AuthToken"),
query = list(nombreUsuario = user,
password = pass))

Expand All @@ -85,7 +57,9 @@ acyrsa_login <- function(user, pass, env) {
} else if (content(query)$Code == 200) {
message(glue("Succesfully connected with rRofex to {base_url}..."))

invisible(acyrsa_connection(token = content(query)$Value, base_url = base_url))
invisible(acyrsa_connection(token = content(query)$Value,
base_url = base_url,
user_name = user))

} else {
message(glue("Something went wrong...
Expand Down Expand Up @@ -113,11 +87,12 @@ acyrsa_margenes <- function(connection, date = Sys.Date()) {
if (!isS4(connection) || rev(class(connection)) != "acyRsaConnection" || !validObject(connection)) stop("The 'connection' must be a valid 'acyRsaConnection'.")
if (as.Date(connection@valid_until) != Sys.Date()) stop("The 'acyRsaConnection' is no longer valid. Please log-in again.")

if (!missing(date) & !.validate_date(date)) stop("Date must be given in the correct format")
if (!missing(date) & !.validate_fecha(date)) stop("Date must be given in the correct format")

query <- GET(url = glue(connection@base_url, "PosTrade/MarginRequirementReport"),
query = list(date = format.Date(date, "%Y%m%d")),
add_headers(.headers = c("Authorization" = glue("Token ", connection@token))))
add_headers(.headers = c("Authorization" = glue("Token ", connection@token))),
user_agent(connection@agent))

if (status_code(query) != 200) {
warn_for_status(query)
Expand Down Expand Up @@ -164,13 +139,14 @@ acyrsa_garantias_integradas <- function(connection, cim, alyc, date = Sys.Date()

if (missing(cim) | missing(alyc)) stop("Compensation Account Code and Clearing Member Code are needed.")

if (!missing(date) & !.validate_date(date)) stop("Date must be given in the correct format")
if (!missing(date) & !.validate_fecha(date)) stop("Date must be given in the correct format")

query <- GET(url = glue(connection@base_url, "PosTrade/MT506"),
query = list(date = format.Date(date, "%Y%m%d"),
cim = cim,
alyc = alyc),
add_headers(.headers = c("Authorization" = glue("Token ", connection@token))))
add_headers(.headers = c("Authorization" = glue("Token ", connection@token))),
user_agent(connection@agent))

if (status_code(query) != 200) {
warn_for_status(query)
Expand Down Expand Up @@ -261,7 +237,7 @@ acyrsa_cotizaciones <- function(connection, entry_type, date, Symbol, CFICode, M
if (some(entry_type, ~ !.x %in% c("2", "3", "5", "6", "B", "C", "D"))) stop("'entry_type' parameter is invalid. See documentation.")

if (missing(date)) stop("'date' parameter is required.")
if (!missing(date) & !.validate_date(date)) stop("Date must be given in the correct format.")
if (!missing(date) & !.validate_fecha(date)) stop("Date must be given in the correct format.")

if (!missing(MarketID) && some(MarketID, ~ !.x %in% c("ROFX", "XMAB"))) stop("'MarketID' parameter is invalid. See documentation.")

Expand All @@ -279,7 +255,8 @@ acyrsa_cotizaciones <- function(connection, entry_type, date, Symbol, CFICode, M
as.list(match.call())[-c(1:4)]
),
recursive = F),
add_headers(.headers = c("Authorization" = glue("Token ", connection@token))))
add_headers(.headers = c("Authorization" = glue("Token ", connection@token))),
user_agent(connection@agent))

if (status_code(query) != 200) {
warn_for_status(query)
Expand All @@ -288,9 +265,9 @@ acyrsa_cotizaciones <- function(connection, entry_type, date, Symbol, CFICode, M

message_for_status(query)

data <- fromJSON(toJSON(content(query)$Value))
data <- fromJSON(toJSON(content(query), digits = NA))

data <- data %>%
data <- data$Value %>%
mutate_all(., ~ replace_na(data = ., replace = NA)) %>%
unnest(Instrument) %>%
mutate_all(., ~ replace_na(data = ., replace = NA)) %>%
Expand Down
13 changes: 10 additions & 3 deletions R/functions_helpers.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# If date is invalid, returns FALSE
.validate_date <- function(date) {
tryCatch({!is.na(format.Date(x = date, "%Y-%m-%d"))}, error = function(cnd) {FALSE})
# Helper Functions ---------------------------

#' @title Helper: Date validation
#'
#' @description Validate date
#'
#' @param date Date
#' @return TRUE if date has a correct format.
.validate_fecha <- function(date) {
tryCatch({!is.na(format.Date(x = date, "%Y-%m-%d"))}, error = function(e) {FALSE})
}
1 change: 1 addition & 0 deletions R/global.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
utils::globalVariables(c(".", "Instrument", "MDFullGrp", "Party", "Accounts", "SubAccounts", "References"))
Loading

0 comments on commit be7aad2

Please sign in to comment.