Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/ruby-http #472

Merged
merged 19 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
0f4002a
initial commit: ruby http nethttp
Aug 11, 2021
a2ea39f
implemented a send async method for ruby
Aug 12, 2021
dd546d2
updated README.md supported languages table to include ruby http pac…
Aug 12, 2021
ecd2458
removed commented out code in the rubyrefiner and ruby method writer
Aug 12, 2021
7ded7b5
Changed default namespaces for abstractions serialization files back …
Aug 12, 2021
4e08179
updated the abstractions package to include up-to-date methods and pa…
Aug 12, 2021
82c7390
removed defualt values for de/serialization parameters in http_core c…
Aug 12, 2021
11d7b81
updated abstractions core files to include changes required to fully …
Aug 13, 2021
a47a468
updated abstractions version in http package, updated send_async to a…
Aug 13, 2021
f11cca2
bumbped serialization version - updated abstraction dependency versio…
Aug 13, 2021
bbde2c1
updated the ruby generation refiner and writers to not write indexers…
Aug 13, 2021
e93292c
Merge branch 'main' into feature/ruby-http
abfarah Aug 13, 2021
eb1eb43
tested for no namespace in requestBuilderBody
Aug 13, 2021
0a7a66e
Removed get_auth_token method
abfarah Aug 13, 2021
0ceebd8
Added TODO: comments for missing work
abfarah Aug 13, 2021
167864b
updated test to check for Void return type in requestExecuterBody
Aug 13, 2021
6d48804
- adds changelog entry for ruby http
baywet Aug 16, 2021
9671e61
- fixes an issue where the namespace prefix would not be added for en…
baywet Aug 16, 2021
3f2fb70
- adds support for other http methods and request body in ruby
baywet Aug 16, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions .github/workflows/http-ruby-nethttp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: Ruby http nethttp

on:
workflow_dispatch:
push:
branches: [ main ]
paths: ['http/ruby/**', '.github/workflows/**']
pull_request:
paths: ['http/ruby/**', '.github/workflows/**']

