Skip to content

Commit

Permalink
Merge pull request #25 from SanduDS/meidum2
Browse files Browse the repository at this point in the history
Adding medium connector
  • Loading branch information
indikasampath2000 authored Jun 28, 2021
2 parents 506e7ab + 50f3fa3 commit 6797ff0
Show file tree
Hide file tree
Showing 6 changed files with 660 additions and 0 deletions.
1 change: 1 addition & 0 deletions openapi/medium/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
11 changes: 11 additions & 0 deletions openapi/medium/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
org = "ballerinax"
name = "medium"
version = "0.1.0"
keywords = ["Medium", "Publication"]
repository = "https://github.com/ballerina-platform/ballerinax-openapi-connectors"
authors = ["Ballerina"]
license = ["Apache-2.0"]

[build-options]
observabilityIncluded = true
23 changes: 23 additions & 0 deletions openapi/medium/Package.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Connects to Medium from Ballerina.

## Module Overview

The Medium connector consume the data exposed in https://developers.medium.com/. It is currently supporting the following operations.

- getUserDetail
- createUserPost
- getPublicationList
- getContributorList


## Compatibility

| Ballerina Language Versions | Medium API |
|:----------------------------:|:-----------------:|
| Swan Lake Alpha 5 | V1 |

## Configuration

In order to publish on behalf of a Medium account, you will need an access token. An access token grants limited access to a user’s account. We offer two ways to acquire an access token: browser-based OAuth authentication, and self-issued access token.

Please follow this [link](https://github.com/Medium/medium-api-docs#2-authentication) to obtain the tokens.
84 changes: 84 additions & 0 deletions openapi/medium/client.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;

# Configuration record for Medium
#
# + authConfig - Bearer Token or OAuth2 Refresh Token Grant Configuration Tokens
# + secureSocketConfig - Secure Socket Configuration
public type ClientConfig record {
http:BearerTokenConfig|http:OAuth2RefreshTokenGrantConfig authConfig;
http:ClientSecureSocket secureSocketConfig?;
};

# Client endpoint for Medium API
#
# + clientEp - Client for Medium API
public client class Client {
http:Client clientEp;
public isolated function init(ClientConfig clientConfig, string serviceUrl = "https://api.medium.com/v1") returns error? {
http:ClientSecureSocket? secureSocketConfig = clientConfig?.secureSocketConfig;
http:Client httpEp = check new (serviceUrl, { auth: clientConfig.authConfig, secureSocket: secureSocketConfig });
self.clientEp = httpEp;
}
# Get the authenticated user’s details
#
# + return - If success returns details of the user who has granted permission to the application otherwise the
# relevant error
@display {label: "Get User Detail"}
remote isolated function getUserDetail() returns UserResponse|error {
string path = string `/me`;
UserResponse response = check self.clientEp-> get(path, targetType = UserResponse);
return response;
}
# List the user’s publications
#
# + userId - A unique identifier for the user.
# + return - If success returns a list of publications that the user is subscribed to, writes to, or edits otherwise
# the relevant error
@display {label: "Get Publication List"}
remote isolated function getPublicationList(@display {label: "User ID"} string userId) returns PublicationResponse|error {
string path = string `/users/${userId}/publications`;
PublicationResponse response = check self.clientEp-> get(path, targetType = PublicationResponse);
return response;
}
# List contributors for a given publication
#
# + publicationId - A unique identifier for the publication.
# + return - If success returns a list of contributors
@display {label: "Get Contributor List"}
remote isolated function getContributorList(@display {label: "Publication ID"} string publicationId) returns ContributorResponse|error {
string path = string `/publications/${publicationId}/contributors`;
ContributorResponse response = check self.clientEp-> get(path, targetType = ContributorResponse);
return response;
}
# Creates a post on the authenticated user’s profile
#
# + authorId - authorId is the user id of the authenticated user.
# + payload - Creates a post for user.
# + return - If success returns a Post record that includes the newly created post detail otherwise the relevant
# error
@display {label: "Create Post"}
remote isolated function createUserPost(@display {label: "User ID"} string authorId, @display {label: "Post Detail"} Post payload) returns PostDetails|error {
string path = string `/users/${authorId}/posts`;
http:Request request = new;
json jsonBody = check payload.cloneWithType(json);
request.setPayload(jsonBody);
PostDetails response = check self.clientEp->post(path, request, targetType=PostDetails);
return response;
}
}
Loading

0 comments on commit 6797ff0

Please sign in to comment.