-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement request, response, disk resource
- Loading branch information
Showing
11 changed files
with
276 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## Request | ||
|
||
### DSL build request | ||
|
||
```ruby | ||
req = Yadisk::Client::Request.get( | ||
url: 'https://httpbin.org/get', | ||
query: { 'hello' => 'world', 'other' => 'value' }, | ||
headers: { | ||
'Content-Type' => 'application/json', | ||
'Accept': 'application/json', | ||
'User-Agent' => 'My User Agent' | ||
} | ||
) | ||
puts req.to_curl | ||
``` | ||
|
||
```ruby | ||
req = Yadisk::Client::Request.build(config: Yadisk::Client.configuration) do |r| | ||
r.method = :get | ||
r.url = 'https://httpbin.org/get' | ||
r.query = { 'hello' => 'world', 'other' => 'value' } | ||
r.headers = { | ||
'Content-Type' => 'application/json', | ||
'Accept': 'application/json', | ||
'User-Agent' => 'My User Agent' | ||
} | ||
end | ||
puts req.to_curl | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
# frozen_string_literal: true | ||
|
||
module Yadisk | ||
NAME = 'Yadisk' | ||
end | ||
|
||
require 'yadisk/version' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,104 @@ | ||
require 'uri' | ||
require 'yadisk/client/request/curlify' | ||
require 'yadisk/client/request/http_faraday' | ||
|
||
module Yadisk | ||
class Client | ||
class Request | ||
# TODO | ||
include Curlify | ||
include HttpFaraday | ||
|
||
attr_accessor :config, | ||
:method, | ||
:headers, | ||
:url, | ||
:query, | ||
:body, | ||
:file_io, | ||
:filepath, | ||
:filename, | ||
:file_type | ||
|
||
def initialize(**params) | ||
@config = params[:config] || Client.configuration | ||
@method = params[:method] | ||
@headers = params[:headers] | ||
@url = params[:url] | ||
@query = params[:query] | ||
@body = params[:body] | ||
@file_io = params[:file_io] | ||
@filepath = params[:filepath] | ||
@filename = params[:filename] | ||
@file_type = params[:file_type] | ||
end | ||
|
||
class << self | ||
def build(**params, &block) | ||
req = new(**params) | ||
yield req | ||
req | ||
end | ||
|
||
def get(url:, query: nil, headers: {}) | ||
build(method: :get) do |r| | ||
r.url = url | ||
r.headers = headers | ||
r.query = query | ||
end | ||
end | ||
|
||
def post(url:, query: nil, headers: {}, body:) | ||
build( | ||
method: :post, | ||
url: url, | ||
query: query, | ||
headers: headers, | ||
body: body | ||
) | ||
end | ||
|
||
def patch(url:, query: nil, headers: {}, body:) | ||
build( | ||
method: :patch, | ||
url: url, | ||
query: query, | ||
headers: headers, | ||
body: body | ||
) | ||
end | ||
|
||
def put(url:, query: nil, headers: {}, body:) | ||
build( | ||
method: :put, | ||
url: url, | ||
query: query, | ||
headers: headers, | ||
body: body | ||
) | ||
end | ||
|
||
def delete(url:, query: nil, headers: {}, body:) | ||
build( | ||
method: :delete, | ||
url: url, | ||
query: query, | ||
headers: headers, | ||
body: body | ||
) | ||
end | ||
end | ||
|
||
def call | ||
perform_simple_request | ||
end | ||
|
||
def to_s | ||
to_curl | ||
end | ||
|
||
def inspect | ||
to_s | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Yadisk | ||
class Client | ||
class Request | ||
module Curlify | ||
def to_curl | ||
case method | ||
when :get | ||
parts = ["curl -X GET '#{url}'"] | ||
parts << curl_query_str(indent: 0) if !query.nil? && !query.empty? | ||
parts << curl_header_str(indent: 2) if !headers.nil? && !headers.empty? | ||
parts.join(" \\\n#{curl_space_prefix(indent: 2)}") | ||
end | ||
end | ||
|
||
def curl_space_prefix(indent: 2) | ||
"#{' ' * indent}" | ||
end | ||
|
||
def curl_query_str(indent: 2) | ||
"#{curl_space_prefix(indent: indent)}-G -d '#{URI.encode_www_form(query)}'" | ||
end | ||
|
||
def curl_header_str(indent: 2) | ||
headers | ||
.each_pair | ||
.map { |name, value| "-H '#{name}: #{value}'" } | ||
.join(" \\\n#{curl_space_prefix(indent: indent)}") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Yadisk | ||
class Client | ||
class Request | ||
module HttpFaraday | ||
def perform_simple_request | ||
faraday_resp = http_faraday.send(method) do |req| | ||
req.url url | ||
req.params = query if !query.nil? | ||
req.headers = headers if !headers.nil? && !headers.empty? | ||
req.body = body if !body.nil? | ||
end | ||
|
||
Response.build do |resp| | ||
resp.request = self | ||
resp.status_code = faraday_resp.env.status | ||
resp.status_reason = faraday_resp.env.reason_phrase | ||
resp.headers = faraday_resp.headers | ||
resp.body = faraday_resp.body | ||
end | ||
end | ||
|
||
def http_faraday | ||
@_http_faraday ||= Faraday.new(http_faraday_connection_options) do |builder| | ||
http_faraday_build(builder) | ||
end | ||
end | ||
|
||
def http_faraday_connection_options | ||
{ | ||
request: { | ||
timeout: 30 | ||
}.merge(config.faraday_request_options || {}) | ||
} | ||
end | ||
|
||
def http_faraday_build(builder) | ||
if config.faraday_logger | ||
case config.faraday_logger | ||
when Array | ||
builder.response :logger, *config.faraday_logger | ||
else | ||
builder.response :logger, config.faraday_logger | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,5 @@ def disk = @disk | |
end | ||
end | ||
|
||
require 'yadisk/client/resources/v1/helpers' | ||
require 'yadisk/client/resources/v1/disk' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Yadisk | ||
class Client | ||
module Resources | ||
class V1 | ||
module HttpHeaders | ||
def user_agent | ||
"#{Yadisk::NAME}/#{Yadisk::VERSION}" | ||
end | ||
|
||
def oauth_token | ||
client.config.oauth_token | ||
end | ||
|
||
def default_headers | ||
{ | ||
"Content-Type" => "application/json", | ||
"User-Agent" => user_agent | ||
} | ||
end | ||
|
||
def auth_headers | ||
{ | ||
"Authorization" => "OAuth #{oauth_token}" | ||
} | ||
end | ||
|
||
def build_headers(headers = {}, with_auth: true) | ||
default_headers.then { |h| with_auth ? h.merge(auth_headers) : h }.merge(headers) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
module Yadisk | ||
class Client | ||
class Response | ||
# TODO | ||
attr_accessor :request, | ||
:status_code, | ||
:status_reason, | ||
:headers, | ||
:body | ||
|
||
class << self | ||
def build(&block) | ||
resp = new | ||
yield resp | ||
resp | ||
end | ||
end | ||
end | ||
end | ||
end |