jobs:
build:
env:
relativePath: ./http/ruby/nethttp/microsoft_kiota_nethttp
BUNDLE_HTTPS://RUBYGEMS__PKG__GITHUB__COM/MICROSOFT/: "${{ secrets.PUBLISH_GH_USERNAME }}:${{ secrets.PUBLISH_GH_TOKEN }}"
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
ruby-version: [2.5, 2.6, 2.7, '3.0', head, jruby, jruby-head, truffleruby, truffleruby-head]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
bundler: 'latest'
cache-version: 1
working-directory: ${{ env.relativePath }}
- name: Run tests
run: bundle exec rake
working-directory: ${{ env.relativePath }}
- name: Upload artifacts for ruby version 3 and ubuntu
if: ${{ matrix.os == 'ubuntu-latest' && matrix.ruby-version == '3.0'}}
uses: actions/upload-artifact@v2
with:
name: drop
path: |
${{ env.relativePath }}/Gemfile.lock
${{ env.relativePath }}/README.md
deploy:
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
environment:
name: staging_feeds
runs-on: ubuntu-latest
env:
relativePath: ./http/ruby/nethttp/microsoft_kiota_nethttp
needs: [build]
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
ruby-version: '3.0'
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
bundler: 'latest'
cache-version: 1
working-directory: ${{ env.relativePath }}
- run: bundle exec rake
working-directory: ${{ env.relativePath }}
- name: Publish to GPR
run: |
mkdir -p $HOME/.gem
touch $HOME/.gem/credentials
chmod 0600 $HOME/.gem/credentials
printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
gem build *.gemspec
gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
working-directory: ${{ env.relativePath }}
env:
GEM_HOST_API_KEY: "Bearer ${{secrets.PUBLISH_GH_TOKEN}}"
OWNER: ${{ github.repository_owner }}
# deploy_prod:
# if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
# environment:
# name: production_feeds
# runs-on: ubuntu-latest
# env:
# relativePath: ./http/ruby/nethttp/microsoft_kiota_nethttp
# needs: [build]
# steps:
# - uses: actions/checkout@v2
# - uses: ruby/setup-ruby@v1
# with:
# # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
# ruby-version: '3.0'
# bundler-cache: true # runs 'bundle install' and caches installed gems automatically
# - run: bundle exec rake
# - name: Publish to RubyGems
# run: |
# mkdir -p $HOME/.gem
# touch $HOME/.gem/credentials
# chmod 0600 $HOME/.gem/credentials
# printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
# gem build *.gemspec
# gem push *.gem
# env:
# GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}" TODO: Token for rubyGems.org

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The following table provides an overview of the languages supported by Kiota and
| Java | [✔](https://github.com/microsoft/kiota/projects/7) | [✔](./abstractions/java) | [JSON](./serialization/java/json) | [Azure](./authentication/java/azure) | [✔](./http/java/okhttp) | [link](./requiredtools/java.md) |
| PHP | [❌](https://github.com/microsoft/kiota/projects/4) | [▶](https://github.com/microsoft/kiota/pull/321) | ❌ | ❌ | ❌ | |
| Python | [❌](https://github.com/microsoft/kiota/projects/3) | ❌ | ❌ | ❌ | ❌ | |
| Ruby | [✔](https://github.com/microsoft/kiota/projects/6) | [✔](./abstractions/ruby) | [JSON](./serialization/ruby/json/microsoft_kiota_serialization) | [❌](https://github.com/microsoft/kiota/issues/421) | [❌](https://github.com/microsoft/kiota/issues/419) | |
| Ruby | [✔](https://github.com/microsoft/kiota/projects/6) | [✔](./abstractions/ruby) | [JSON](./serialization/ruby/json/microsoft_kiota_serialization) | [❌](https://github.com/microsoft/kiota/issues/421) | [✔](./http/ruby/nethttp/microsoft_kiota_nethttp)| |
| TypeScript/JavaScript | [✔](https://github.com/microsoft/kiota/projects/2) | [✔](./abstractions/typescript) | [JSON](./serialization/typescript/json) | [Azure](./authentication/typescript/azure) | [✔](./http/typescript/fetch) | [link](./requiredtools/typescript.md) |

> Legend: ✔ -> in preview, ❌ -> not started, ▶ -> in progress.
Expand Down
2 changes: 2 additions & 0 deletions abstractions/ruby/microsoft_kiota_abstractions/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ gem 'rake', '~> 13.0'
gem 'rspec', '~> 3.0'

gem 'rubocop', require: false

gem 'concurrent-ruby', '~> 1.1', '>= 1.1.9'
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative "serialization/parse_node_factory"
require_relative "serialization/parse_node_factory_registry"
require_relative "serialization/serialization_writer_factory"
require_relative "serialization/serialization_writer_factory_registry"

module MicrosoftKiotaAbstractions
module ApiClientBuilder

def get_authorization_token(request_url)
abfarah marked this conversation as resolved.
Show resolved Hide resolved
raise NotImplementedError.new
end

#TODO: Implement default de/serializer registration in the api_client generation issue #478
def register_default_serializer(factory_class)
begin
factory = factory_class.new()
MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.new().content_type_associated_factories[factory.get_valid_content_type(), factory]
rescue => exception
raise exception
end
end

def register_default_deserializer(factory_class)
begin
factory = factory_class.new()
MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.new().content_type_associated_factories[factory.get_valid_content_type(), factory]
rescue => exception
raise exception
end
end
end
end

Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ module MicrosoftKiotaAbstractions
module HttpCore
include ResponseHandler

def sendAsync(request_info=RequestInfo.new, response_handler=ResponseHandler)
raise NotImplementedError.new
end

def sendPrimitiveAsync(request_info=RequestInfo.new, response_handler=ResponseHandler)
raise NotImplementedError.new
end

def sendAsync(request_info=RequestInfo.new, response_handler=ResponseHandler)
def sendAsync(request_info, type, response_handler)
raise NotImplementedError.new
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,19 @@ def set_content_from_parsable(serializer_factory, content_type, values)
end
end

def set_headers_from_raw_object(h)
if !h
return
end
h.select{|x,y| @headers[x.to_s] = y.to_s}
end

def set_query_string_parameters_from_raw_object(q)
if !q
return
end
q.select{|x,y| @query_parameters[x.to_s] = y.to_s}
end

end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaAbstractions
VERSION = "0.1.2"
VERSION = "0.1.3"
end
11 changes: 11 additions & 0 deletions http/ruby/nethttp/microsoft_kiota_nethttp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions http/ruby/nethttp/microsoft_kiota_nethttp/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
19 changes: 19 additions & 0 deletions http/ruby/nethttp/microsoft_kiota_nethttp/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec

git_source(:github) { |repo_name| 'https://rubygems.pkg.github.com/microsoft' }

source 'https://rubygems.pkg.github.com/microsoft' do
gem 'microsoft_kiota_abstractions', '0.1.3'
end

gem 'rake', '~> 13.0'

gem 'rspec', '~> 3.0'

gem 'rubocop', require: false

gem 'concurrent-ruby', '~> 1.1', '>= 1.1.9'
50 changes: 50 additions & 0 deletions http/ruby/nethttp/microsoft_kiota_nethttp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# MicrosoftKiotaNethttp

![Ruby](https://github.com/microsoft/kiota/actions/workflows/http-ruby-nethttp.yml/badge.svg)

## Using the core implementations

Option 1: Bundler config

```shell
bundle config https://rubygems.pkg.github.com/microsoft/kiota USERNAME:TOKEN
```

Option 2: Configuring `~/.gemrc` file

```
---
:backtrace: false
:bulk_threshold: 1000
:sources:
- https://rubygems.org/
- https://USERNAME:TOKEN@rubygems.pkg.github.com/microsoft/kiota
:update_sources: true
:verbose: true
```

## Installation

Add this line to your application's Gemfile:

```ruby
source "https://rubygems.pkg.github.com/microsoft" do
gem "microsoft_kiota_nethttp", "0.1.0"
end
```

And then execute:

```shell
bundle install
```

Or install it yourself as:

```shell
gem install microsoft_kiota_nethttp --version "0.1.0" --source "https://{USERNAME}{PASSWORD/TOKEN}rubygems.pkg.github.com/microsoft"
```

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/microsoft/kiota.
10 changes: 10 additions & 0 deletions http/ruby/nethttp/microsoft_kiota_nethttp/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task default: :spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require_relative 'microsoft_kiota_nethttp/version'
require_relative 'microsoft_kiota_nethttp/http_core'

module MicrosoftKiotaNethttp
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require 'microsoft_kiota_abstractions'
require 'net/https'
require 'net/http'
require 'concurrent'

module MicrosoftKiotaNethttp
class HttpCore
include MicrosoftKiotaAbstractions::HttpCore
include Concurrent::Async

attr_accessor :authorization_header_key, :content_type_header_key, :parse_node_factory, :serialization_writer_factory, :client

def initialize(authentication_provider, parse_node_factory, serialization_writer_factory, client = Net::HTTP)
abfarah marked this conversation as resolved.
Show resolved Hide resolved
if !authentication_provider
raise StandardError , 'authentication provider cannot be null'
end
@authentication_provider = authentication_provider
@authorization_header_key = 'Authorization'
@content_type_header_key = 'Content-Type'
@parse_node_factory = parse_node_factory
@serialization_writer_factory = serialization_writer_factory
abfarah marked this conversation as resolved.
Show resolved Hide resolved
@client = client
end

def send_async(request_info, type, response_handler)
if !request_info
raise StandardError, 'requestInfo cannot be null'
end

self.await.add_bearer_if_not_present(request_info)
uri = request_info.uri
http = @client.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = self.get_request_from_request_info(request_info)
response = http.request(request)

if response_handler
return response_handler.await.handle_response_async(response);
else
payload = response.body
response_content_type = self.get_response_content_type(response);
if !response_content_type
raise StandardError, 'no response content type found for deserialization'
end
root_node = @parse_node_factory.get_parse_node(response_content_type, payload)
root_node.get_object_value(type)
end
end

def add_bearer_if_not_present(request_info)
if !request_info.uri
raise StandardError, 'uri cannot be null'
end
if !request_info.headers.has_key?(@authorization_header_key)
token = @authentication_provider.await.get_authorization_token(request_info.uri).value
if !token
raise StandardError, 'Could not get an authorization token'
end
if !request_info.headers
request_info.headers Hash.new()
end
request_info.headers[@authorization_header_key] = 'Bearer ' + token
end
end

def get_request_from_request_info(request_info)
#TODO Add swtich using reequest_info.http_method for the different types of requests
abfarah marked this conversation as resolved.
Show resolved Hide resolved
request = @client::Get.new(request_info.uri.request_uri)
if request_info.headers.instance_of? Hash
abfarah marked this conversation as resolved.
Show resolved Hide resolved
request_info.headers.select{|k,v| request[k] = v }
end
request
end

def get_response_content_type(response)
begin
response['content-type'].split(';')[0].downcase()
rescue
return nil
end
end

end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaNethttp
VERSION = '0.1.0'
end
Loading